我正在开发一个非常基本和简单的应用程序,核心是javascript和html
我已经安装了webpack v 4并使用
npm run webpack
项目编译成功;下面是输出
> canvas@1.0.0 webpack /opt/parixan/canvas
> webpack-dev-server --inline --hot
â¹ ï½¢wdsï½£: Project is running at http://localhost:8080/
â¹ ï½¢wdsï½£: webpack output is served from /
â¹ ï½¢wdmï½£: Hash: 99985e9b8632c783a375
Version: webpack 4.12.1
Time: 819ms
Built at: 2018-06-30 18:45:48
Asset Size Chunks Chunk Names
bundle.js 354 KiB main [emitted] main
bundle.js.map 406 KiB main [emitted] main
Entrypoint main = bundle.js bundle.js.map
[./js/color.js] 359 bytes {main} [built]
[./js/index.js] 628 bytes {main} [built]
[./node_modules/loglevel/lib/loglevel.js] 7.68 KiB {main} [built]
[./node_modules/sockjs-client/dist/sockjs.js] 176 KiB {main} [built]
[./node_modules/url/url.js] 22.8 KiB {main} [built]
[./node_modules/webpack-dev-server/client/index.js?http://localhost:8080] (webpack)-dev-server/client?http://localhost:8080 7.75 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.58 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.05 KiB {main} [built]
[./node_modules/webpack-dev-server/node_modules/strip-ansi/index.js] (webpack)-dev-server/node_modules/strip-ansi/index.js 161 bytes {main} [built]
[0] multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./js/index.js 52 bytes {main} [built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
[./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.6 KiB {main} [built]
[./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 77 bytes {main} [built]
[./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built]
[./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1010 bytes {main} [built]
+ 14 hidden modules
â¹ ï½¢wdmï½£: Compiled successfully.
但是当我导航到
本地主机:8080/alphabet.html
它在控制台中抛出一个错误
未捕获的语法错误:意外的令牌导出。字母.js:30
我也注意到没有
bundle.js包
在下面归档
建造
文件夹,如中所述
webpack.config.js文件
这是我的文件夹结构
âââ README.md
âââ alphabet.css
âââ alphabet.html
âââ build
âââ fruits
âââ js
âââ alphabet.js
âââ color.js
âââ index.js
âââ .babelrc
âââ package.json
âââ webpack.config.js
-
节点-V
8.9.2条
-
净现值
5.5.1条
-
MacOS-V型
10.13.3条
-
巴别塔——版本
6.26.0(巴别塔核心6.26.3)
巴贝尔
{
"presets": ["@babel/preset-env"]
}
包.json
"scripts": {
"webpack": "webpack-dev-server --inline --hot",
"start": "http-server"
},
"main": "./js.index.js",
"devDependencies": {
"@babel/core": "^7.0.0-beta.51",
"babel-loader": "^8.0.0-beta.4",
"babel-register": "^6.26.0",
"jsxobj": "^1.1.0",
"webpack": "^4.12.1",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"@babel/preset-env": "^7.0.0-beta.51",
"babel-eslint": "^7.2.3",
"babel-preset-es2015": "^6.24.1",
"path": "^0.12.7"
}
webpack.config.js文件
module.exports = {
context: __dirname,
entry: "./js/index.js",
mode: "development",
output: {
path: __dirname + "/build",
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"]
}
}
}
]
},
devtool: "source-map",
resolve: {
extensions: [".html", ".js", ".json", ".css"]
}
};
/js/索引.js
import { fetchImage } from "./alphabet.js";
import { color } from "./color.js";
const colorBox = color;
console.log("colorBox", colorBox);
const len = Object.keys(colorBox).length;
fetchImage(colorBox);
js/字母.js
export const fetchImage = letter => {
const fruit = fruits[letter] || "tamarindo";
const request = new Request(`./fruits/${fruit}.png`, myInit);
fetch(request)
.then(response => response.blob())
.then(blob => {
const objectURL = URL.createObjectURL(blob);
const img = new Image(200);
img.src = objectURL;
let element = document.getElementById("figure");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
element.appendChild(img);
});
};
我对这件事的主要怀疑
字母表.html
文件
字母表.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Alphabet</title>
<link rel="stylesheet" href="./alphabet.css">
</head>
<body>
<div class="content" >
<section class="alphabet"></section>
<figure id="figure"></figure>
</div>
<script src="./js/alphabet.js"></script>
</body>
</html>
如果我改变
<script src="./js/index.js">
比它抛出的错误
未捕获的语法错误:意外的标记{
即
进口
和
出口
无法识别关键字
如何解决这个问题?