简体   繁体   English

Striptags NPM导致Webpack / Babel / React ES6编译错误

[英]Striptags NPM causes Webpack / Babel / React ES6 compile error

I am running into a bad compile error running Babel / Webpack on a React / Node app. 我遇到了在React / Node应用程序上运行Babel / Webpack的错误编译错误。

The Webpack appears to compile, but UglifyJS is throwing eval errors the second it finishes - like Babel is not compiling the React / ES6 code into ES5 at all. Webpack似乎是编译的,但是UglifyJS在它完成的第二个错误中抛出了eval错误 - 就像Babel根本没有将React / ES6代码编译成ES5一样。

Webpack输出

Here is my webpack config: 这是我的webpack配置:

const path = require('path');
const webpack = require('webpack');
const WebpackStrip = require('strip-loader');

module.exports = {
  devtool: 'source-map',
  context: path.join(__dirname, './CLIENTSIDE/components'),
  entry: {
     background: ['babel-polyfill', './background'],
     uniqueShare: ['babel-polyfill', './uniqueShare'],
     starRating: ['babel-polyfill', './starRating'],
     testingPage: ['babel-polyfill', './testingPage'],
     style: ['babel-polyfill', './style']
  },
  output: {
    path: path.join(__dirname, 'CLIENTSIDE/static'),
    filename: '[name].js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new webpack.optimize.UglifyJsPlugin({ mangle: true, sourcemap: false })
  ],
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: [
          path.resolve(__dirname, "node_modules"),
        ],
        use: [
          { loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              presets: ['es2015', 'stage-0', 'react'],
              plugins: ['transform-runtime','transform-decorators-legacy', 'transform-object-assign', 'array-includes']
            }
          },
          { loader: WebpackStrip.loader('debug', 'console.log') }
        ],
      },
      {
          test: /\.scss$/,
          loaders: ['style-loader', 'css-loader', 'sass-loader']
      }
    ]
  }
};

Strangely enough, I have the (almost) EXACT same webpack / babel config running in another app and compiling with zero issues. 奇怪的是,我有(几乎) 完全一样的WebPack /巴贝尔配置在另一个应用程序运行,并与零个问题进行编译。 I even added a few things like babel-polyfill with no luck. 我甚至添加了一些像babel-polyfill没有运气的东西。

Here is the relevant package.json: 这是相关的package.json:

"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-array-includes": "^2.0.3",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.24.1",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"webpack": "^3.6.0"

The code it is transpiling has been into production mode before, and is now directly causing legacy browsers to break due to the presence of ES6 code (when I remove UglifyJS from Webpack entirely, the code will compile and upload to Heroku just fine. It builds great per the logs. And, on modern browsers like Chrome 60, the thing runs great. But switching to browers that explicitly do NOT support ES6, such as Safari on iOS 9, or old versions of Chrome, breaks down entirely and throws ES6 errors like Uncaught SyntaxError: Use of const in strict mode . So it's clearly not down-compiling to ES5. HELP! ) 它正在转换的代码之前已经进入生产模式,并且由于ES6代码的存在,现在直接导致旧浏览器中断(当我完全从Webpack中删除UglifyJS时,代码将编译并上传到Heroku就好了。它构建在Chrome 60这样的现代浏览器上,事情运行得很好。但切换到明确不支持ES6的浏览器,例如iOS 9上的Safari或旧版Chrome,会完全崩溃并抛出ES6错误像Uncaught SyntaxError: Use of const in strict mode 。所以它显然没有向ES5下编译。 帮助!

After many hours of systematically moving through our code and removing individual require() calls, I discovered the root of the problem. 经过几个小时系统地遍历我们的代码并删除各个require()调用后,我发现了问题的根源。

We use an NPM module called striptags - https://www.npmjs.com/package/striptags - to get rid of HTML fragments on certain pieces of copy within the app that is managed from an external CMS. 我们使用名为striptags的NPM模块 - https://www.npmjs.com/package/striptags - 来删除从外部CMS管理的应用程序中的某些副本上的HTML片段。

striptags v3.0.0+ introduces a breaking change that completely removes compatibility with UglifyJS and, hence, prevents our Webpack stack from compiling our code into ES5. striptags v3.0.0+ 引入了一个重大变化,完全消除了与UglifyJS的兼容性 ,因此阻止了我们的Webpack堆栈将我们的代码编译成ES5。 (In a sense, @Kryten was right - we removed the striptags package from the other build and opted for a different content management system, where our identical Webpack compiler was working) (从某种意义上说, @ Kryten是对的 - 我们从其他版本中删除了striptags包,并选择了一个不同的内容管理系统,我们相同的Webpack编译器正在运行)

From the striptags documentation: striptags文档:

Note: v3+ targets ES6, and is therefore incompatible with the master branch of uglifyjs. 注意:v3 +以ES6为目标,因此与uglifyjs的主分支不兼容。 You can either: 你可以:

  • use babili, which supports ES6 使用支持ES6的babili
  • use the harmony branch of uglifyjs 使用uglifyjs的和谐分支
  • stick with the 2.xx branch 坚持2.xx分支

We opted to roll back our version of striptags to v2.2.1 explicitly. 我们选择回滚我们的版本striptagsv2.2.1明确。

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

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