worker-loader
Advanced tools
Comparing version 0.7.1 to 0.8.0
@@ -5,20 +5,23 @@ // http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string | ||
module.exports = function(content, url) { | ||
try { | ||
try { | ||
var blob; | ||
try { // BlobBuilder = Deprecated, but widely implemented | ||
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; | ||
blob = new BlobBuilder(); | ||
blob.append(content); | ||
blob = blob.getBlob(); | ||
} catch(e) { // The proposed API | ||
blob = new Blob([content]); | ||
} | ||
return new Worker(URL.createObjectURL(blob)); | ||
} catch(e) { | ||
return new Worker('data:application/javascript,' + encodeURIComponent(content)); | ||
} | ||
} catch(e) { | ||
return new Worker(url); | ||
} | ||
} | ||
try { | ||
try { | ||
var blob; | ||
try { // BlobBuilder = Deprecated, but widely implemented | ||
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; | ||
blob = new BlobBuilder(); | ||
blob.append(content); | ||
blob = blob.getBlob(); | ||
} catch(e) { // The proposed API | ||
blob = new Blob([content]); | ||
} | ||
return new Worker(URL.createObjectURL(blob)); | ||
} catch(e) { | ||
return new Worker('data:application/javascript,' + encodeURIComponent(content)); | ||
} | ||
} catch(e) { | ||
if (!url) { | ||
throw Error('Inline worker is not supported'); | ||
} | ||
return new Worker(url); | ||
} | ||
} |
118
index.js
@@ -1,56 +0,66 @@ | ||
var WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplatePlugin"); | ||
var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin"); | ||
var path = require("path"); | ||
'use strict'; | ||
var loaderUtils = require("loader-utils"); | ||
module.exports = function() {}; | ||
module.exports.pitch = function(request) { | ||
if(!this.webpack) throw new Error("Only usable with webpack"); | ||
this.cacheable(false); | ||
var callback = this.async(); | ||
var query = loaderUtils.parseQuery(this.query); | ||
var filename = loaderUtils.interpolateName(this, query.name || "[hash].worker.js", { | ||
context: query.context || this.options.context, | ||
regExp: query.regExp | ||
}); | ||
var outputOptions = { | ||
filename: filename, | ||
chunkFilename: "[id]." + filename, | ||
namedChunkFilename: null | ||
}; | ||
if(this.options && this.options.worker && this.options.worker.output) { | ||
for(var name in this.options.worker.output) { | ||
outputOptions[name] = this.options.worker.output[name]; | ||
} | ||
} | ||
var workerCompiler = this._compilation.createChildCompiler("worker", outputOptions); | ||
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions)); | ||
workerCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request, "main")); | ||
if(this.options && this.options.worker && this.options.worker.plugins) { | ||
this.options.worker.plugins.forEach(function(plugin) { | ||
workerCompiler.apply(plugin); | ||
}); | ||
} | ||
var subCache = "subcache " + __dirname + " " + request; | ||
workerCompiler.plugin("compilation", function(compilation) { | ||
if(compilation.cache) { | ||
if(!compilation.cache[subCache]) | ||
compilation.cache[subCache] = {}; | ||
compilation.cache = compilation.cache[subCache]; | ||
} | ||
}); | ||
workerCompiler.runAsChild(function(err, entries, compilation) { | ||
if(err) return callback(err); | ||
if (entries[0]) { | ||
var workerFile = entries[0].files[0]; | ||
var constructor = "new Worker(__webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; | ||
if(query.inline) { | ||
constructor = "require(" + JSON.stringify("!!" + path.join(__dirname, "createInlineWorker.js")) + ")(" + | ||
JSON.stringify(compilation.assets[workerFile].source()) + ", __webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; | ||
} | ||
return callback(null, "module.exports = function() {\n\treturn " + constructor + ";\n};"); | ||
} else { | ||
return callback(null, null); | ||
} | ||
}); | ||
const path = require('path'); | ||
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin'); | ||
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); | ||
const loaderUtils = require('loader-utils'); | ||
const getWorker = (file, content, query) => { | ||
const workerPublicPath = `__webpack_public_path__ + ${JSON.stringify(file)}`; | ||
if (query.inline) { | ||
const createInlineWorkerPath = JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`); | ||
const fallbackWorkerPath = query.fallback === false ? 'null' : workerPublicPath; | ||
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${fallbackWorkerPath})`; | ||
} | ||
return `new Worker(${workerPublicPath})`; | ||
}; | ||
module.exports = function workerLoader() {}; | ||
module.exports.pitch = function pitch(request) { | ||
if (!this.webpack) throw new Error('Only usable with webpack'); | ||
this.cacheable(false); | ||
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 outputOptions = { | ||
filename, | ||
chunkFilename: `[id].${filename}`, | ||
namedChunkFilename: null, | ||
}; | ||
if (this.options && this.options.worker && this.options.worker.output) { | ||
Object.keys(this.options.worker.output).forEach((name) => { | ||
outputOptions[name] = this.options.worker.output[name]; | ||
}); | ||
} | ||
const workerCompiler = this._compilation.createChildCompiler('worker', outputOptions); | ||
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions)); | ||
workerCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`, 'main')); | ||
if (this.options && this.options.worker && this.options.worker.plugins) { | ||
this.options.worker.plugins.forEach(plugin => workerCompiler.apply(plugin)); | ||
} | ||
const subCache = `subcache ${__dirname} ${request}`; | ||
workerCompiler.plugin('compilation', (compilation) => { | ||
if (compilation.cache) { | ||
if (!compilation.cache[subCache]) { | ||
compilation.cache[subCache] = {}; | ||
} | ||
compilation.cache = compilation.cache[subCache]; | ||
} | ||
}); | ||
workerCompiler.runAsChild((err, entries, compilation) => { | ||
if (err) return callback(err); | ||
if (entries[0]) { | ||
const workerFile = entries[0].files[0]; | ||
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), query); | ||
if (query.fallback === false) { | ||
delete this._compilation.assets[workerFile]; | ||
} | ||
return callback(null, `module.exports = function() {\n\treturn ${workerFactory};\n};`); | ||
} | ||
return callback(null, null); | ||
}); | ||
}; |
{ | ||
"name": "worker-loader", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"author": "Tobias Koppers @sokra", | ||
"description": "worker loader module for webpack", | ||
"scripts": { | ||
"test": "mocha", | ||
"posttest": "eslint ." | ||
}, | ||
"eslintConfig": { | ||
"extends": "webpack", | ||
"rules": { | ||
"linebreak-style": 0, | ||
"comma-dangle": [ | ||
"error", | ||
{ | ||
"arrays": "always-multiline", | ||
"objects": "always-multiline", | ||
"imports": "always-multiline", | ||
"exports": "always-multiline", | ||
"functions": "never" | ||
} | ||
], | ||
"no-underscore-dangle": 0, | ||
"no-param-reassign": 0, | ||
"prefer-destructuring": 0, | ||
"strict": 0 | ||
} | ||
}, | ||
"peerDependencies": { | ||
"webpack": ">=0.9 <2 || ^2.1.0-beta" | ||
"webpack": ">=0.9 <2 || ^2.1.0-beta || ^2.2.0" | ||
}, | ||
"dependencies": { | ||
"loader-utils": "0.2.x" | ||
"loader-utils": "^1.0.2" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/mit-license.php" | ||
} | ||
] | ||
"devDependencies": { | ||
"del": "^2.2.2", | ||
"eslint": "^3.16.0", | ||
"eslint-config-webpack": "^1.0.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"mocha": "^3.2.0", | ||
"webpack": "^2.2.1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/webpack-contrib/worker-loader.git" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -1,5 +0,24 @@ | ||
# worker loader for webpack | ||
[![npm][npm]][npm-url] | ||
[![deps][deps]][deps-url] | ||
[![test][test]][test-url] | ||
[![chat][chat]][chat-url] | ||
## Usage | ||
<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"> | ||
</a> | ||
<h1>Worker Loader</h1> | ||
<p>Worker loader for Webpack.<p> | ||
</div> | ||
<h2 align="center">Install</h2> | ||
```bash | ||
npm i -D worker-loader | ||
``` | ||
<h2 align="center">Usage</h2> | ||
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) | ||
@@ -11,3 +30,3 @@ | ||
// main.js | ||
var MyWorker = require("worker!./file.js"); | ||
var MyWorker = require("worker-loader!./file.js"); | ||
@@ -22,6 +41,19 @@ var worker = new MyWorker(); | ||
``` javascript | ||
var MyWorker = require("worker?inline!./file.js"); | ||
var MyWorker = require("worker-loader?inline!./myWorker.js"); | ||
``` | ||
Inline mode will also create chunks for browsers without supporting of inline workers, | ||
to disable this behavior just set `fallback` parameter as `false`: | ||
``` javascript | ||
var MyWorker = require("worker-loader?inline&fallback=false!./myWorker.js"); | ||
``` | ||
To set custom name use the `name` parameter: | ||
``` javascript | ||
var MyWorker = require("worker-loader?name=[name].js!./myWorker.js"); | ||
``` | ||
The worker file can import dependencies just like any other file: | ||
@@ -49,4 +81,52 @@ | ||
## License | ||
<h2 align="center">Maintainers</h2> | ||
MIT (http://www.opensource.org/licenses/mit-license.php) | ||
<table> | ||
<tbody> | ||
<tr> | ||
<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> | ||
</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">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">Bogdan Chadkin</a> | ||
</td> | ||
</tr> | ||
<tbody> | ||
</table> | ||
[npm]: https://img.shields.io/npm/v/worker-loader.svg | ||
[npm-url]: https://npmjs.com/package/worker-loader | ||
[deps]: https://david-dm.org/webpack-contrib/worker-loader.svg | ||
[deps-url]: https://david-dm.org/webpack-contrib/worker-loader | ||
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg | ||
[chat-url]: https://gitter.im/webpack/webpack | ||
[test]: http://img.shields.io/travis/webpack-contrib/worker-loader.svg | ||
[test-url]: https://travis-ci.org/webpack-contrib/worker-loader |
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
96577
28
269
130
6
2
+ Addedbig.js@5.2.2(transitive)
+ Addedemojis-list@3.0.0(transitive)
+ Addedjson5@1.0.2(transitive)
+ Addedloader-utils@1.4.2(transitive)
Updatedloader-utils@^1.0.2