默认情况下,webpack打包js不会做任何转换和编译 而至原样输出,所以让浏览器支持es高级语法需要用babel来转换。

npm install --save-dev @babel/preset-env

@babel/core

babel-loader

webpack.config.js

const miniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {

entry: './src/index.js',

output: {

filename: 'main.js',

},

mode: 'development', // 或者 'production'

module: {

rules: [

{

test: /\.js/,

exclude: /(node_modules)/,

use: {

loader: 'babel-loader',

options: {

presets: ['@babel/preset-env']

}

}

}

]

}

}

src/index.js

const fn = (a) => {

console.log('fn')

return a+1

}

fn(2)

const promise = () => {

return new Promise((resolve, reject) => {

setTimeout(() => {

console.log('promise')

resolve('success')

}, 2000);

})

}

(async()=>{

await promise()

console.log('aaa')

})()

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。