12

我强调试图让 Uglify 与我的项目一起工作,以前我使用过 Uglify 并且没有出现问题,但现在我认为这与 SASS 相关。

ERROR in ./~/css-loader!./~/sass-loader!./app/scss/global-header.scss
    Module build failed: TypeError: Cannot read property '4' of undefined
        at reduce (/Users/contractor/centralefcom-global-components/node_modules/postcss-reduce-transforms/dist/index.js:121:23)
        at walk (/Users/contractor/centralefcom-global-components/node_modules/postcss-value-parser/lib/walk.js:7:22)
        at ValueParser.walk (/Users/contractor/centralefcom-global-components/node_modules/postcss-value-parser/lib/index.js:18:5)
        at /Users/contractor/centralefcom-global-components/node_modules/postcss-reduce-transforms/dist/index.js:131:75
        at /Users/contractor/centralefcom-global-components/node_modules/postcss/lib/container.js:92:28
        at /Users/contractor/centralefcom-global-components/node_modules/postcss/lib/container.js:73:26
...
...

这个错误重复了很多次,每个多捆绑一个。

这是我的 webpack 配置。

var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

module.exports = {
  context: __dirname,
  resolve: {
    modulesDirectories: ['node_modules', 'bower_components']
  },
  entry: {
    'all': [
      './app/global-all.js',
      './app/scss/global-all.scss'
    ],
    'footer': [
      './app/global-footer.js',
      './app/scss/global-footer.scss'
    ],
    'header': [
      './app/global-header.js',
      './app/scss/global-header.scss'
    ],
    'footer.nodeps': [
      './app/global-footer-nodeps.js',
      './app/scss/global-footer-nodeps.scss'
    ],
    'header.nodeps': [
      './app/global-header-nodeps.js',
      './app/scss/global-header-nodeps.scss'
    ],
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'js/global.[name].js',
  },
  module: {
    loaders: [
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style','css!sass!')
      },
      {
        test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'file?name=/assets/[name].[ext]'
      },
      {
        test: /\.(jpg|jpeg|png)$/,
        loader: 'file?name=/assets/[name].[ext]'
      }
    ],
  },
  plugins: [
    new webpack.EnvironmentPlugin([
      'NODE_ENV'
    ]),
    new ExtractTextPlugin('css/global.[name].css'),
    new HtmlWebpackPlugin({
      template: 'underscore-template-loader!./app/views/secondary.html',
      inject: false,
      filename: 'secondary.html'
    }),
        new HtmlWebpackPlugin({
      template: 'underscore-template-loader!./app/views/secondary-transparent.html',
      inject: false,
      filename: 'secondary-transparent.html'
    }),
    new HtmlWebpackPlugin({
      template: 'underscore-template-loader!./app/views/secondary-academy.html',
      inject: false,
      filename: 'secondary-academy.html'
    }),
    new HtmlWebpackPlugin({
      template: 'underscore-template-loader!./app/views/hero-stage.html',
      inject: false,
      filename: 'hero-stage.html'
    }),

    // Only minify in build, check npm tasks
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: false,
      mangle: false
    }),
    ]
};

如果我评论 uglifyJsPlugin 行,则没有错误。但是我必须进行缩小并且无法使其运行,我尝试了 mangler: false and nothing,不起作用。

4

2 回答 2

10

您正在尝试通过 UglifyJs 插件传递 CSS 文件,该插件不受支持。

要仅丑化您的 JS 源,您可以根据文件扩展名进行过滤。

下面的示例将仅 Uglify 具有.js.jsx扩展名的文件:

new webpack.optimize.UglifyJsPlugin({
  sourceMap: false,
  mangle: false,
  test: /\.(js|jsx)$/
})
于 2016-04-06T13:29:43.090 回答
1

您将 SCSS 文件作为入口点的一部分。在 webpack 中,更常见的做法是在 JS 文件中要求您需要的文件(SCSS 明智)。

// Here, the ExtractTextPlugin will use sass-loader to extract and compile styles
require('styles.scss');

// The rest of your code goes here
// For instance, app.js
function someCode() {
    document.body.style.backgroundColor = 'blue';
}

然后,您设置 sass-loader 来检测和处理您已经使用以下加载程序完成的那些 require 语句。

{
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract('style','css!sass!')
}
于 2016-04-08T03:29:22.927 回答