【问题标题】:Why does the es6 version of my webpack config give me an error, but the es5 version doesn't?为什么我的 webpack 配置的 es6 版本给我一个错误,而 es5 版本没有?
【发布时间】:2015-11-06 17:51:41
【问题描述】:

我正在使用 webpack 捆绑一个项目,并且我正在使用这个使用 babel 6(节点 5,npm 3)编译的配置:

import webpack from 'webpack';

export default {
  entry: './node_modules/graphql/graphql.js',
  output: {
    path: './build',
    filename: 'bundle.js',
    libraryTarget: "var",
    library: "graphql"
  },
  resolve: {
    extensions: ['', '.js']
  },
  devtool: 'eval'
};

但是,使用 babel 6,当我运行 webpack 时,这会给我一个错误:

Output filename not configured.

如果我将此代码翻译回 es5:

var webpack = require('webpack');

module.exports = {
  entry: './node_modules/graphql/graphql.js',
  output: {
    path: './build',
    filename: 'bundle.js',
    libraryTarget: "var",
    library: "graphql"
  },
  resolve: {
    extensions: ['', '.js']
  },
  devtool: 'eval'
};

效果很好。

我的.babelrc 文件如下所示:

{
  "plugins": ["transform-es2015-modules-commonjs"]
}

我在本地 node_modules 中安装了以下软件包:

├── babel@6.0.15
├── babel-core@6.1.2
├── babel-loader@6.0.1
├── babel-plugin-transform-es2015-modules-commonjs@6.1.3
├── babel-runtime@6.0.14
├── graphql@0.4.12
└── webpack@1.12.3

编辑 在进一步检查 Babel 吐出的代码后,我认为我的问题是 export default 没有正确转换为 modules.export

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _webpack = require('webpack');

var _webpack2 = _interopRequireDefault(_webpack);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = {
  entry: './node_modules/graphql/graphql.js',
  output: {
    path: './build',
    filename: 'bundle.js',
    libraryTarget: "amd",
    library: "graphql"
  },
  resolve: {
    extensions: ['', '.js']
  },
  devtool: 'eval'
};

如何让 Babel 使用 transform-es2015-modules-commonjs 插件正确地将 es6 模块代码转换为 Node 的模块格式?

【问题讨论】:

标签: javascript ecmascript-6 webpack babeljs


【解决方案1】:

Babel 似乎已弃用此功能。

https://github.com/babel/babel/issues/2212

【讨论】:

    猜你喜欢
    • 2022-06-17
    • 2011-10-27
    • 2020-04-05
    • 2018-05-23
    • 2015-03-29
    • 1970-01-01
    • 2016-11-08
    • 2016-11-27
    • 2020-07-10
    相关资源
    最近更新 更多