我有2个JS文件
Index.js
const sum = require('./app');
const total = sum(10,5);
console.log(total);
app.js
function sum(a,b){
return a+b;
}
module.exports = sum;
webpack config
const path = require('path');
const config ={
entry:"./app/index.js",
output:{
path:path.resolve(__dirname,"app/build"),
filename:'bundle.js'
}
}
module.exports = config;
bundle.js正在生成,我已将包添加到我的index.html,在检查网络选项卡时也会加载该包。但console.log(total)
无效,无效。
答案 0 :(得分:4)
浏览器控制台中没有显示任何内容?有错误吗? 也许最好向我们展示你的配置(例如:webpack命令或/和你的package.json)?
您的代码可以正常使用此配置:
app
├── app.js
├── build
│ ├── bundle.js
│ └── index.html
└── index.js
webpack.config.js:
const path = require('path');
const config ={
entry:"./app/index.js",
output:{
path:path.resolve(__dirname,"app/build"),
filename:'bundle.js'
}
}
module.exports = config;
package.json:
{
"name": "stackof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack -d --watch"
},
"author": "Marwen <marwen_no_spam_pls@gmail.com> (http://www.gameole.com)",
"license": "ISC",
"devDependencies": {
"webpack": "2.2.1"
}
}
index.html:
<!DOCTYPE html>
<html>
<head>
<title>title from webpack</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="bundle.js"></script></body>
</html>
要生成捆绑包,您只需执行:
npm run dev