1

在我开始集成purifycss-webpack之前,我的第一个“Hello Webpack”项目进展顺利。如果我手动删除我的 /dist 文件夹然后构建,则此配置有效。但是如果 /dist 文件夹存在并且CleanWebpackPlugin按照设计的方式将其删除,那么 PurifyCSS 会抛出这个错误:

clean-webpack-plugin: \dist 已被删除。
purifycss-webpack\dist\index.js:52 \ if (!_fs2.default.existsSync(p)) throw new Error('Path ' + p + ' 不存在。');

是否有另一种方法让 Webpack 仅在所有其他插件执行后才运行 PurifyCSS?我以为我是通过将它放在插件数组的最后来做到这一点的,但这对我来说感觉像是执行顺序问题。这是我的配置:

const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const globAll = require('glob-all');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const PurifyCSSPlugin = require('purifycss-webpack');
const webpack = require("webpack");

var extractPluginCSS = new ExtractTextPlugin({
  filename: "app.css"
});

module.exports = {
  entry: "./src/js/app.js",

  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "app.js"
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: ["env"]
            }
          }
        ]
      },

      {
        test: /app\.scss$/,
        use: extractPluginCSS.extract({
          use: [
            {
              loader: "css-loader",
              options: {}
            },
            {
              loader: "sass-loader",
              options: {}
            }
          ]
        })
      },

      {
        test: /\.html$/,
        use: ["html-loader"]
      },

      {
        test: /\.(jpg|png)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].[ext]",
              outputPath: "img/"
            }
          }
        ]
      }

    ]
  },

  plugins: [

    new CleanWebpackPlugin(['dist']),

    new HtmlWebpackPlugin({
      template: "./src/html/index.html"
    }),

    new webpack.optimize.UglifyJsPlugin(),

    extractPluginCSS,

    new PurifyCSSPlugin({
      // Give paths to parse for rules. These should be absolute!
      paths: globAll.sync([
        path.join(__dirname, '/dist/*.html')
      ]),
      // minimize: true,
      verbose: true
    })

  ]

};
4

0 回答 0