Skip to content

Commit adb980f

Browse files
author
Alexander Vakrilov
authored
Merge pull request #39 from edusperoni/worker-plugins
feat: Allow worker to use plugins
2 parents 1f0d68b + e86b82a commit adb980f

File tree

8 files changed

+53
-3
lines changed

8 files changed

+53
-3
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ module.exports = env => {
156156
}
157157
```
158158

159+
8. **[Angular projects only]** Update your webpack.config.js to inherit the current `ngCompilerPlugin` to allow the use of shared code.
160+
161+
``` javascript
162+
// webpack.config.js
163+
164+
module.exports = env => {
165+
// ...
166+
167+
const config = {
168+
//...
169+
plugins: [
170+
new NativeScriptWorkerPlugin({
171+
plugins: [ngCompilerPlugin]
172+
}),
173+
// ...
174+
]
175+
}
176+
}
177+
```
178+
159179
## Web workers with/without webpack
160180

161181
Please note that the way to spawn a Worker with webpack differs from the way described in the WWW Web Workers' specification (also followed by NativeScript).

demo-angular/app/app.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from "@angular/core";
22

33
import { WorkerService } from "./worker.service";
4+
import { sharedFunction } from "./shared";
45

56
@Component({ template: `` })
67
export class AppComponent implements OnInit {
@@ -10,6 +11,7 @@ export class AppComponent implements OnInit {
1011
constructor(private workerService: WorkerService) { }
1112

1213
ngOnInit() {
14+
sharedFunction("app");
1315
this.tsWorker = this.workerService.initTsWorker();
1416
this.jsWorker = this.workerService.initJsWorker();
1517

demo-angular/app/shared.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
export function sharedFunction(location: string) {
3+
console.log("This code is shared between worker/app and ran from: " + location);
4+
}

demo-angular/app/workers/typescript.worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import "globals";
2+
import { sharedFunction } from "../shared";
23

34
const context: Worker = self as any;
45

56
context.onmessage = msg => {
7+
sharedFunction("worker");
68
setTimeout(() => {
79
console.log("Inside TS worker...");
810
console.log(msg);

demo-angular/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ module.exports = env => {
267267
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
268268
// For instructions on how to set up workers with webpack
269269
// check out https://github.com/nativescript/worker-loader
270-
new NativeScriptWorkerPlugin(),
270+
new NativeScriptWorkerPlugin({
271+
plugins: [ngCompilerPlugin]
272+
}),
271273
ngCompilerPlugin,
272274
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
273275
new nsWebpack.WatchStateLoggerPlugin(),

src/NativeScriptWorkerPlugin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const { resolve } = require("path");
22
const { RawSource } = require("webpack-sources");
3+
const NATIVESCRIPT_WORKER_PLUGIN_SYMBOL = require("./symbol");
34

45
exports.NativeScriptWorkerPlugin = (function () {
5-
function NativeScriptWorkerPlugin() {
6+
function NativeScriptWorkerPlugin(options) {
7+
this.options = options || {};
8+
this[NATIVESCRIPT_WORKER_PLUGIN_SYMBOL] = true;
69
}
710

811
NativeScriptWorkerPlugin.prototype.apply = function (compiler) {

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplate
66
const NodeTargetPlugin = require("webpack/lib/node/NodeTargetPlugin");
77
const SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
88
const optionsSchema = require("./options.json");
9+
const NATIVESCRIPT_WORKER_PLUGIN_SYMBOL = require("./symbol");
910

1011
const validateSchema = (schema, options, pluginName) => {
1112
if (options.inline) {
@@ -44,6 +45,8 @@ module.exports.pitch = function pitch(request) {
4445
this.cacheable(false);
4546
const callback = this.async();
4647
const options = loaderUtils.getOptions(this) || {};
48+
const compilerOptions = this._compiler.options || {};
49+
const pluginOptions = compilerOptions.plugins.find(p => p[NATIVESCRIPT_WORKER_PLUGIN_SYMBOL]).options;
4750

4851
// handle calls to itself to avoid an infinite loop
4952
if (requests.indexOf(request) === -1) {
@@ -68,7 +71,18 @@ module.exports.pitch = function pitch(request) {
6871
namedChunkFilename: null,
6972
};
7073

71-
const workerCompiler = this._compilation.createChildCompiler("worker", outputOptions);
74+
const plugins = (pluginOptions.plugins || []).map(plugin => {
75+
if (typeof plugin !== 'string') {
76+
return plugin;
77+
}
78+
const found = compilerOptions.plugins.find(p => p.constructor.name === plugin);
79+
if (!found) {
80+
console.warn(`Warning (worker-plugin): Plugin "${plugin}" is not found.`);
81+
}
82+
return found;
83+
});
84+
85+
const workerCompiler = this._compilation.createChildCompiler("worker", outputOptions, plugins);
7286
new WebWorkerTemplatePlugin(outputOptions).apply(workerCompiler);
7387
if (this.target !== "webworker" && this.target !== "web") {
7488
new NodeTargetPlugin().apply(workerCompiler);

src/symbol.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
const NATIVESCRIPT_WORKER_PLUGIN_SYMBOL = Symbol('NATIVESCRIPT_WORKER_PLUGIN_SYMBOL');
3+
module.exports = NATIVESCRIPT_WORKER_PLUGIN_SYMBOL;

0 commit comments

Comments
 (0)