worker-loader
Advanced tools
Comparing version 0.8.0 to 0.8.1
29
index.js
'use strict'; | ||
const path = require('path'); | ||
const loaderUtils = require('loader-utils'); | ||
const validateOptions = require('schema-utils'); | ||
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin'); | ||
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin'); | ||
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); | ||
const loaderUtils = require('loader-utils'); | ||
const schema = require('./options.json'); | ||
const getWorker = (file, content, query) => { | ||
const getWorker = (file, content, options) => { | ||
const workerPublicPath = `__webpack_public_path__ + ${JSON.stringify(file)}`; | ||
if (query.inline) { | ||
if (options.inline) { | ||
const createInlineWorkerPath = JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`); | ||
const fallbackWorkerPath = query.fallback === false ? 'null' : workerPublicPath; | ||
const fallbackWorkerPath = options.fallback === false ? 'null' : workerPublicPath; | ||
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${fallbackWorkerPath})`; | ||
@@ -24,6 +27,9 @@ } | ||
const callback = this.async(); | ||
const query = loaderUtils.getOptions(this) || {}; | ||
const filename = loaderUtils.interpolateName(this, query.name || '[hash].worker.js', { | ||
context: query.context || this.options.context, | ||
regExp: query.regExp, | ||
const options = loaderUtils.getOptions(this) || {}; | ||
validateOptions(schema, options, 'Worker Loader'); | ||
const filename = loaderUtils.interpolateName(this, options.name || '[hash].worker.js', { | ||
context: options.context || this.options.context, | ||
regExp: options.regExp, | ||
}); | ||
@@ -42,2 +48,5 @@ const outputOptions = { | ||
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions)); | ||
if (this.target !== 'webworker' && this.target !== 'web') { | ||
workerCompiler.apply(new NodeTargetPlugin()); | ||
} | ||
workerCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`, 'main')); | ||
@@ -60,4 +69,4 @@ if (this.options && this.options.worker && this.options.worker.plugins) { | ||
const workerFile = entries[0].files[0]; | ||
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), query); | ||
if (query.fallback === false) { | ||
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), options); | ||
if (options.fallback === false) { | ||
delete this._compilation.assets[workerFile]; | ||
@@ -64,0 +73,0 @@ } |
{ | ||
"name": "worker-loader", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -34,3 +34,4 @@ "description": "worker loader module for webpack", | ||
"dependencies": { | ||
"loader-utils": "^1.0.2" | ||
"loader-utils": "^1.0.2", | ||
"schema-utils": "^0.3.0" | ||
}, | ||
@@ -37,0 +38,0 @@ "devDependencies": { |
@@ -7,9 +7,7 @@ [![npm][npm]][npm-url] | ||
<div align="center"> | ||
<!-- replace with accurate logo e.g from https://worldvectorlogo.com/ --> | ||
<a href="https://github.com/webpack/webpack"> | ||
<img width="200" height="200" vspace="" hspace="25" | ||
src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg"> | ||
<img width="200" height="200" hspace="25" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg"> | ||
</a> | ||
<h1>Worker Loader</h1> | ||
<p>Worker loader for Webpack.<p> | ||
<p>This loader registers the script as <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Worker</a>.<p> | ||
</div> | ||
@@ -23,6 +21,10 @@ | ||
<h2 align="center">Usage</h2> | ||
or | ||
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) | ||
```bash | ||
yarn add worker-loader --dev | ||
``` | ||
<h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2> | ||
Import the worker file: | ||
@@ -52,6 +54,7 @@ | ||
To set custom name use the `name` parameter: | ||
To set a custom name for the output script, use the `name` parameter. The name may contain the string `[hash]`, | ||
which will be replaced with a content-dependent hash for caching purposes. For example: | ||
``` javascript | ||
var MyWorker = require("worker-loader?name=[name].js!./myWorker.js"); | ||
var MyWorker = require("worker-loader?name=outputWorkerName.[hash].js!./myWorker.js"); | ||
``` | ||
@@ -69,2 +72,8 @@ | ||
_.has(o, 'foo') // true | ||
// Post data to parent thread | ||
self.postMessage({foo: 'foo'}) | ||
// Respond to message from parent thread | ||
self.addEventListener('message', function(event){ console.log(event); }); | ||
``` | ||
@@ -81,4 +90,28 @@ | ||
_.has(o, 'foo') // true | ||
// Post data to parent thread | ||
self.postMessage({foo: 'foo'}) | ||
// Respond to message from parent thread | ||
self.addEventListener('message', (event) => { console.log(event); }); | ||
``` | ||
### Integrating with TypeScript | ||
To integrate with TypeScript, you will need to define a custom module for the exports of your worker. You will also need to cast the new worker as the `Worker` type: | ||
**typings/custom.d.ts** | ||
``` | ||
declare module "worker-loader!*" { | ||
const content: any; | ||
export = content; | ||
} | ||
``` | ||
**App.ts** | ||
``` | ||
import * as MyWorker from "worker-loader!../../worker"; | ||
const worker: Worker = new MyWorker(); | ||
``` | ||
<h2 align="center">Maintainers</h2> | ||
@@ -90,29 +123,20 @@ | ||
<td align="center"> | ||
<img width="150" height="150" | ||
src="https://avatars3.githubusercontent.com/u/166921?v=3&s=150"> | ||
</br> | ||
<a href="https://github.com/bebraw">Juho Vepsäläinen</a> | ||
<a href="https://github.com/sokra"> | ||
<img width="150" height="150" src="https://github.com/sokra.png?s=150"> | ||
</a> | ||
<br /> | ||
<a href="https://github.com/sokra">Tobias Koppers</a> | ||
</td> | ||
<td align="center"> | ||
<img width="150" height="150" | ||
src="https://avatars2.githubusercontent.com/u/8420490?v=3&s=150"> | ||
</br> | ||
<a href="https://github.com/d3viant0ne"> | ||
<img width="150" height="150" src="https://avatars2.githubusercontent.com/u/8420490?v=3&s=150"> | ||
</a> | ||
<br /> | ||
<a href="https://github.com/d3viant0ne">Joshua Wiens</a> | ||
</td> | ||
<td align="center"> | ||
<img width="150" height="150" | ||
src="https://avatars3.githubusercontent.com/u/533616?v=3&s=150"> | ||
</br> | ||
<a href="https://github.com/SpaceK33z">Kees Kluskens</a> | ||
</td> | ||
<td align="center"> | ||
<img width="150" height="150" | ||
src="https://avatars3.githubusercontent.com/u/3408176?v=3&s=150"> | ||
</br> | ||
<a href="https://github.com/TheLarkInn">Sean Larkin</a> | ||
</td> | ||
<td align="center"> | ||
<img width="150" height="150" | ||
src="https://avatars3.githubusercontent.com/u/5635476?v=3&s=150"> | ||
</br> | ||
<a href="https://github.com/TrySound"> | ||
<img width="150" height="150" src="https://avatars3.githubusercontent.com/u/5635476?v=3&s=150"> | ||
</a> | ||
<br /> | ||
<a href="https://github.com/TrySound">Bogdan Chadkin</a> | ||
@@ -119,0 +143,0 @@ </td> |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const del = require('del'); | ||
@@ -17,3 +18,3 @@ const webpack = require('webpack'); | ||
output: { | ||
path: `expected/${name}`, | ||
path: path.join(__dirname, `expected/${name}`), | ||
filename: 'bundle.js', | ||
@@ -82,2 +83,4 @@ }, | ||
assert.equal(files.length, 2); | ||
assert.equal(files[0], 'expected/name-options/w1.js'); | ||
assert.equal(files[1], 'expected/name-options/w2.js'); | ||
assert.notEqual(readFile(files[0]).indexOf('// w1 via worker options'), -1); | ||
@@ -185,2 +188,46 @@ assert.notEqual(readFile(files[1]).indexOf('// w2 via worker options'), -1); | ||
); | ||
['web', 'webworker'].forEach((target) => { | ||
it(`should have missing dependencies (${target})`, () => | ||
makeBundle('nodejs-core-modules', { | ||
target, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /worker\.js$/, | ||
loader: '../index.js', | ||
options: { | ||
inline: true, | ||
fallback: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}).then((stats) => { | ||
assert.notEqual(stats.compilation.missingDependencies.length, 0); | ||
}) | ||
); | ||
}); | ||
['node', 'async-node', 'node-webkit', 'atom', 'electron', 'electron-main', 'electron-renderer'].forEach((target) => { | ||
it(`should not have missing dependencies (${target})`, () => | ||
makeBundle('nodejs-core-modules', { | ||
target, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /worker\.js$/, | ||
loader: '../index.js', | ||
options: { | ||
inline: true, | ||
fallback: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}).then((stats) => { | ||
assert.equal(stats.compilation.missingDependencies.length, 0); | ||
}) | ||
); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
107272
31
338
154
0
3
2
+ Addedschema-utils@^0.3.0
+ Addedajv@5.5.2(transitive)
+ Addedfast-deep-equal@1.1.0(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedjson-schema-traverse@0.3.1(transitive)
+ Addedschema-utils@0.3.0(transitive)