feat: add worker support

This commit is contained in:
Igor Randjelovic
2021-03-07 19:33:52 +01:00
parent 02d2befc8a
commit d2abd58174
9 changed files with 273 additions and 0 deletions

View File

@@ -182,6 +182,14 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
},
});
config.module
.rule('workers')
.test(/\.(js|ts)$/)
.exclude.add(/node_modules/)
.end()
.use('nativescript-worker-loader')
.loader('nativescript-worker-loader')
// default PostCSS options to use
// projects can change settings
// via postcss.config.js

View File

@@ -0,0 +1,57 @@
const WorkerDependency = require('webpack/lib/dependencies/WorkerDependency');
const RuntimeGlobals = require("webpack/lib/RuntimeGlobals");
/**
* Patch WorkerDependency to change:
*
* new Worker(new URL(workerPath, baseUrl))
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* to.
*
* new Worker('~/' + workerPath)
*
* Note: we are changing source **outside** of the dependency range, and this may
* break when the dependency range changes, for example if this PR is merged:
* - https://github.com/webpack/webpack/pull/12750
*/
WorkerDependency.Template.prototype.apply = function apply(dependency, source, templateContext) {
const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
const dep = /** @type {WorkerDependency} */ (dependency);
const block = /** @type {AsyncDependenciesBlock} */ (moduleGraph.getParentBlock(
dependency
));
const entrypoint = /** @type {Entrypoint} */ (chunkGraph.getBlockChunkGroup(
block
));
const chunk = entrypoint.getEntrypointChunk();
// runtimeRequirements.add(RuntimeGlobals.publicPath);
// runtimeRequirements.add(RuntimeGlobals.baseURI);
runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
/**
* new URL(
* ^^^^^^^^ = 8 characters, we subtract it from the dep.range[0]
*/
source.replace(
dep.range[0] - 8,
dep.range[1],
`/* worker import */ /* patched by nativescript-worker-loader */ '~/' + ${
RuntimeGlobals.getChunkScriptFilename
}(${JSON.stringify(chunk.id)})`
);
}
const NEW_WORKER_WITH_STRING_RE = /new\s+Worker\((['"`].+['"`])\)/
/**
* Replaces
* new Worker('./somePath')
* with
* new Worker(new URL('./somePath', import.meta.url))
*/
export default function loader(content: string, map: any) {
const source = content.replace(NEW_WORKER_WITH_STRING_RE, 'new Worker(new URL($1, import.meta.url))')
this.callback(null, source, map)
}