Skip to content

Commit 9a5cd1e

Browse files
authored
WRN-18582: Migrate to webpack5 (#90)
* WRN-18582: webpack5 initial commit Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * WRN-18582: Migrated isomorphic and snapshot plugin Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * update verbose plugin Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * fix travis errors Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * update dependencies Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * fix Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * fix based on review Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected]) * update shrinkwrap Enact-DCO-1.0-Signed-off-by: Mikyung Kim ([email protected])
1 parent 10e795c commit 9a5cd1e

File tree

12 files changed

+1573
-5623
lines changed

12 files changed

+1573
-5623
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# unreleased
2+
3+
* Updated all dependencies, with webpack peer dependency restricted to >=5.0.0.
4+
* Updated all webpack plugins to use WeakMap since `compilation.hooks` became frozen.
5+
* `IlibPlugin`:
6+
* Added `ILIB_ADDITIONAL_RESOURCES_PATH` to defined constants if provided.
7+
* Added `publicPath` option to specify webpack public path.
8+
19
# 4.1.4 (February 18, 2022)
210

311
* `option-parser`: Added `additionalModulePaths` to specify paths to check when resolving modules.

mixins/isomorphic.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939

4040
// Include plugin to prerender the html into the index.html
4141
const htmlPluginInstance = helper.getPluginByName(config, 'HtmlWebpackPlugin');
42+
const webOSMetaPluginInstance = helper.getPluginByName(config, 'WebOSMetaPlugin');
4243
config.plugins.push(
4344
new PrerenderPlugin({
4445
server: reactDOMServer,
@@ -49,7 +50,8 @@ module.exports = {
4950
fontGenerator: app.fontGenerator,
5051
externalStartup: app.externalStartup,
5152
mapfile: opts.mapfile,
52-
htmlPlugin: htmlPluginInstance && htmlPluginInstance.constructor
53+
htmlPlugin: htmlPluginInstance && htmlPluginInstance.constructor,
54+
webOSMetaPlugin: webOSMetaPluginInstance && webOSMetaPluginInstance.constructor
5355
})
5456
);
5557

@@ -63,7 +65,8 @@ module.exports = {
6365
// Include plugin to attempt generation of v8 snapshot binary if V8_MKSNAPSHOT env var is set
6466
config.plugins.push(
6567
new SnapshotPlugin({
66-
target: 'main.js'
68+
target: 'main.js',
69+
webOSMetaPlugin: webOSMetaPluginInstance && webOSMetaPluginInstance.constructor
6770
})
6871
);
6972
}

mixins/verbose.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
const helper = require('../config-helper');
12
const VerboseLogPlugin = require('../plugins/VerboseLogPlugin');
23

34
module.exports = {
45
apply: function (config) {
5-
return config.plugins.push(new VerboseLogPlugin());
6+
const prerenderInstance = helper.getPluginByName(config, 'PrerenderPlugin');
7+
const snapshotPluginInstance = helper.getPluginByName(config, 'SnapshotPlugin');
8+
9+
return config.plugins.push(
10+
new VerboseLogPlugin({
11+
prerenderPlugin: prerenderInstance && prerenderInstance.constructor,
12+
snapshotPlugin: snapshotPluginInstance && snapshotPluginInstance.constructor
13+
})
14+
);
615
}
716
};

npm-shrinkwrap.json

Lines changed: 1260 additions & 5499 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

option-parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Object.defineProperty(config, 'environment', {
199199
} else if (targets.every(b => !b.startsWith('not') && b.includes('node'))) {
200200
return 'node';
201201
} else {
202-
return 'web';
202+
return 'browserslist';
203203
}
204204
} else {
205205
return 'web';

package.json

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"repository": "https://github.com/enyojs/enact-dev-utils",
88
"license": "Apache-2.0",
99
"engines": {
10-
"node": ">=6.4.0"
10+
"node": ">=10.13.0"
1111
},
1212
"publishConfig": {
1313
"access": "public"
@@ -31,31 +31,24 @@
3131
"plugins/SnapshotPlugin/mock-window.js"
3232
],
3333
"dependencies": {
34-
"browserslist": "^4.12.0",
35-
"chalk": "^4.1.0",
34+
"browserslist": "^4.20.2",
35+
"chalk": "^4.1.2",
3636
"console.mute": "^0.3.0",
37-
"core-js": "^3.6.5",
38-
"electron-to-chromium": "^1.3.474",
39-
"enzyme-adapter-utils": "^1.13.0",
40-
"fast-glob": "^3.2.0",
41-
"fbjs": "^0.8.18",
42-
"find-cache-dir": "^3.3.1",
43-
"graceful-fs": "^4.2.4",
44-
"import-fresh": "^3.2.1",
37+
"core-js": "^3.21.1",
38+
"fast-glob": "^3.2.11",
39+
"fbjs": "^3.0.4",
40+
"find-cache-dir": "^3.3.2",
41+
"graceful-fs": "^4.2.9",
42+
"import-fresh": "^3.3.0",
4543
"mock-require": "^3.0.3",
46-
"react": "^17.0.1",
47-
"react-dom": "^17.0.1",
48-
"react-is": "^17.0.1",
49-
"react-reconciler": "^0.26.1",
50-
"react-test-renderer": "^17.0.1",
51-
"resolve": "^1.17.0",
52-
"tapable": "^1.1.3",
53-
"webpack-bundle-analyzer": "^3.8.0",
54-
"webpack-sources": "^1.4.3"
44+
"resolve": "^1.22.0",
45+
"tapable": "^2.2.1",
46+
"webpack-bundle-analyzer": "^4.5.0",
47+
"webpack-sources": "^3.2.3"
5548
},
5649
"peerDependencies": {
57-
"html-webpack-plugin": ">=4.0.0",
58-
"webpack": ">=4.0.0"
50+
"html-webpack-plugin": ">=5.0.0",
51+
"webpack": ">=5.0.0"
5952
},
6053
"peerDependenciesMeta": {
6154
"html-webpack-plugin": {
@@ -67,18 +60,19 @@
6760
},
6861
"devDependencies": {
6962
"babel-eslint": "^10.1.0",
70-
"eslint": "^7.11.0",
63+
"eslint": "^7.30.0",
7164
"eslint-config-enact": "^3.1.3",
7265
"eslint-config-prettier": "^6.11.0",
7366
"eslint-plugin-babel": "^5.3.1",
7467
"eslint-plugin-enact": "^0.2.3",
75-
"eslint-plugin-import": "^2.22.1",
76-
"eslint-plugin-jsx-a11y": "^6.3.1",
77-
"eslint-plugin-prettier": "^3.1.4",
78-
"eslint-plugin-react": "^7.21.5",
79-
"eslint-plugin-react-hooks": "^4.2.0",
80-
"html-webpack-plugin": "^4.5.0",
81-
"prettier": "^2.0.5",
82-
"webpack": "^4.44.2"
68+
"eslint-plugin-import": "^2.25.4",
69+
"eslint-plugin-jsx-a11y": "^6.5.1",
70+
"eslint-plugin-prettier": "^4.0.0",
71+
"eslint-plugin-react": "^7.29.4",
72+
"eslint-plugin-react-hooks": "^4.3.0",
73+
"html-webpack-plugin": "^5.3.2",
74+
"prettier": "^2.6.0",
75+
"react": "^17.0.2",
76+
"webpack": "^5.64.4"
8377
}
8478
}

plugins/ILibPlugin/index.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ function packageSearch(dir, pkg) {
2828
}
2929

3030
// Determine if it's a NodeJS output filesystem or if it's a foreign/virtual one.
31+
// The internal webpack5 implementation of outputFileSystem is graceful-fs.
3132
function isNodeOutputFS(compiler) {
32-
return (
33-
compiler.outputFileSystem &&
34-
compiler.outputFileSystem.constructor &&
35-
compiler.outputFileSystem.constructor.name &&
36-
compiler.outputFileSystem.constructor.name === 'NodeOutputFileSystem'
37-
);
33+
return compiler.outputFileSystem && JSON.stringify(compiler.outputFileSystem) === JSON.stringify(fs);
3834
}
3935

4036
// Normalize a filepath to be relative to the webpack context, using forward-slashes, and
@@ -57,7 +53,7 @@ function bundleConst(name) {
5753
);
5854
}
5955

60-
function resolveBundle(dir, context, symlinks, relative) {
56+
function resolveBundle({dir, context, symlinks, relative, publicPath}) {
6157
const bundle = {resolved: dir, path: dir, emit: true};
6258
if (path.isAbsolute(bundle.path)) {
6359
bundle.emit = false;
@@ -73,7 +69,7 @@ function resolveBundle(dir, context, symlinks, relative) {
7369
if (relative) {
7470
bundle.resolved = JSON.stringify(transformPath(context, bundle.path));
7571
} else {
76-
bundle.resolved = '__webpack_require__.p + ' + JSON.stringify(transformPath(context, bundle.path));
72+
bundle.resolved = publicPath + JSON.stringify(transformPath(context, bundle.path));
7773
}
7874
}
7975
return bundle;
@@ -180,6 +176,26 @@ function emitAsset(compilation, name, data) {
180176
};
181177
}
182178

179+
const iLibPluginHooksMap = new WeakMap();
180+
181+
function getILibPluginHooks(compilation) {
182+
let hooks = iLibPluginHooksMap.get(compilation);
183+
184+
// Setup the hooks only once
185+
if (hooks === undefined) {
186+
hooks = createILibPluginHooks();
187+
iLibPluginHooksMap.set(compilation, hooks);
188+
}
189+
190+
return hooks;
191+
}
192+
193+
function createILibPluginHooks() {
194+
return {
195+
ilibManifestList: new SyncWaterfallHook(['manifests'])
196+
};
197+
}
198+
183199
class ILibPlugin {
184200
constructor(options = {}) {
185201
this.options = options;
@@ -239,24 +255,38 @@ class ILibPlugin {
239255
}
240256

241257
// Resolve an accurate basepath for iLib.
242-
const ilib = resolveBundle(opts.ilib, opts.context, opts.symlinks);
243-
const resources = resolveBundle(
244-
opts.resources || 'resources',
245-
opts.context,
246-
opts.symlinks,
247-
Boolean(opts.relativeResources)
248-
);
258+
const ilib = resolveBundle({
259+
dir: opts.ilib,
260+
context: opts.context,
261+
symlinks: opts.symlinks,
262+
publicPath: opts.publicPath
263+
});
264+
const resources = resolveBundle({
265+
dir: opts.resources || 'resources',
266+
context: opts.context,
267+
symlinks: opts.symlinks,
268+
relative: Boolean(opts.relativeResources),
269+
publicPath: opts.publicPath
270+
});
249271
const definedConstants = {
250272
ILIB_BASE_PATH: ilib.resolved,
251273
ILIB_RESOURCES_PATH: resources.resolved,
252274
ILIB_CACHE_ID: '__webpack_require__.ilib_cache_id',
253275
// when `emit` is false and `ilib` is not absolute, can delare no assets
254276
ILIB_NO_ASSETS: JSON.stringify(!opts.emit && !path.isAbsolute(opts.ilib))
255277
};
278+
if (opts.ilibAdditionalResourcesPath) {
279+
definedConstants.ILIB_ADDITIONAL_RESOURCES_PATH = '"' + opts.ilibAdditionalResourcesPath + '"';
280+
}
256281
definedConstants[bundleConst(app.name)] = definedConstants.ILIB_RESOURCES_PATH;
257282
for (const name in opts.bundles) {
258283
if (opts.bundles[name]) {
259-
const bundle = resolveBundle(opts.bundles[name], opts.context, opts.symlinks);
284+
const bundle = resolveBundle({
285+
dir: opts.bundles[name],
286+
context: opts.context,
287+
symlinks: opts.symlinks,
288+
publicPath: opts.publicPath
289+
});
260290
const bundleManifest = path.join(bundle.path, 'ilibmanifest.json');
261291
definedConstants[bundleConst(name)] = bundle.resolved;
262292
if (opts.emit && bundle.emit && fs.existsSync(bundleManifest)) {
@@ -273,16 +303,13 @@ class ILibPlugin {
273303
new ContextReplacementPlugin(/ilib/, /^$/).apply(compiler);
274304

275305
compiler.hooks.compilation.tap('ILibPlugin', compilation => {
276-
// Define compilation hooks
277-
compilation.hooks.ilibManifestList = new SyncWaterfallHook(['manifests']);
278-
279306
// Add a unique ID value to the webpack require-function, so that the value is correctly updated,
280307
// even when hot-reloading and serving.
281308
const main = compilation.mainTemplate;
282309
main.hooks.requireExtensions.tap('ILibPlugin', source => {
283310
const buf = [source];
284311
buf.push('');
285-
buf.push(main.requireFn + '.ilib_cache_id = ' + JSON.stringify('' + new Date().getTime()) + ';');
312+
buf.push('__webpack_require__.ilib_cache_id = ' + JSON.stringify('' + new Date().getTime()) + ';');
286313
return Template.asString(buf);
287314
});
288315
});
@@ -332,11 +359,15 @@ class ILibPlugin {
332359
)
333360
);
334361
}
335-
manifests = compilation.hooks.ilibManifestList.call(manifests);
362+
manifests = getILibPluginHooks(compilation).ilibManifestList.call(manifests);
336363
handleBundles(compilation, manifests, opts, callback);
337364
});
338365
}
339366
}
340367
}
341368

369+
// A static helper to get the hooks for this plugin
370+
// Usage: ILibPlugin.getHooks(compilation).HOOK_NAME.tapAsync('YourPluginName', () => { ... });
371+
ILibPlugin.getHooks = getILibPluginHooks;
372+
342373
module.exports = ILibPlugin;

0 commit comments

Comments
 (0)