9

我已经为我的 VueJS 项目创建了 webpack 配置。我想将样式与 javascript 代码分开。我使用了 mini-css-extract-plugin 但最后我只收到了 bundle.js 文件。这个配置有什么问题,哪里有错误?有没有丢失的装载机。我的配置如下:

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;

我的文件 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"
  }
}

4

3 回答 3

4

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

在你的 package.json 中,添加:

"sideEffects": [
    '.scss'
 ]
于 2018-05-21T06:17:09.900 回答
1

在 Webpack 4 中,您可以在加载器中添加“sideEffects: true”,以防止编译器丢弃 MiniCssExtractPlugin.loader 输出的 CSS 文件(参见Webpack Tree Shaking Guide)。这适用于 Angular + TypeScript(在 tsconfig 中使用“module:”“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'},
    ]
},
于 2018-09-24T18:50:34.530 回答
0

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

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

于 2018-05-22T10:27:25.183 回答