我想在webpack 2中使用动态导入。我使用webpack 2.4.1
index.js
import 'babel-polyfill'
import React from 'react'
import { render } from 'react-dom'
import Root from './containers/Root'
import '../styles/custom.scss'
var btn = document.getElementsById('button');
btn.addEventListener("click", () => {
import('./assets/chunk.css').then(()=>{
console.log("Success")
})
});
render(
<Root />,
document.getElementById('root')
)
但是当我跑步&#34; npm start&#34;它没有工作,并在index.js的第9行通知错误
Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level (9:4)
感谢您的帮助!
答案 0 :(得分:9)
正如wuxiandiejia所说,答案是
babel-plugin-syntax-dynamic-import
'syntax-dynamic-import'
醇>
示例options.plugins
babel.rc