Gathering detailed insights and metrics for workerize-loader-wp5
Gathering detailed insights and metrics for workerize-loader-wp5
Gathering detailed insights and metrics for workerize-loader-wp5
Gathering detailed insights and metrics for workerize-loader-wp5
Automatically move a module into a Web Worker (Webpack loader)
npm install workerize-loader-wp5
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
3 Stars
18 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Mar 09, 2023
Latest Version
2.0.2
Package Id
workerize-loader-wp5@2.0.2
Unpacked Size
47.32 kB
Size
13.13 kB
File Count
11
NPM Version
6.14.13
Node Version
14.17.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
A webpack loader that moves a module and its dependencies into a Web Worker, automatically reflecting exported functions as asynchronous proxies.
Worker
1npm install -D workerize-loader
worker.js:
1// block for `time` ms, then return the number of loops we could run in that time: 2export function expensive(time) { 3 let start = Date.now(), 4 count = 0; 5 while (Date.now() - start < time) count++; 6 return count; 7}
index.js: (our demo)
1import worker from "workerize-loader!./worker"; 2 3let instance = worker(); // `new` is optional 4 5instance.expensive(1000).then(count => { 6 console.log(`Ran ${count} loops`); 7});
Workerize options can either be defined in your Webpack configuration, or using Webpack's syntax for inline loader options.
inline
Type: Boolean
Default: false
You can also inline the worker as a BLOB with the inline
parameter
1// webpack.config.js 2{ 3 loader: 'workerize-loader', 4 options: { inline: true } 5}
or
1import worker from "workerize-loader?inline!./worker";
name
Type: String
Default: [hash]
Customize filename generation for worker bundles. Note that a .worker
suffix
will be injected automatically ({name}.worker.js
).
1// webpack.config.js 2{ 3 loader: 'workerize-loader', 4 options: { name: '[name].[contenthash:8]' } 5}
or
1import worker from "workerize-loader?name=[name].[contenthash:8]!./worker";
publicPath
Type: String
Default: based on output.publicPath
Workerize uses the configured value of output.publicPath
from Webpack unless
specified here. The value of publicPath
gets prepended to bundle filenames to
get their full URL. It can be a path, or a full URL with host.
1// webpack.config.js 2{ 3 loader: 'workerize-loader', 4 options: { publicPath: '/static/' } 5}
ready
Type: Boolean
Default: false
If true
, the imported "workerized" module will include a ready
property,
which is a Promise that resolves once the Worker has been loaded. Note: this is
unnecessary in most cases, since worker methods can be called prior to the
worker being loaded.
1// webpack.config.js 2{ 3 loader: 'workerize-loader', 4 options: { ready: true } 5}
or
1import worker from "workerize-loader?ready!./worker"; 2 3let instance = worker(); // `new` is optional 4await instance.ready;
import
Type: Boolean
Default: false
When enabled, generated output will create your Workers using a Data URL that
loads your code via importScripts
(eg:
new Worker('data:,importScripts("url")')
). This workaround enables
cross-origin script preloading, but Workers are created on an "opaque origin"
and cannot access resources on the origin of their host page without CORS
enabled. Only enable it if you understand this and specifically need the
workaround.
1// webpack.config.js 2{ 3 loader: 'workerize-loader', 4 options: { import: true } 5}
or
1import worker from "workerize-loader?import!./worker";
If you're using Babel in your build, make sure you disabled commonJS transform. Otherwize, workerize-loader won't be able to retrieve the list of exported function from your worker script :
1{ 2 test: /\.js$/, 3 loader: "babel-loader", 4 options: { 5 presets: [ 6 [ 7 "env", 8 { 9 modules: false, 10 }, 11 ], 12 ] 13 } 14}
Workerize-loader supports browsers that support Web Workers - that's IE10+. However, these browsers require a polyfill in order to use Promises, which Workerize-loader relies on. It is recommended that the polyfill be installed globally, since Webpack itself also needs Promises to load bundles.
The smallest implementation is the one we recommend installing:
npm i promise-polyfill
Then, in the module you are "workerizing", just add it as your first import:
1import "promise-polyfill/src/polyfill";
All worker code can now use Promises.
To test a module that is normally imported via workerize-loader
when not using
Webpack, import the module directly in your test:
1-const worker = require('workerize-loader!./worker.js'); 2+const worker = () => require('./worker.js'); 3 4const instance = worker();
In Jest, it's possible to define a custom transform
that emulates
workerize-loader on the main thread.
First, install babel-jest
and identity-object-proxy
:
1npm i -D babel-jest identity-object-proxy
Then, add these properties to the "transform"
and "moduleNameMapper"
sections of your Jest config (generally located in your package.json
):
1{ 2 "jest": { 3 "moduleNameMapper": { 4 "workerize-loader(\\?.*)?!(.*)": "identity-obj-proxy" 5 }, 6 "transform": { 7 "workerize-loader(\\?.*)?!(.*)": "<rootDir>/workerize-jest.js", 8 "^.+\\.[jt]sx?$": "babel-jest", 9 "^.+\\.[jt]s?$": "babel-jest" 10 } 11 } 12}
Finally, create the custom Jest transformer referenced above as a file
workerize-jest.js
in your project's root directory (where the package.json
is):
1module.exports = { 2 process(src, filename) { 3 return ` 4 async function asyncify() { return this.apply(null, arguments); } 5 module.exports = function() { 6 const w = require(${JSON.stringify(filename.replace(/^.+!/, ""))}); 7 const m = {}; 8 for (let i in w) m[i] = asyncify.bind(w[i]); 9 return m; 10 }; 11 `; 12 } 13};
Now your tests and any modules they import can use workerize-loader!
prefixes,
and the imports will be turned into async functions just like they are in
Workerize.
The inner workings here are heavily inspired by worker-loader. It's worth a read!
No vulnerabilities found.
No security vulnerabilities found.