浏览代码

Prevent bundling worker fallbacks into compiled workers

Tim Kendrick 7 年之前
父节点
当前提交
2535fd5a79
共有 2 个文件被更改,包括 36 次插入13 次删除
  1. 22 7
      webpack/index.js
  2. 14 6
      webpack/plugins/AddWorkerEntryPointPlugin.js

+ 22 - 7
webpack/index.js

@@ -125,8 +125,16 @@ function createPlugins(webpack, workers, outputPath) {
     ...Object.keys(IGNORED_IMPORTS).map((id) =>
       createIgnoreImportsPlugin(webpack, id, IGNORED_IMPORTS[id])
     ),
-    ...workers.map(({ id, entry, output }) =>
-      createEntryPointPlugin(webpack, id, resolveMonacoPath(entry), path.join(outputPath, output))
+    ...uniqBy(workers, ({ id }) => id).map(({ id, entry, output }) =>
+      new AddWorkerEntryPointPlugin(webpack, {
+        id,
+        entry: resolveMonacoPath(entry),
+        filename: path.join(outputPath, output),
+        plugins: [
+          createContextPlugin(webpack, WORKER_LOADER_PATH, {}),
+          new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
+        ],
+      })
     ),
     ...(workerFallbacks ? [createContextPlugin(webpack, WORKER_LOADER_PATH, workerFallbacks)] : []),
   ];
@@ -137,7 +145,7 @@ function createContextPlugin(webpack, filePath, contextPaths) {
     new RegExp(`^${path.dirname(filePath)}$`),
     '',
     contextPaths
-  )
+  );
 }
 
 function createIgnoreImportsPlugin(webpack, targetPath, ignoredModules) {
@@ -147,10 +155,6 @@ function createIgnoreImportsPlugin(webpack, targetPath, ignoredModules) {
   );
 }
 
-function createEntryPointPlugin(webpack, id, entry, output) {
-  return new AddWorkerEntryPointPlugin(webpack, { id, entry, output });
-}
-
 function flatMap(items, iteratee) {
   return items.map(iteratee).reduce((acc, item) => [...acc, ...item], []);
 }
@@ -170,4 +174,15 @@ function mapValues(object, iteratee) {
   );
 }
 
+function uniqBy(items, iteratee) {
+  const keys = {};
+  return items.reduce((acc, item) => {
+    const key = iteratee(item);
+    if (key in keys) { return acc; }
+    keys[key] = true;
+    acc.push(item);
+    return acc;
+  }, []);
+}
+
 module.exports = MonacoWebpackPlugin;

+ 14 - 6
webpack/plugins/AddWorkerEntryPointPlugin.js

@@ -1,15 +1,22 @@
 class AddWorkerEntryPointPlugin {
-  constructor(webpack, { id, entry, output }) {
+  constructor(webpack, {
+    id,
+    entry,
+    filename,
+    chunkFilename = undefined,
+    plugins = undefined,
+  }) {
     this.webpack = webpack;
-    this.options = { id, entry, output };
+    this.options = { id, entry, filename, chunkFilename, plugins };
   }
 
   apply(compiler) {
     const webpack = this.webpack;
-    const { id, entry, output } = this.options;
-    compiler.plugin('make', (compilation, callback) => {
+    const { id, entry, filename, chunkFilename, plugins } = this.options;
+    compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', (compilation, callback) => {
       const outputOptions = {
-        filename: output,
+        filename,
+        chunkFilename,
         publicPath: compilation.outputOptions.publicPath,
         // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
         globalObject: 'this',
@@ -17,8 +24,9 @@ class AddWorkerEntryPointPlugin {
       const childCompiler = compilation.createChildCompiler(id, outputOptions, [
         new webpack.webworker.WebWorkerTemplatePlugin(),
         new webpack.LoaderTargetPlugin('webworker'),
-        new webpack.SingleEntryPlugin(this.context, entry, 'main'),
+        new webpack.SingleEntryPlugin(compiler.context, entry, 'main'),
       ]);
+      plugins.forEach((plugin) => plugin.apply(childCompiler));
       childCompiler.runAsChild(callback);
     });
   }