简体   繁体   English

使用Webpack 2和extract-text-webpack-plugin

[英]Using Webpack 2 and extract-text-webpack-plugin

I'm using extract-text-webpack-plugin 2.0.0-rc.3 with Webpack 2.2.1 and am getting this error when running the build: 我在Webpack 2.2.1中使用extract-text-webpack-plugin 2.0.0-rc.3,并且在运行构建时遇到此错误:

/node_modules/extract-text-webpack-plugin/index.js:259
var shouldExtract = !!(options.allChunks || chunk.isInitial());
                                                  ^
TypeError: chunk.isInitial is not a function

Here is my webpack.config.js: 这是我的webpack.config.js:

'use strict';
const argv = require('yargs').argv;
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require('webpack');

module.exports = (function () {
  let config = {
    entry   : './' + process.env.npm_package_config_paths_source + '/main.js',
    output  : {
      filename : 'main.js'
    },
    watch   : !!argv.watch,
    vue     : {
      loaders : {
        js   : 'babel-loader',
        // Create separate CSS file to prevent unstyled content
        sass : ExtractTextPlugin.extract("css!sass?sourceMap") // requires `devtool: "#source-map"`
      }
    },
    module  : {
      rules : [
        {
          test    : /\.js$/,
          use     : 'babel-loader',
          exclude : '/node_modules/'
        },
        {
          test    : /\.vue$/,
          use     : 'vue-loader',
          options : {
            loaders : {
              'scss' : 'vue-style-loader!css-loader!sass-loader',
              'sass' : 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
            },
          }
        }
      ]
    },
    plugins : [
      new webpack.DefinePlugin({
        'process.env' : {
          npm_package_config_paths_source : '"' + process.env.npm_package_config_paths_source + '"'
        }
      }),
      new ExtractTextPlugin("style.css")
    ],
    resolve : {
      alias : {
        'vue$' : 'vue/dist/vue.common.js'
      }
    },
    babel   : {
      "presets"  : ["es2015", "stage-2"],
      "comments" : false,
      "env"      : {
        "test" : {
          "plugins" : ["istanbul"]
        }
      }
    },
    devtool : "#source-map" // #eval-source-map is faster but not compatible with ExtractTextPlugin
  };

  if (process.env.NODE_ENV === 'production') {
    config.plugins = [
      // short-circuits all Vue.js warning code
      new webpack.DefinePlugin({
        'process.env' : {
          NODE_ENV                        : '"production"',
          npm_package_config_paths_source : '"' + process.env.npm_package_config_paths_source + '"'
        }
      }),
      // minify with dead-code elimination
      new webpack.optimize.UglifyJsPlugin(),
      new ExtractTextPlugin("style.css")
    ];
    config.devtool = "#source-map";
  }

  return config;
})();

When I remove new ExtractTextPlugin("style.css") from the plugins array the build runs fine, but doesn't create style.css . 当我从plugins数组中删除new ExtractTextPlugin("style.css") ,构建运行良好,但未创建style.css

If I add the allChunks: true option the error becomes this: 如果我添加allChunks: true选项,则错误将变为:

/node_modules/webpack/lib/Chunk.js:80
return this.entrypoints.length > 0;
                       ^
TypeError: Cannot read property 'length' of undefined

In case you are writing style rules in .vue file or seprate .scss file, with below webpack configurations you can achieve what you're searching for: 如果你是在编写样式规则.vue文件或seprate .scss文件,下面的WebPack配置就可以实现你正在寻找:

webpack.confi.js: webpack.confi.js:

 var webpack = require('webpack'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); .... .... module.exports = { .... .... module: { rules: [ { test: /\\.vue$/, loader: 'vue-loader', options: { loaders: { 'scss': ExtractTextPlugin.extract('css-loader!sass-loader'), 'sass': ExtractTextPlugin.extract('css-loader!sass-loader?indentedSyntax') } } }, .... .... { test: /\\.scss$/, loader: ExtractTextPlugin.extract('css-loader!sass-loader') } .... .... ] // rules end }, // module end plugins: [ new ExtractTextPlugin('style.css') ], .... } 

Just make sure that you have installed these modules/loaders via NPM: 只需确保已通过NPM安装了以下模块/加载器:

  • css-loader CSS加载器
  • sass-loader 萨斯装载机
  • node-sass 节点ass
  • extract-text-webpack-plugin 提取文本Webpack插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM