From 533182dfe2558c283bfeb90320621b4220d2112b Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 2 Feb 2022 09:47:55 -0500 Subject: [PATCH 1/4] Bugfix: only process JS bundle assets This should fix #23, which was caused by OptimizePlugin attempting to modernize an `.ejs` template file. --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index fdcfabc..239ef68 100644 --- a/src/index.js +++ b/src/index.js @@ -136,7 +136,8 @@ export default class OptimizePlugin { let transformed; try { transformed = await Promise.all(files.map(file => { - if (!file.endsWith('js')) return undefined; + // ignore non-JS files + if (!file.match(/\.m?[jt]sx?$/i)) return undefined; const asset = compilation.assets[file]; let pending = processing.get(asset); if (pending) return pending; @@ -405,7 +406,7 @@ export default class OptimizePlugin { /** @todo Support other file extensions */ toLegacyFilename (file) { - let out = file.replace(/(\.m?js)$/g, '.legacy$1'); + let out = file.replace(/(\.m?[jt]sx?)$/g, '.legacy$1'); if (out === file) { // this will create `foo.js.legacy.js`, but it's the best we can hope for. out += '.legacy.js'; From dd2fca47bd853683bf60ba1b489e6d8457794c5d Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 2 Feb 2022 10:30:35 -0500 Subject: [PATCH 2/4] Add FAQ --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 11f1ea6..5026ed3 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,18 @@ handles graph creation and reduction, then passes its bundles to Babel for trans +## FAQ + +### What do I do with my current Babel configuration? + +In order to migrate to optimize-plugin, you'll need to move your babel configuration into a `.babelrc` or `babel.config.js` file and remove `babel-loader` from your Webpack configuration. Remember, optimize-plugin only uses your babel configuration when generating _modern_ bundles. Legacy bundles are automatically compiled to ES5 without looking at your Babel configuration, though you can customize their compilation by defining a [browserslist](https://github.com/browserslist/browserslist) field in your package.json. + +### Do I need to include any polyfills manually? + +In general, adopting optimize-plugin means removing all of your current polyfills, since the plugin automatically detects and polyfills JavaScript features for legacy bundles. The plugin does _not_ polyfill DOM features though, so be sure to keep including any DOM polyfills your application relies (`ParentNode.append()`, Module Workers, etc). + +Remember: the premise of this plugin is that you don't need to configure JS transpilation or polyfills - it's all done automatically based on usage. + ### License Apache-2.0 From f9126469a1b178c0ce84c1eaaee2b04520e4991f Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Fri, 4 Feb 2022 20:17:07 -0600 Subject: [PATCH 3/4] feat: Exclude assets option --- README.md | 1 + src/index.js | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5026ed3..4df7b93 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ plugins: [ | `modernize` | `boolean\|true` | Attempt to upgrade ES5 syntax to equivalent modern syntax. | | `verbose` | `boolean\|false` | Will log performance information and information about polyfills. | | `polyfillsFilename` | `string\|polyfills.legacy.js` | The name for the chunk containing polyfills for the legacy bundle. | +| `exclude` | `RegExp[]\|[]` | Asset patterns that should be excluded | ## How does this work? diff --git a/src/index.js b/src/index.js index 239ef68..7f71efd 100644 --- a/src/index.js +++ b/src/index.js @@ -68,7 +68,13 @@ const DEFAULT_OPTIONS = { /** * @default "polyfills.legacy.js" */ - polyfillsFilename: 'polyfills.legacy.js' + polyfillsFilename: 'polyfills.legacy.js', + + /** + * RegExp patterns of assets to exclude + * @default [] + */ + exclude: [] }; export default class OptimizePlugin { @@ -130,7 +136,15 @@ export default class OptimizePlugin { const processing = new WeakMap(); const chunkAssets = Array.from(compilation.additionalChunkAssets || []); - const files = [...chunkFiles, ...chunkAssets]; + const files = [...chunkFiles, ...chunkAssets] + .filter((asset) => { + for (const pattern of this.options.exclude) { + if (pattern.test(asset)) { + return false; + } + } + return true; + }); start('Optimize Assets'); let transformed; From 59fa98926f3315cdb52160b798a3bdec068beda9 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Sun, 5 Jun 2022 22:28:21 -0400 Subject: [PATCH 4/4] 1.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5e43b92..6b4616c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "optimize-plugin", - "version": "1.1.0", + "version": "1.2.0", "description": "Webpack plugin to optimize bundles.", "main": "dist/optimize-plugin.js", "repository": "developit/optimize-plugin",