@@ -3,7 +3,15 @@ const build = require('../../tasks/serialize-workers').build;
|
||||
function loader() {
|
||||
const callback = this.async();
|
||||
|
||||
build(this.resource, {minify: false})
|
||||
let minify = false;
|
||||
|
||||
// TODO: remove when https://github.com/webpack/webpack/issues/6496 is addressed
|
||||
const compilation = this._compilation;
|
||||
if (compilation) {
|
||||
minify = compilation.compiler.options.mode === 'production';
|
||||
}
|
||||
|
||||
build(this.resource, {minify})
|
||||
.then(chunk => {
|
||||
for (const filePath in chunk.modules) {
|
||||
this.addDependency(filePath);
|
||||
|
||||
@@ -29,26 +29,27 @@ async function build(input, {minify = true} = {}) {
|
||||
}
|
||||
]
|
||||
]
|
||||
}),
|
||||
{
|
||||
name: 'serialize worker and export create function',
|
||||
renderChunk(code) {
|
||||
return `
|
||||
const source = ${JSON.stringify(code)};
|
||||
const blob = new Blob([source], {type: 'application/javascript'});
|
||||
const url = URL.createObjectURL(blob);
|
||||
export function create() {
|
||||
return new Worker(url);
|
||||
}
|
||||
`;
|
||||
}
|
||||
}
|
||||
})
|
||||
];
|
||||
|
||||
if (minify) {
|
||||
plugins.push(terser());
|
||||
}
|
||||
|
||||
plugins.push({
|
||||
name: 'serialize worker and export create function',
|
||||
renderChunk(code) {
|
||||
return `
|
||||
const source = ${JSON.stringify(code)};
|
||||
const blob = new Blob([source], {type: 'application/javascript'});
|
||||
const url = URL.createObjectURL(blob);
|
||||
export function create() {
|
||||
return new Worker(url);
|
||||
}
|
||||
`;
|
||||
}
|
||||
});
|
||||
|
||||
const bundle = await rollup.rollup({input, plugins});
|
||||
const {output} = await bundle.generate({format: 'es'});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user