|
|
|
@ -5,6 +5,7 @@
|
|
|
|
|
const path = require('path'); |
|
|
|
|
const webpack = require('webpack'); |
|
|
|
|
const { WorkerPluginName } = require('./constants'); |
|
|
|
|
const ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers'); |
|
|
|
|
|
|
|
|
|
class FuiWorkerPlugin { |
|
|
|
|
constructor(options = {}) { |
|
|
|
@ -39,6 +40,39 @@ class FuiWorkerPlugin {
|
|
|
|
|
// 动态添加 worker 的 worker-loader, 命名为 "fui-worker"
|
|
|
|
|
compiler.options.resolveLoader.alias['fui-worker'] = path.resolve(__dirname, './worker-loader.js'); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 将FuiWorkerPlugin的参数传递给fui-worker loader
|
|
|
|
|
compiler.hooks.compilation.tap(WorkerPluginName, compilation => { |
|
|
|
|
compilation.hooks.normalModuleLoader.tap(WorkerPluginName, (context, module) => { |
|
|
|
|
// 仅提供给fui-worker
|
|
|
|
|
const fuiLoader = module.loaders.find(loader => loader.loader.indexOf('fui-worker') !== -1); |
|
|
|
|
|
|
|
|
|
if (fuiLoader) { |
|
|
|
|
const resource = module.resource; |
|
|
|
|
|
|
|
|
|
if (!resource) return; |
|
|
|
|
|
|
|
|
|
// fui-worker通过options读取
|
|
|
|
|
context.options = context.options || {}; |
|
|
|
|
|
|
|
|
|
const index = resource.indexOf('?'); |
|
|
|
|
|
|
|
|
|
if (ModuleFilenameHelpers.matchObject( |
|
|
|
|
this.options, |
|
|
|
|
index < 0 ? resource : resource.substr(0, index) |
|
|
|
|
)) { |
|
|
|
|
for (const key of Object.keys(this.options)) { |
|
|
|
|
// 忽略关键属性
|
|
|
|
|
if (key === "include" || key === "exclude" || key === "test") { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
context.options[key] = this.options[key]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|