【发布时间】:2017-04-08 03:24:51
【问题描述】:
我开始使用 Webpack,但已经遇到以下问题:我创建了一个 app/index.js 文件(如文档中所述)。我还创建了一个 index.html 文件(从文档中复制了 HTML 和 CSS)。我在 CLI 中运行了正确的命令(包括生成 dist/bundle.js 文件)。
代码:
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<title>Webpack</title>
<!-- <script src="https://unpkg.com/lodash@4.16.6" 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
如何让它正确运行?
【问题讨论】:
-
你需要 Babel 来转译 ES6 语法
-
你可以在这里找到关于 Webpack 和 Babel 的教程:data-blogger.com/2017/04/07/…
标签: javascript html npm webpack babeljs