【发布时间】:2016-04-21 23:19:06
【问题描述】:
我似乎无法解决这个问题:
想一个简单的示例应用程序,它带有一个 javascript 模块 app1.js,在最简单的版本中看起来像这样:
function configure() {
// configure app
}
function start(id) {
// start app
}
// This is what I dislike and would like to find a better solution!
window.APP = {start: start};
export default {configure};
我的 html 页面 app.html 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="view"></div>
<script src="app.bundle.js"></script>
<script>APP.start('view');</script>
</body>
</html>
还有一个 webpack 配置文件 webpack.config.js,看起来像这样:
const webpack = require('webpack'); const path = require('path');
module.exports = {
entry: {'app': './app.js'},
output: {
path: __dirname,
filename: '[name].bundle.js'
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel?presets=es2015'
}]
}
};
我能想出的唯一方法是将 start 函数导出到窗口对象 (window.APP = {start: start}) 中的 APP 命名空间,就像在模块前时代完成的那样。有没有更好的(更模块化的方式)来做到这一点?
【问题讨论】:
标签: javascript webpack es6-module-loader