Andrei Vorsa
Andrei Vorsa

Reputation: 171

How to remove css from js bundle?

I want to create React components keeping js and css in the same folder (for each component):

Here is a part of my webpack config:

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 creates bundle.js + bundle.css files. I would say it works as expected except for one moment. I found that bundle.js contains css from bundle.css.

For example, if size of bundle.css is 50KB, then bundle.js = own size + 50KB.

I don't need that duplicated css code in my js bundle. So how to fix it?

Upvotes: 0

Views: 979

Answers (1)

Andrei Vorsa
Andrei Vorsa

Reputation: 171

Fixed when I switched from laravel-elixir-webpack-official to native webpack (webpack-stream)

Upvotes: 0

Related Questions