使用Webpack 4创作库时如何拆分块?

时间:2018-08-13 22:33:43

标签: reactjs npm webpack publish

我正在尝试将自定义react组件发布到私有存储库。

我使用react-loadable来按需加载子组件。在本地运行时,一切正常。必要时,index.js文件正确地向chunk.js文件发出请求。但是,当从另一个项目发布并使用该组件时,该组件在尝试请求子组件块时遇到404错误。 编写库时,我应该如何拆分块并按需加载它们?这是否有可能或者我是否以错误的方式思考?

这是我的webpack.config.js,以防它是简单的配置问题:

module.exports = () => ({
    context: __dirname,
    mode: 'development',
    entry: './src/index.jsx',
    output: {
        path: path.join(__dirname, './dist'),
        filename: "index.js",
        library: "reactcombobox",
        libraryTarget: "umd",
        publicPath: path.join(__dirname, './dist'),
        umdNamedDefine: true
    },
    plugins: [
        new webpack.LoaderOptionsPlugin({ options: { context: __dirname}}),
        new CleanWebpackPlugin([path.join(__dirname, './dist')], {verbose: true, allowExternal: true})
    ],
    module: {
        rules: [
            {
                test: /.jsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            ['@babel/preset-env', {
                                "targets": {
                                    "browsers": ["last 4 Chrome versions"]
                                }
                            }],
                            '@babel/preset-react'
                        ],
                        plugins: [
                            '@babel/plugin-transform-react-jsx',
                            '@babel/plugin-proposal-class-properties',
                            '@babel/plugin-syntax-dynamic-import'
                        ]
                    }
                }
            },
            {
                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',
        }
    },
    resolve: {
        extensions: ['*', '.js', '.jsx']
    }
});

0 个答案:

没有答案