如何从js bundle中删除css?

时间:2017-09-22 21:29:38

标签: css reactjs webpack less extracttextwebpackplugin

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

  • 对话
    • Container.jsx
    • Presentation.jsx
    • styles.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的css。

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

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

1 个答案:

答案 0 :(得分:0)

当我从laravel-elixir-webpack-official切换到本地webpack(webpack-stream)时修复了