Browse Source

KERNEL-12093 feat: 拓展下fui-worker支持配置

es6
Austin.Duan 2 years ago
parent
commit
afe02f585e
  1. 4
      bin/cli/worker/template/main_thread.helper.t
  2. 15
      plugins/webpack-fui-worker-plugin/worker-loader.js

4
bin/cli/worker/template/main_thread.helper.t

@ -1,6 +1,7 @@
import { ${WorkerName}MainThreadWorker } from './main_thread/${workerName}_main_thread';
// 不需要一起打包的话则不需要引入这行
import { workerUrl } from 'fui-worker!./worker_thread/${workerName}_worker_thread';
// 根据实际需求传入inline属性true -> blob urlfalse -> servicePath
import { workerUrl } from 'fui-worker?inline=true!./worker_thread/${workerName}_worker_thread';
export class ${WorkerName}WorkerHelper {
private worker: ${WorkerName}MainThreadWorker;
@ -23,6 +24,7 @@ export class ${WorkerName}WorkerHelper {
/**
* 格式化worker url比如补充一些环境信息到参数里
* 通过 #hash 传入blob url或 ?params 传入servicePath
* @param url worker url
*/
private urlFormatter(url: string) {

15
plugins/webpack-fui-worker-plugin/worker-loader.js

@ -43,7 +43,7 @@ function loader() {
const entryFileName = `${FileNamePrefix}index`;
// 获取传递给 loader 的 options
loaderUtils.getOptions(this) || {};
const options = loaderUtils.getOptions(this) || {};
// 创建 childCompiler, 用于实现 worker 构建为独立 js 资源
const childCompiler = this._compilation.createChildCompiler(WorkerLoaderName, {
@ -92,15 +92,24 @@ function loader() {
return callback(error);
}
return callback(
// 支持blob url形式
return options.inline
? callback(
null,
// 插入代码的转译和压缩由主构建配置的 babel/ts loader 处理, 不需要 worker-worker 来处理
// 添加 @ts-nocheck 避免 ts-check 报错
`// @ts-nocheck
const blob = new Blob([${JSON.stringify(compilation.assets[entry].source())}]);
export const workerUrl = window.URL.createObjectURL(blob);
`
)
: callback(
null,
`// @ts-nocheck
const servicePath = __webpack_public_path__ + ${JSON.stringify(entry)};
export const workerUrl = servicePath;
`
);
)
});
return;

Loading…
Cancel
Save