将es6 npm模块转换为es5

时间:2017-12-01 15:48:35

标签: javascript webpack ecmascript-6 babeljs

我目前使用的应用程序使用babel + webpack作为es6实现。 我的webpack配置如下:

`       const webpack = require('webpack');       const path = require('path');

  const DEV = process.env.NODE_ENV !== 'production';

  module.exports = {
    bail: !DEV,
    devtool: DEV ? 'cheap-module-source-map' : 'source-map',
    target: 'node',
    entry: './src/server/index.js',
    output: {
      path: path.join(__dirname,'build/server'),
      filename: 'bundle.js',
      publicPath: '/',
    },
    externals: (context, request, callback) => {
      // Externalize all npm modules.
      if (/^[a-z0-9-][a-z0-9-./]+$/.test(request)) {
        return callback(null, `commonjs ${request}`);
      }
      callback();
    },
    module: {
      loaders: [
        {
          test: /\.js$/,
          loader: 'babel-loader',
          // exclude: /(node_modules)/,
        },
        {
          test: /\.json$/,
          loader: 'json-loader',
        },
        {
          test: /\.(graphql|gql)$/,
          exclude: /node_modules/,
          loader: 'graphql-tag/loader',
        },
        {
           test: /\.js/,
           loader: 'import-glob',
           exclude: /(node_modules)/
        },
        {
          enforce: "pre",
          test: /\.js$/,
          exclude: /(node_modules)/,
          loader: 'eslint-loader'
        }
      ],
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(DEV ? 'development' : 'production'),
      }),
    ],
    node: {
      console: false,
      global: false,
      process: false,
      Buffer: false,
      __filename: false,
      __dirname: false,
      setImmediate: false,
    }
  };

One of the module node-lyft is implemented in es6 format . when I tried to import the package it gives the es6 error which is导入未定义。

1 个答案:

答案 0 :(得分:0)

第一次开始使用webpack时,webpack会让人感到困惑。你需要做的是将它添加到你的babel装载机

options: { presets: ['env'] } 

并确保您运行

 npm install babel-preset-env

您需要使用预设将ES2015 +编译为ES5