我正在尝试创建一个可重用的 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, 
    }) 
  ] 
}); 

请您参考如下方法:

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

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

也就是说,Github 上有几个围绕将工作线程用作 blob 的未决问题,所以 YMMV


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!