0

我想创建将 js 和 css 保存在同一个文件夹中的 React 组件(对于每个组件):

  • 对话
    • 容器.jsx
    • 演示文稿.jsx
    • 样式.less

这是我的 webpack 配置的一部分:

module: {
    loaders: [
        {
            test: /\.(js|jsx)$/,
            loader: 'babel',
            exclude: /(node_modules)/,
            query: {
                presets: [
                    ['react'],
                    ['es2015'],
                    ['stage-0']
                ],
                plugins: [
                    ['transform-decorators-legacy'],
                    ['transform-runtime'],
                    ['transform-react-remove-prop-types'],
                    ['transform-react-constant-elements'],
                    ['transform-react-inline-elements']
                ]
            }
        },
        {
            test: /\.less$/,
            loader: ExtractTextPlugin.extract(['css-loader', 'less-loader']),
        }
    ],
},
plugins: [
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"production"'
    }),
    new webpack.ProvidePlugin({
        'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
    }),
    new webpack.optimize.UglifyJsPlugin({
        beautify: false,
        comments: false,
        sourceMap: true,
        compress: {
            sequences: true,
            booleans: true,
            loops: true,
            unused: true,
            warnings: false,
            drop_console: true,
            unsafe: true
        }
    }),
    new ExtractTextPlugin({
        filename: 'bundle.css',
        allChunks: true
    }),
    new OptimizeCssAssetsPlugin({
        assetNameRegExp: /\.css$/g,
        cssProcessor: require('cssnano'),
        cssProcessorOptions: {
            discardComments: {
                removeAll: true
            }
        },
        canPrint: true
    })
],

Webpack 创建bundle.js+bundle.css文件。我会说它按预期工作,除了片刻。我发现其中bundle.js包含来自bundle.css.

例如,如果大小bundle.css为 50KB,则bundle.js= 自己的大小 + 50KB。

我的 js 包中不需要重复的 CSS 代码。那么如何解决呢?

4

1 回答 1

0

当我切换laravel-elixir-webpack-official到本机webpack(webpack-stream)时已修复

于 2017-09-23T06:19:40.533 回答