我开始使用Webpack,但已经遇到以下问题:我创建了一个app/index.js文件(如文档中指定的)。我还创建了一个index.html文件(从文档中复制了HTML和CSS)。我在CLI中运行了正确的命令(包括生成dist/bundle.js文件)。Webpack入门,导入错误
代码:
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<title>Webpack</title>
<!-- <script src="https://unpkg.com/[email protected]" type="text/javascript"></script> -->
<script src="dist/bundle.js" type="text/javascript"></script>
</head>
<body>
<!-- Markup here -->
<div id="root"></div>
<!-- <script src="app/index.js" type="text/javascript"></script> -->
</body>
</html>
的JS:
// To bundle the lodash dependency with the index.js, we need to import lodash.
import _ from 'lodash';
function component() {
var element = document.createElement('div');
/* lodash is required for the next line to work */
element.innerHTML = _.map([ 'Hello, webpack' ], function(item) {
return item + ' ';
});
return element;
}
document.body.appendChild(component());
这将返回以下控制台错误:bundle.js:48 Uncaught SyntaxError: Unexpected token import
我如何得到这个正确运行?
你需要通天transpile的ES6语法 – Phil
你可以在这里找到关于Webpack和Babel的教程:https://www.data-blogger.com/2017/04/07/building-a-tic-tac-toe-web-app-with-webpack-babel-react -redux/ –