首页 > 解决方案 > 从 webpack 输出到更多文件

问题描述

是否有一些选项可以分别捆绑反应核心和我的脚本?例如两个文件:

  1. bundle-ract.js(包括 react、react-dom、redux、immutable)
  2. bundle.js(我的应用程序和脚本)

标签: javascriptreactjswebpack

解决方案


当然,有这样的方法。您必须在 webpack 配置中定义多个条目。与此类似:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
    entry: {
        'SubItems': './src/modules/sub-items/sub.items.module.js',
        'UniversalDiscovery': './src/modules/universal-discovery/universal.discovery.module.js',
        'MultiFileUpload': './src/modules/multi-file-upload/multi.file.upload.module.js',
    },
    output: {
        filename: '[name].module.js',
        path: path.resolve(__dirname, 'Resources/public/js'),
        library: ['eZ', 'modules', '[name]'],
        libraryTarget: 'umd',
        libraryExport: 'default',
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            }
        ]
    },
    externals: {
        'react': {
            root: 'React',
            commonjs2: 'react',
            commonjs: 'react',
            amd: 'react'
        },
        'react-dom': {
            root: 'ReactDOM',
            commonjs2: 'react-dom',
            commonjs: 'react-dom',
            amd: 'react-dom'
        }
    },
    plugins: [
        new CleanWebpackPlugin(['Resources']),
        new UglifyJSPlugin({
            sourceMap: true,
            uglifyOptions: {
                ecma: 6
            }
        })
    ]
};

推荐阅读