Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

worker-loader

webpack-contrib2.3mMIT3.0.8

worker loader module for webpack

webpack

readme

npm node deps tests coverage chat size

worker-loader

worker loader module for webpack

Getting Started

To begin, you'll need to install worker-loader:

$ npm install worker-loader --save-dev

Inlined

App.js

import Worker from "worker-loader!./Worker.js";

Config

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.js$/,
        use: { loader: "worker-loader" },
      },
    ],
  },
};

App.js

import Worker from "./file.worker.js";

const worker = new Worker();

worker.postMessage({ a: 1 });
worker.onmessage = function (event) {};

worker.addEventListener("message", function (event) {});

And run webpack via your preferred method.

Options

Name Type Default Description
worker {String|Object} Worker Allows to set web worker constructor name and options
publicPath {String|Function} based on output.publicPath specifies the public URL address of the output files when referenced in a browser
filename {String|Function} based on output.filename The filename of entry chunks for web workers
chunkFilename {String} based on output.chunkFilename The filename of non-entry chunks for web workers
inline 'no-fallback'|'fallback' undefined Allow to inline the worker as a BLOB
esModule {Boolean} true Use ES modules syntax

worker

Type: String|Object Default: Worker

Set the worker type.

String

Allows to set web worker constructor name.

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          worker: "SharedWorker",
        },
      },
    ],
  },
};

Object

Allow to set web worker constructor name and options.

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          worker: {
            type: "SharedWorker",
            options: {
              type: "classic",
              credentials: "omit",
              name: "my-custom-worker-name",
            },
          },
        },
      },
    ],
  },
};

publicPath

Type: String|Function Default: based on output.publicPath

The publicPath specifies the public URL address of the output files when referenced in a browser. If not specified, the same public path used for other webpack assets is used.

String

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          publicPath: "/scripts/workers/",
        },
      },
    ],
  },
};

Function

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          publicPath: (pathData, assetInfo) => {
            return `/scripts/${pathData.hash}/workers/`;
          },
        },
      },
    ],
  },
};

filename

Type: String|Function Default: based on output.filename, adding worker suffix, for example - output.filename: '[name].js' value of this option will be [name].worker.js

The filename of entry chunks for web workers.

String

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          filename: "[name].[contenthash].worker.js",
        },
      },
    ],
  },
};

Function

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          filename: (pathData) => {
            if (
              /\.worker\.(c|m)?js$/i.test(pathData.chunk.entryModule.resource)
            ) {
              return "[name].custom.worker.js";
            }

            return "[name].js";
          },
        },
      },
    ],
  },
};

chunkFilename

Type: String Default: based on output.chunkFilename, adding worker suffix, for example - output.chunkFilename: '[id].js' value of this option will be [id].worker.js

The filename of non-entry chunks for web workers.

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          chunkFilename: "[id].[contenthash].worker.js",
        },
      },
    ],
  },
};

inline

Type: 'fallback' | 'no-fallback' Default: undefined

Allow to inline the worker as a BLOB.

Inline mode with the fallback value will create file for browsers without support web workers, to disable this behavior just use no-fallback value.

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          inline: "fallback",
        },
      },
    ],
  },
};

esModule

Type: Boolean Default: true

By default, worker-loader generates JS modules that use the ES modules syntax.

You can enable a CommonJS modules syntax using:

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          esModule: false,
        },
      },
    ],
  },
};

Examples

Basic

The worker file can import dependencies just like any other file:

index.js

import Worker from "./my.worker.js";

var worker = new Worker();

var result;

worker.onmessage = function (event) {
  if (!result) {
    result = document.createElement("div");
    result.setAttribute("id", "result");

    document.body.append(result);
  }

  result.innerText = JSON.stringify(event.data);
};

const button = document.getElementById("button");

button.addEventListener("click", function () {
  worker.postMessage({ postMessage: true });
});

my.worker.js

onmessage = function (event) {
  var workerResult = event.data;

  workerResult.onmessage = true;

  postMessage(workerResult);
};

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        loader: "worker-loader",
        options: {
          esModule: false,
        },
      },
    ],
  },
};

Integrating with ES6+ features

You can even use ES6+ features if you have the babel-loader configured.

index.js

import Worker from "./my.worker.js";

const worker = new Worker();

let result;

worker.onmessage = (event) => {
  if (!result) {
    result = document.createElement("div");
    result.setAttribute("id", "result");

    document.body.append(result);
  }

  result.innerText = JSON.stringify(event.data);
};

const button = document.getElementById("button");

button.addEventListener("click", () => {
  worker.postMessage({ postMessage: true });
});

my.worker.js

