npm - 如何使用 Webpack 为 Babel 包含节点模块

标签 npm webpack ecmascript-6 babeljs uglifyjs

运行时

npm run build

我从 uglify 遇到了一个与 es6 相关的语法错误,所以我猜 babel 没有正确处理节点模块(秒到分钟)。

我的.babelrc
{
  "presets": ["es2015", "stage-0"],
  "plugins": ["transform-runtime"],
  "comments": false,
  "env": {
    "test": {
      "plugins": [ "istanbul" ]
    }
  }
}

我的 Webpack 配置:
var path = require('path')
var config = require('../config')
var utils = require('./utils')
var projectRoot = path.resolve(__dirname, '../')

var env = process.env.NODE_ENV
// check env & config/index.js to decide whether to enable CSS source maps for the
// various preprocessor loaders added to vue-loader at the end of this file
var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
var useCssSourceMap = cssSourceMapDev || cssSourceMapProd

module.exports = {
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js', '.vue', '.json'],
    fallback: [path.join(__dirname, '../node_modules')],
    alias: {
      'vue$': 'vue/dist/vue.common.js',
      'src': path.resolve(__dirname, '../src'),
      'assets': path.resolve(__dirname, '../src/assets'),
      'components': path.resolve(__dirname, '../src/components')
    }
  },
  resolveLoader: {
    fallback: [path.join(__dirname, '../node_modules')]
  },
  module: {
    preLoaders: [
      {
        test: /\.vue$/,
        loader: 'eslint',
        include: [
          path.join(projectRoot, 'src')
        ],
        exclude: /node_modules/
      },
      {
        test: /\.js$/,
        loader: 'eslint',
        include: [
          path.join(projectRoot, 'src')
        ],
        exclude: /node_modules/
      }
    ],
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue'
      },
      {
        test: /\.js$/,
        loader: 'babel',
        include: [
          path.join(projectRoot, 'src'),
          'node_modules/sec-to-min'
        ],
        exclude: /node_modules/
      },
      {
        test: /\.json$/,
        loader: 'json'
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  eslint: {
    formatter: require('eslint-friendly-formatter')
  },
  vue: {
    loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),
    postcss: [
      require('autoprefixer')({
        browsers: ['last 2 versions']
      })
    ]
  }
}

&错误:

ERROR in static/js/vendor.8d64852626f0513309d9.js from UglifyJs SyntaxError: Unexpected token: operator (>) [./~/sec-to-min/index.js:3,0]



如何指导 babel 编译这个模块?

最佳答案

请注意,在 Windows 上,路径中的斜杠将是\因此上述解决方案必须更改为 exclude: /node_modules\\(?!(sec-to-min)\/).*/
这是对我有用的解决方案,使用 webpack 4.3 和 babel-loader 8.0.5,并使用推荐的 @babel/preset-env,改编自这里 https://github.com/webpack/webpack/issues/2031#issuecomment-283517150

{
            test: /\.(js|jsx|mjs)$/,
            include: [paths.appSrc, paths.appNodeModules],
            exclude: function(modulePath) {
              return (
                /node_modules/.test(modulePath) &&
                !/node_modules\\react-dev-utils/.test(modulePath)
              );
            },
            loader: require.resolve('babel-loader'),
            options: {
              compact: true,
              presets: ['@babel/preset-env']
            }
          },

我发现使用排除函数很有帮助,因为我能够在函数中添加控制台日志来检查正则表达式匹配哪些模块。

关于npm - 如何使用 Webpack 为 Babel 包含节点模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173228/

相关文章:

javascript - 输入行前面的前缀

javascript - 使用 NPM 和 Watchify 编译多个脚本包

javascript - 通过 Babel 使用 Es2015 模块进行 Webpack 和 React

node.js - 将 babel 与 Node 集群结合使用

node.js - Meteor:如何列出已安装的软件包

node.js - npm 发布 node.js 模块,但在 npm 安装后找不到模块

angular - 仅在 Safari 上的 Angular ThreeJS 应用程序中的 ChunkLoadError

javascript - 我应该如何在 webpack 中使用时刻时区?

ecmascript-6 - 我什么时候应该直接调用 Promise.resolve() ?

javascript - spread 元素在这里起什么作用