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

问题描述 投票:3回答:1

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

Webpack将输出捆绑在一起--这 是一个单独的文件。 但实际上它放出的bundle.js是我所期望的。加上 一个叫[some_hash].worker.js的文件。

我不知道如何强制webpack将那个 "worker "文件包含在我所要求的单个bundle中。

webpack.config.中的 "worker "文件。

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,
    })
  ]
});
reactjs webpack react-component
1个回答
1
投票

你可以尝试使用 worker-loader插件 与内联处理捆绑 -

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

话说回来,在Github上有几个公开的问题,围绕着将worker作为一个 blob所以,YMMV


0
投票

其实如果你使用的是webpack 3及以上版本,捆绑包的分块会自动帮你完成。在SplitChunks Plugin文档中 此处 它实际上是说这是如何表现的。

所以正因为如此,你可能需要扫描你的代码并检查这个条件。另外,很想知道你是否异步导入了某个模块?这可能会给webpack一个信号,让它变成一个单独的模块。

© www.soinside.com 2019 - 2024. All rights reserved.