-
Notifications
You must be signed in to change notification settings - Fork 249
/
javascript.js
423 lines (391 loc) · 14.4 KB
/
javascript.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const CacheManager = require('../helpers/CacheManager');
module.exports = function(grunt) {
const convertSlashes = /\\/g;
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
const path = require('path');
const fs = require('fs-extra');
const rollup = require('rollup');
const { babel } = require('@rollup/plugin-babel');
const terser = require('@rollup/plugin-terser');
const { deflate, unzip, constants } = require('zlib');
const cwd = process.cwd().replace(convertSlashes, '/') + '/';
const isDisableCache = process.argv.includes('--disable-cache');
let cache;
const extensions = ['.js', '.jsx'];
const cacheManager = new CacheManager(grunt, grunt.config('cacheAge'));
const restoreCache = async (cachePath, basePath) => {
if (isDisableCache || cache || !fs.existsSync(cachePath)) return;
await new Promise((resolve, reject) => {
const buffer = fs.readFileSync(cachePath);
unzip(buffer, (err, buffer) => {
if (err) {
console.error('An error occurred restoring rollup cache:', err);
process.exitCode = 1;
reject(err);
return;
}
let str = buffer.toString();
// Restore cache to current basePath
str = str.replace(/%%basePath%%/g, basePath);
cache = JSON.parse(str);
resolve();
});
});
};
const checkCache = function(invalidate) {
if (!cache) return;
const idHash = {};
const missing = {};
cache.modules.forEach(mod => {
const moduleId = mod.id;
const isRollupHelper = (moduleId[0] === '\u0000');
if (isRollupHelper) {
// Ignore as injected rollup module
return null;
}
if (!fs.existsSync(moduleId)) {
grunt.log.error(`Cache missing file: ${moduleId.replace(cwd, '')}`);
missing[moduleId] = true;
return false;
}
if (invalidate && invalidate.includes(moduleId)) {
grunt.log.ok(`Cache skipping file: ${moduleId.replace(cwd, '')}`);
return false;
}
idHash[moduleId] = mod;
return true;
});
if (Object.keys(missing).length) {
cache = null;
return;
}
cache.modules = Object.values(idHash);
};
const saveCache = async (cachePath, basePath, bundleCache) => {
if (!isDisableCache) {
cache = bundleCache;
}
await new Promise((resolve, reject) => {
let str = JSON.stringify(bundleCache);
// Make cache location agnostic by stripping current basePath
str = str.replace(new RegExp(escapeRegExp(basePath), 'g'), '%%basePath%%');
deflate(str, { level: constants.Z_BEST_SPEED }, (err, buffer) => {
if (err) {
console.error('An error occurred saving rollup cache:', err);
process.exitCode = 1;
reject(err);
return;
}
fs.writeFileSync(cachePath, buffer);
resolve();
});
});
};
const logPrettyError = (err, cachePath, basePath) => {
let hasOutput = false;
if (err.loc) {
// Code error
switch (err.plugin) {
case 'babel':
err.frame = err.message.substr(err.message.indexOf('\n') + 1);
err.message = err.message.substr(0, err.message.indexOf('\n')).slice(2).replace(/^([^:]*): /, '');
break;
default:
hasOutput = true;
console.error(err.toString());
}
if (!hasOutput) {
console.error(err.toString());
console.error(`Line: ${err.loc.line}, Col: ${err.loc.column}, File: ${err.id.replace(cwd, '')}`);
console.error(err.frame);
hasOutput = true;
}
}
if (!hasOutput) {
cache = null;
saveCache(cachePath, basePath, cache);
console.error(err.toString());
}
};
grunt.registerMultiTask('javascript', 'Compile JavaScript files', async function() {
const Helpers = require('../helpers')(grunt);
const buildConfig = Helpers.generateConfigData();
const isStrictMode = buildConfig.strictMode;
grunt.log.ok(`Strict mode (config.json:build.strictMode): ${isStrictMode}`);
grunt.log.ok(`Cache disabled (--disable-cache): ${isDisableCache}`);
const done = this.async();
const options = this.options({});
const isSourceMapped = Boolean(options.generateSourceMaps);
const basePath = path.resolve(cwd + '/' + options.baseUrl).replace(convertSlashes, '/') + '/';
const cachePath = cacheManager.cachePath(cwd, options.out);
if (!isDisableCache) {
grunt.log.ok(`Cache path: ${cachePath}`);
}
try {
await restoreCache(cachePath, basePath);
await cacheManager.clean();
const pluginsPath = path.resolve(cwd, options.pluginsPath).replace(convertSlashes, '/');
// Make src/plugins.js to attach the plugins dynamically
if (!fs.existsSync(pluginsPath)) {
fs.writeFileSync(pluginsPath, '');
}
// Collect all plugin entry points for injection
const pluginPaths = [];
for (let i = 0, l = options.plugins.length; i < l; i++) {
const src = options.plugins[i];
grunt.file.expand({
filter: options.pluginsFilter
}, src).forEach(function(bowerJSONPath) {
if (bowerJSONPath === undefined) return;
const pluginPath = path.dirname(bowerJSONPath);
const bowerJSON = grunt.file.readJSON(bowerJSONPath);
const requireJSRootPath = pluginPath.substr(options.baseUrl.length);
const requireJSMainPath = path.join(requireJSRootPath, bowerJSON.main);
const ext = path.extname(requireJSMainPath);
const requireJSMainPathNoExt = requireJSMainPath.slice(0, -ext.length).replace(convertSlashes, '/');
pluginPaths.push(requireJSMainPathNoExt);
});
}
// Collect react templates
const reactTemplatePaths = [];
options.reactTemplates.forEach(pattern => {
grunt.file.expand({
filter: options.pluginsFilter
}, pattern).forEach(templatePath => reactTemplatePaths.push(templatePath.replace(convertSlashes, '/')));
});
// Process remapping and external model configurations
const mapParts = Object.keys(options.map);
const externalParts = Object.keys(options.external);
const externalMap = options.externalMap;
const findFile = function(filename) {
filename = filename.replace(convertSlashes, '/');
const hasValidExtension = extensions.includes(path.parse(filename).ext);
if (!hasValidExtension) {
const ext = extensions.find(ext => fs.existsSync(filename + ext)) || '';
filename += ext;
}
return filename;
};
const umdImports = options.umdImports.map(filename => findFile(path.resolve(basePath, filename)));
// Rework modules names and inject plugins
const adaptLoader = function() {
return {
name: 'adaptLoader',
resolveId(moduleId, parentId) {
const isRollupHelper = (moduleId[0] === '\u0000');
if (isRollupHelper) {
// Ignore as injected rollup module
return null;
}
const mapPart = mapParts.find(part => moduleId.startsWith(part));
if (mapPart) {
// Remap module, usually coreJS/adapt to core/js/adapt etc
moduleId = moduleId.replace(mapPart, options.map[mapPart]);
}
// Remap ../libraries/ or core/js/libraries/ to libraries/
moduleId = Object.entries(externalMap).reduce((moduleId, [ match, replaceWith ]) => moduleId.replace((new RegExp(match, 'g')), replaceWith), moduleId);
const isRelative = (moduleId[0] === '.');
if (isRelative) {
if (!parentId) {
// Rework app.js path so that it can be made basePath agnostic in the cache
const filename = findFile(path.resolve(moduleId));
return {
id: filename,
external: false
};
}
// Rework relative paths into absolute ones
const filename = findFile(path.resolve(parentId + '/../' + moduleId));
return {
id: filename,
external: false
};
}
const externalPart = externalParts.find(part => moduleId.startsWith(part));
const isEmpty = (options.external[externalPart] === 'empty:');
if (isEmpty) {
// External module as is defined as 'empty:', libraries/ bower handlebars etc
return {
id: moduleId,
external: true
};
}
const isES6Import = !fs.existsSync(moduleId);
if (isES6Import) {
// ES6 imports start inside ./src so need correcting
const filename = findFile(path.resolve(cwd, options.baseUrl, moduleId));
return {
id: filename,
external: false
};
}
// Normalize all other absolute paths as conflicting slashes will load twice
const filename = findFile(path.resolve(cwd, moduleId));
return {
id: filename,
external: false
};
}
};
};
const adaptInjectPlugins = function() {
return {
name: 'adaptInjectPlugins',
transform(code, moduleId) {
const isRollupHelper = (moduleId[0] === '\u0000');
if (isRollupHelper) {
return null;
}
const isPlugins = (moduleId.includes('/' + options.pluginsModule + '.js'));
if (!isPlugins) {
return null;
}
// Dynamically construct plugins.js with plugin dependencies
code = `define([${pluginPaths.map(filename => {
return `"${filename}"`;
}).concat(reactTemplatePaths.map(filename => {
return `"${filename}"`;
})).join(',')}], function() {});`;
return code;
}
};
};
const targets = buildConfig.targets || null;
const browserList = fs.existsSync('.browserslistrc')
? fs.readFileSync('.browserslistrc')
.toString()
.replace(/#+[^\n]+\n/gm, '')
.replace(/\r/g, '')
.split('\n')
.filter(Boolean)
.join(', ')
: [
'last 2 chrome versions, last 2 firefox versions',
'last 2 safari versions',
'last 2 edge versions',
'last 2 ios_saf versions',
'last 2 and_chr versions',
'firefox esr'
].join(', ');
grunt.log.ok(`Targets: ${targets || browserList}`);
const inputOptions = {
input: './' + options.baseUrl + options.name,
shimMissingExports: true,
plugins: [
adaptLoader({}),
adaptInjectPlugins({}),
babel({
babelHelpers: 'bundled',
extensions,
minified: false,
compact: false,
comments: false,
exclude: [
'**/node_modules/**'
],
presets: [
[
'@babel/preset-react',
{
runtime: 'classic'
}
],
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: 3,
exclude: [
// Breaks lockingModel.js, set function vs set variable
'transform-function-name'
],
targets
}
]
],
plugins: [
[
'transform-amd-to-es6',
{
umdToAMDModules: true,
amdToES6Modules: true,
amdDefineES6Modules: true,
ignoreNestedRequires: true,
defineFunctionName: '__AMD',
defineModuleId: (moduleId) => moduleId.replace(convertSlashes, '/').replace(basePath, '').replace('.js', ''),
excludes: [
'**/templates/**/*.jsx'
]
}
],
[
'transform-react-templates',
{
includes: [
'**/templates/**/*.jsx'
],
importRegisterFunctionFromModule: path.resolve(basePath, 'core/js/reactHelpers.js').replace(convertSlashes, '/'),
registerFunctionName: 'register',
registerTemplateName: (moduleId) => path.parse(moduleId).name
}
]
]
})
]
};
const umdImport = () => {
return umdImports.map(filename => {
let code = fs.readFileSync(filename).toString();
code = code
.replace('require("object-assign")', 'Object.assign')
.replace('define.amd', 'define.noop');
return code;
}).join('\n');
};
const outputOptions = {
file: options.out,
format: 'amd',
plugins: [
!isSourceMapped && terser({
mangle: false,
compress: false
})
].filter(Boolean),
intro: umdImport(),
footer: `// Allow ES export default to be exported as amd modules
window.__AMD = function(id, value) {
window.define(id, function() { return value; }); // define for external use
window.require([id]); // force module to load
return value; // return for export
};`,
sourcemap: isSourceMapped,
sourcemapPathTransform: (relativeSourcePath) => {
// Rework sourcemap paths to overlay at the appropriate root
return relativeSourcePath.replace(convertSlashes, '/').replace('../' + options.baseUrl, '');
},
amd: {
define: 'require'
},
strict: isStrictMode
};
checkCache([pluginsPath]);
inputOptions.cache = cache;
const bundle = await rollup.rollup(inputOptions);
await saveCache(cachePath, basePath, bundle.cache);
await bundle.write(outputOptions);
// Remove old sourcemap if no longer required
if (!isSourceMapped && fs.existsSync(options.out + '.map')) {
fs.unlinkSync(options.out + '.map');
}
done();
} catch (err) {
try {
await fs.unlink(cachePath);
} catch (err) {}
logPrettyError(err, cachePath, basePath);
done(false);
}
});
};