我已经安装了最新版本的babel.目前6.4.0.我创建了一个名为myclass.js的文件,其中包含以下代码.
class MyClass {
constructor(name) {
console.log("I am a MyClass object .. ", name);
}
}
var myclass = new MyClass('1234');
Run Code Online (Sandbox Code Playgroud)
创建我的类后,我在命令行中执行以下操作.
$> babel ./src/myclass.js --out-file ./out/app.js
Run Code Online (Sandbox Code Playgroud)
我希望我的app.js文件有es5编译的javascript,但它具有与myclass.js文件相同的精确代码.我错过了什么可能导致这种情况?
通过 webpack 运行我的代码后,它包含箭头函数。我需要代码在 ie11 中工作,所以我需要摆脱箭头函数。
我正在为所有 .js 文件使用 babel-loader。
我写了一个加载器来检查箭头函数的代码,并在 babel-loader 之后运行它,但没有得到任何箭头函数,所以我知道 babel 的输出是好的。
我还尝试了 babel-polyfill 和用于转换箭头功能的 babel 插件。
我知道 babel-loader 输出好的代码,我怀疑它可能是一个插件,但我不能只是禁用它们进行测试,因为这会破坏构建。
开发中使用的 Webpack 插件:
plugins: [
new webpack.DefinePlugin({
'process.env': require('../config/dev.env')
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
// copy custom static assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}
])
]
Run Code Online (Sandbox Code Playgroud)
这个问题也出现在 prod 中,但是在 …
javascript internet-explorer-11 webpack babeljs arrow-functions
我在 IE11 上生成的 webpack 包有问题。我检查了捆绑包,这是由于一些箭头函数。
它来自一个 node_module 包:lite-id
我的 webpack 配置:
var config = {
devtool: 'source-map',
entry: ["babel-polyfill", APP_DIR + '/index.js'],
output: {
path: BUILD_DIR,
filename: 'BundleNodeJs.js',
libraryTarget: "umd",
},
resolve: {
extensions: ['.js', '.jsx', '.css', '.scss'],
symlinks: false
},
[...]
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules|bower_components/,
loader: "babel-loader",
options: {
presets: ['env', 'react', 'stage-2']
}
},
]
}
};
Run Code Online (Sandbox Code Playgroud)
在此节点模块中将箭头函数转换为常规函数的好方法是什么?