Webpack - MiniCssExtractPlugin doesn't extract file

时间:2018-05-18 17:13:39

标签: javascript webpack vue.js mini-css-extract-plugin

I've created webpack config to my VueJS project. I want to separate styles from javascript code. I've used mini-css-extract-plugin but finally I receive only bundle.js file. What's wrong with this config and where is a mistake? Is there any missing loader. My config is below:

import path from 'path';
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import VueLoaderPlugin from 'vue-loader/lib/plugin';
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
import CleanWebpackPlugin from 'clean-webpack-plugin';
const devMode = process.env.NODE_ENV !== 'production';

const prodPlugins = [
    new UglifyJsPlugin(),
    new MiniCssExtractPlugin({
        filename: "css/style.css"
    }),
    new OptimizeCssAssetsPlugin({
        assetNameRegExp: /\.optimize\.css$/g,
        cssProcessor: require('cssnano'),
        cssProcessorOptions: { discardComments: { removeAll: true } },
        canPrint: true
    })
];

const basicPlugins = [
    new CleanWebpackPlugin('dist'),
    new VueLoaderPlugin()
];

const config = {
    entry: {
        bundle: './src/main.js'
    },
    output: {
        filename: 'js/[name].js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
            {
                test: /\.css$/,
                use: [
                    devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
                    'css-loader'
                ]
            },
            { test: /\.(scss|sass)$/,
                use: [
                    devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
                    'vue-style-loader',
                    'css-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders: {
                        js: 'babel-loader',
                    }
                }
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {}
                    }
                ]
            }
        ]
    },
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.esm.js'
        },
        extensions: ['*', '.js', '.vue', '.json']
    },
    plugins: !process.env.NODE_ENV || !devMode ? basicPlugins : basicPlugins.concat(prodPlugins)
};

module.exports = config;

My file package.json

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "watch": "webpack --watch --mode development --hot",
    "dev": "webpack-dev-server --mode development --progress --hot --open",
    "build": "webpack --mode production --progress"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "autoprefixer-loader": "^3.2.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "^8.2.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0",
    "clean-webpack-plugin": "^0.1.19",
    "css-loader": "^0.28.11",
    "cssnano": "^3.10.0",
    "eslint": "^4.19.1",
    "eslint-watch": "^3.1.4",
    "mini-css-extract-plugin": "^0.4.0",
    "node-sass": "^4.9.0",
    "optimize-css-assets-webpack-plugin": "^4.0.1",
    "sass-loader": "^7.0.1",
    "sass-resources-loader": "^1.3.3",
    "style-loader": "^0.21.0",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "vue-loader": "^15.0.10",
    "vue-style-loader": "^4.1.0",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^4.8.3",
    "webpack-cli": "^2.1.3",
    "webpack-dev-server": "^3.1.4"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "material-design-icons": "^3.0.1",
    "vue": "^2.5.16",
    "vue-router": "^3.0.1",
    "vuetify": "^1.0.17"
  }
}

3 个答案:

答案 0 :(得分:1)

  

请注意,任何导入的文件都会受到树木抖动的影响。这意味着如果您在项目中使用类似css-loader的东西并导入CSS文件,则需要将其添加到副作用列表中,这样就不会无意中将其丢弃到生产模式中。   块引用

在你的package.json中添加:

"sideEffects": [
    '.scss'
 ]

答案 1 :(得分:0)

检查您是否设置了NODE_ENV环境变量。

在您的配置中,将css提取到单独的文件(MiniCssExtractPlugin)只会在构建生产时出现,换句话说,当NODE_ENV设置为'production'时。在构建开发时,正在使用样式加载器,它将在标记中注入css。

答案 2 :(得分:0)

在Webpack 4中,可以向加载器添加“ sideEffects:true”,以防止编译器删除MiniCssExtractPlugin.loader输出的CSS文件(请参见Webpack Tree Shaking Guide)。这适用于Angular + TypeScript(在tsconfig中使用“模块:”“ ES2015”)。我想它也会对您的设置有效。

{
    test: /\.scss$/,
    include: [
      helpers.root('src', 'assets')
    ],
    sideEffects: true,
    use: [
      MiniCssExtractPlugin.loader,
      {loader: 'css-loader'},
      {loader: 'resolve-url-loader'}, // Angular only
      {loader: 'sass-loader'},
    ]
},