如何使用Webpack包含Babel的节点模块

时间:2018-05-04 10:52:13

标签: npm webpack ecmascript-6 babel uglifyjs

运行时

npm run build

我遇到了来自uglify的与es6相关的语法错误,所以我猜测babel并没有正确处理节点模块(sec-to-min)。

我的.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']
      })
    ]
  }
}

&安培; ERR:

  

来自UglifyJs的static / js / vendor.8d64852626f0513309d9.js中的错误   SyntaxError:意外的令牌:运算符(>)   [./~/sec-to-min/index.js:3,0]

如何指导babel编译此模块?

4 个答案:

答案 0 :(得分:1)

请注意,在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']
            }
          },

我发现使用排除功能很有帮助,因为我可以在功能内添加控制台日志,以检查正则表达式匹配的模块。

答案 1 :(得分:0)

安装以下软件包

npm install babel-preset-es2015 --save-dev
npm install babel-preset-stage-0 --save-dev

在您的babel预设中添加es2015stage-0

{
   "presets": ["es2015", "stage-0"],
}

<强>更新 试试这个

{
    test: /\.js$/,
    use: {
      loader: 'babel-loader',
       options: {
        presets: ['env', 'react', 'stage-0'],
       },
    },
    include: [
      path.join(projectRoot, 'src'),
      'node_modules/sec-to-min'
    ],
    exclude: /node_modules/
  },

答案 2 :(得分:0)

在webpack配置的babel部分更改为:

  {
    test: /\.js$/,
    loader: 'babel',
    include: [
      path.join(projectRoot, 'src')
    ],
    exclude: /node_modules\/(?!(sec-to-min)\/).*/
  }

看起来exclude的优先级高于include

答案 3 :(得分:0)

在Webpack v4中为我工作:

rules: [{
    include: [
    path.resolve(__dirname, 'src'),
    /node_modules\/(dom7|swiper)/
  ],
  loader: 'babel-loader',
  presets: [['@babel/preset-env', {
  'modules': false
  }]]
},
  test: /\.(js)$/
}]