Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack generating [hash].worker.js file when packaging custom library

I'm trying to create a library of reusable react components for our internal use.

Webpack is bundling the output - which should be a single file. But it's actually putting out the bundle.js that I'd expect plus a file called [some_hash].worker.js.

I'm not sure how to force webpack to include that "worker" file with the single bundle that I'm asking it for.

The webpack.config:

const path = require('path');
const webpack = require('webpack');

const DIST_PATH = path.resolve(__dirname, "../dist");
const SRC_PATH = path.resolve(__dirname, "../src");
const APP_PATH = path.resolve(__dirname, "../src/index.js");
const BASE_PATH = path.resolve(__dirname);
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = ({ appPath = APP_PATH, distPath = DIST_PATH }) => ({
  context: BASE_PATH,
  devServer: {
    contentBase: distPath,
    compress: true,
    port: 9000,
    historyApiFallback: true
  },
  resolve: {
    modules: ['node_modules', SRC_PATH],
    alias: {
      'react': path.resolve(__dirname, '../node_modules/react'),
      'react-dom': path.resolve(__dirname, '../node_modules/react-dom'),
    }
  },
  externals: {
      // Don't bundle react or react-dom
      react: {
        commonjs: "react",
        commonjs2: "react",
        amd: "React",
        root: "React"
      },
      "react-dom": {
        commonjs: "react-dom",
        commonjs2: "react-dom",
        amd: "ReactDOM",
        root: "ReactDOM"
      }
  },
  entry: {
    bundle: appPath,
  },
  output: {
    path: distPath,
    filename: 'index.js',
    publicPath: '/dist/',
    library: 'internal-components',
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  module: {
    rules: [
      {
        test: /\.jsx$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env', '@babel/preset-react'],
            plugins: [
              '@babel/plugin-proposal-object-rest-spread',
              '@babel/plugin-syntax-dynamic-import',
              [ '@babel/plugin-proposal-decorators', { 'legacy': true } ],
              [ '@babel/plugin-proposal-class-properties', { 'loose': true } ]
            ]
          }
        }
      },
      {
        test: /\.js$/,
        exclude: /(node_modules|build)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: [
              '@babel/plugin-proposal-object-rest-spread',
              '@babel/plugin-syntax-dynamic-import',
              ['@babel/plugin-proposal-decorators', {'legacy': true}],
              ["@babel/plugin-proposal-class-properties", {'loose': true}]
            ]
          }
        }
      },
      ...
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1,
    })
  ]
});
like image 559
Patrick Dench Avatar asked Apr 24 '20 19:04

Patrick Dench


1 Answers

You might try using the worker-loader plugin with inline to handle the bundling -

rules: [
      ...
      {
        test: /\.worker\.js$/,
        use: {
           loader: 'worker-loader',
           options: { inline: true, fallback: false }
        }
      }
    ]

That said, there are several open issues on Github surrounding using the worker as a blob, so YMMV

like image 91
nrako Avatar answered Sep 28 '22 08:09

nrako