2
votes

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

1 Answers

2
votes

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 :-)