onmessage = function (event) {
  const workerResult = event.data;

  workerResult.onmessage = true;

  postMessage(workerResult);
};

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.worker\.(c|m)?js$/i,
        use: [
          {
            loader: "worker-loader",
          },
          {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env"],
            },
          },
        ],
      },
    ],
  },
};

Integrating with TypeScript

To integrate with TypeScript, you will need to define a custom module for the exports of your worker.

typings/worker-loader.d.ts

declare module "worker-loader!*" {
  // You need to change `Worker`, if you specified a different value for the `workerType` option
  class WebpackWorker extends Worker {
    constructor();
  }

  // Uncomment this if you set the `esModule` option to `false`
  // export = WebpackWorker;
  export default WebpackWorker;
}

my.worker.ts

const ctx: Worker = self as any;

// Post data to parent thread
ctx.postMessage({ foo: "foo" });

// Respond to message from parent thread
ctx.addEventListener("message", (event) => console.log(event));

index.ts

import Worker from "worker-loader!./Worker";

const worker = new Worker();

worker.postMessage({ a: 1 });
worker.onmessage = (event) => {};

worker.addEventListener("message", (event) => {});

Cross-Origin Policy

WebWorkers are restricted by a same-origin policy, so if your webpack assets are not being served from the same origin as your application, their download may be blocked by your browser. This scenario can commonly occur if you are hosting your assets under a CDN domain. Even downloads from the webpack-dev-server could be blocked.

There are two workarounds:

Firstly, you can inline the worker as a blob instead of downloading it as an external script via the inline parameter

App.js

import Worker from "./file.worker.js";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        loader: "worker-loader",
        options: { inline: "fallback" },
      },
    ],
  },
};

Secondly, you may override the base download URL for your worker script via the publicPath option

App.js

// This will cause the worker to be downloaded from `/workers/file.worker.js`
import Worker from "./file.worker.js";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        loader: "worker-loader",
        options: { publicPath: "/workers/" },
      },
    ],
  },
};

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.0.8 (2021-02-11)

Bug Fixes

  • make inline workers work from inside workers (#307) (2abd129)

3.0.7 (2020-12-23)

Bug Fixes

3.0.6 (2020-12-02)

Bug Fixes

3.0.5 (2020-10-16)

Bug Fixes

  • determine webpack peer dependency version (#296) (8c63449)

3.0.4 (2020-10-09)

Chore

  • update schema-utils

3.0.3 (2020-09-22)

Bug Fixes

3.0.2 (2020-08-22)

Bug Fixes

3.0.1 (2020-08-05)

Bug Fixes

  • compatibility with webpack@5 cache (#279) (ee519b1)
  • interpolation [name] for the filename option (#277) (5efa77a)

3.0.0 (2020-08-01)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 10.13
  • minimum supported webpack version is 4
  • the name option was renamed to the filename option
  • switch on ES module syntax by default, use the esModule option if you need backward compatibility with Common JS modules
  • the fallback option was removed in favor the inline option, the inline option accepts only fallback and no-fallback values
  • the publicPath option default value based on output.publicPath
  • the filename option default value based on output.filename

Features

  • added the worker option (replaces #178) (#247) (f03498d)
  • added the chunkFilename option, default value based on output.chunkFilename (905ed7b)
  • added the esModule option
  • allow to use any web worker constructor and options for constructor
  • the publicPath option can be Function
  • the filename (previously name) option can be Function

Bug Fixes

  • support WASM (152634c)
  • respect externals (#264) (1e761ed)
  • memory leak for inline web workers (#252) (f729e34)
  • source maps when inline using without fallback (#269) (5047abb)
  • the publicPath options works fine with async web workers chunks
  • compatibility with webpack@5 (#259) (e0d9887)
  • always use self as global object
  • compatibility with webpack-dev-server
  • increase performance

2.0.0 (2018-05-27)

Updates

  • refactor(index): remove Tapable.apply calls (#121)
  • docs: use ES6 import in TypeScript README example (#140) …
  • docs(README): add note about omitted hashes (options.name) (#131)
  • fix(package): homepage URL typo (#130)

Breaking Changes

Drops support for Webpack versions < 3.0.0

1.1.1 (2018-02-25)

Bug Fixes

  • index: add webpack >= v4.0.0 support (#128) (d1a7a94)

1.1.0 (2017-10-24)

Features

  • add publicPath support (options.publicPath) (#31) (96c6144)

1.0.0 (2017-09-25)

Features

  • add options validation (schema-utils) (#78) (5e2f5e6)
  • support loading node core modules (#76) (edcda35)

BREAKING CHANGES

  • loader-utils upgrade to > 1.0 is not backwards compatible with previous versions