webpack打包自定义库时生成[hash].worker.js文件

Pat*_*nch 8 reactjs webpack react-component

我正在尝试创建一个可重用的 React 组件库供我们内部使用。

Webpack 正在捆绑输出 - 这应该是单个文件。但它实际上发布了我期望的 bundle.js一个名为 [some_hash].worker.js 的文件。

我不确定如何强制 webpack 将“worker”文件包含在我要求的单个包中。

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,
    })
  ]
});
Run Code Online (Sandbox Code Playgroud)

nra*_*ako 1

您可以尝试使用内联的worker-loader插件来处理捆绑 -

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

Run Code Online (Sandbox Code Playgroud)

也就是说,Github 上有几个围绕使用工作人员作为 的未解决问题blob,所以 YMMV