/
requirejs.js
201 lines (177 loc) · 6.89 KB
/
requirejs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
(function () {
"use strict";
var path = require('path');
var config = require("../../config.js");
var grunt = config.grunt;
var rootPath = path.resolve();
var realFileNames = [];
function ensureRealNames() {
if (realFileNames.length === 0) {
grunt.file.recurse(config.compiledTsOutput, function (abspath) {
realFileNames.push(path.join(rootPath, abspath));
});
grunt.file.recurse('tasks/utilities', function (abspath) {
realFileNames.push(path.join(rootPath, abspath));
});
grunt.file.recurse('src/less', function (abspath) {
realFileNames.push(path.join(rootPath, abspath));
});
}
}
// ensure that the files discovered by requireJS have appropriate
// casing so that non-Windows builds will work.
function done(done, output) {
var lines = output.split('\n');
lines.splice(0, 3);
lines.pop();
// Remove empty: pattern resources added by Less build.
lines = lines.filter(function (line) {
return line.indexOf("empty:") === -1;
});
lines = lines.filter(function (line) {
return line.indexOf("require-json!") === -1;
});
lines = lines.map(function (line) {
return path.normalize(line.replace("require-style!", ""));
});
ensureRealNames();
lines.forEach(function (line) {
if (realFileNames.indexOf(line) === -1) {
grunt.fail.warn("Source file in build is not in filesystem:" + line + ". Check casing of filename.");
}
});
done();
}
function header(name, dependencies) {
var amdDependencies = dependencies.map(JSON.stringify).join(",");
var commonJsDependencies = dependencies.map(function (dep) {
return "require(" + JSON.stringify(dep) + ")";
}).join(",");
var header = "\n" +
"/*! Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information. */\n" +
"(function () {\n" +
"\n" +
" var globalObject = \n" +
" typeof window !== 'undefined' ? window :\n" +
" typeof self !== 'undefined' ? self :\n" +
" typeof global !== 'undefined' ? global :\n" +
" {};\n" +
" (function (factory) {\n" +
" if (typeof define === 'function' && define.amd) {\n" +
" // amd\n" +
" define([" + amdDependencies + "], factory);\n" +
" } else {\n" +
" globalObject.msWriteProfilerMark && msWriteProfilerMark('$(TARGET_DESTINATION) $(build.version).$(build.branch).$(build.date) " + name + ".js,StartTM');\n" +
" if (typeof module !== 'undefined') {\n" +
" // CommonJS\n" +
" factory(" + commonJsDependencies + ");\n" +
" } else {\n" +
// globalObject.WinJS is here for the have dependencies case where they have already defined WinJS
" // No module system\n" +
" factory(globalObject.WinJS);\n" +
" }\n" +
" globalObject.msWriteProfilerMark && msWriteProfilerMark('$(TARGET_DESTINATION) $(build.version).$(build.branch).$(build.date) " + name + ".js,StopTM');\n" +
" }\n" +
" }(function (WinJS) {\n" +
"\n";
// If this has dependencies it should assume they define WinJS.Utilities._require
if (dependencies.length) {
header += "\n" +
"var require = WinJS.Utilities._require;\n" +
"var define = WinJS.Utilities._define;\n" +
"\n";
}
return header;
}
function footer(name) {
return "\n" +
" require(['WinJS/Core/_WinJS', '" + name + "'], function (_WinJS) {\n" +
" // WinJS always publishes itself to global\n" +
" globalObject.WinJS = _WinJS;\n" +
" if (typeof module !== 'undefined') {\n" +
" // This is a CommonJS context so publish to exports\n" +
" module.exports = _WinJS;\n" +
" }\n" +
" });\n" +
" return globalObject.WinJS;\n" +
" }));\n" +
"}());\n" +
"\n";
}
function footerWithoutExport(name) {
return "\n" +
" require(['" + name + "'], function () {\n" +
" });\n" +
" }));\n" +
"}());\n" +
"\n";
}
function defaults(key, buildConfig) {
buildConfig = buildConfig || {};
var options = buildConfig.options = buildConfig.options || {};
options.baseUrl = config.compiledTsOutput;
options.useStrict = true;
options.optimize = "none"; // uglify2 is run seperately
options.stubModules = ["require-style", "require-json"];
options.done = options.done || done;
options.findNestedDependencies = true;
// If it doesn't have an exclude then we include the default AMD implementations
var primary = !options.exclude;
if (primary) {
options.deps = ["amd"];
}
var outputBase = config.desktopOutput + "js/";
options.paths = {
"less": "../../src/less",
"strings": "../../src/strings",
"require-json": "../../tasks/utilities/require-json",
"require-style": "../../tasks/utilities/require-style",
};
// this is to reconfigure onefile dependencies
if (config.isStorePackage) {
options.paths["WinJS/Utilities/_Telemetry"] = "./WinJS/Utilities/_TelemetryImpl";
}
// The modules build generates a require configuration with this
if (!options.dir) {
options.name = options.name || key;
var target = options.target || options.name;
var name = options.name;
options.out = outputBase + target + ".js";
options.wrap = options.wrap || {
start: header(target, options.exclude || []),
end: footer(name),
};
}
return buildConfig;
}
module.exports = {
defaults: defaults,
header: header,
footer: footer,
ui: defaults('ui', {
options: {
exclude: ['./base']
}
}),
intrinsics: defaults('intrinsics', {
options: {
cssOutputSuffix: "-intrinsics",
wrap: {
start: header("intrinsics", []),
end: footerWithoutExport("intrinsics")
}
}
}),
tv: defaults('tv', {
options: {
cssOutputSuffix: "-tv",
exclude: ['./base', './ui'],
wrap: {
start: header("tv", ['./base', './ui']),
end: footerWithoutExport("tv")
}
}
})
};
})();