3

Im trying to setup worker-loader with vue-cli webpack installation that provides the following file structure for build / configuration:

-build
--vue-loader.conf.js
--webpack.base.conf.js
--other build files...
-config
--index.js
--dev.env.js
--other config files...

I then installed worker-loader with

npm install worker-loader --save-dev

So then I tried require my worker.js with

require('worker-loader!my-worker.js');

But its not loaded by babel which is used as default for vue-cli webpack version

So then I tried update webpack.base.conf.js with the following configuration:

module: {
rules: [
  {
    test: /\.(js|vue)$/,
    loader: 'eslint-loader',
    enforce: 'pre',
    include: [resolve('src'), resolve('test')],
    options: {
      formatter: require('eslint-friendly-formatter')
    }
  },
  {
    test: /\.vue$/,
    loader: 'vue-loader',
    options: vueLoaderConfig
  },
  {
    test: /\.js$/,
    loader: 'babel-loader',
    include: [resolve('src'), resolve('test')]
  },
  {
    test: /\worker\.js$/,
    loader: 'worker-loader',
    include: [resolve('src'), resolve('test')]
  },
  {
    test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('img/[name].[hash:7].[ext]')
    }
  },
  {
    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
    }
  }
]
}

But my worker is then only read by babel and imported as a regular js file, and the worker-loader doesnt seam to do anything.

So how to configure this correctly?

1 Answer 1

3

Woops, I did find my bug.

I tried to load worker-loader with:

import myWorker from 'worker-loader?./myworker'
let worker = new Worker(myWorker);

So the solution was simply to use:

import myWorker from 'worker-loader?./myworker'
let worker = new myWorker;

So now it works :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure it was 'worker-loader?./myworker' and not 'worker-loader!./myworker'? Could you post the structure of your worker file please?
It can be used directly whitout any conf after npm install worker-loader. Ex. import NoxferWorker from 'worker-loader?name=workers/[hash:7].worker.js!./noxfer.worker'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.