似乎要运行这样的脚本,您必须在捆绑包上使用独立。
browserify main.js --standalone Bundle > bundle.js
之后你应该window.Bundle在bundle.js.
因此,此时您应该能够从script.js.
如果你使用 grunt
如果您使用的是grunt安装grunt-browserify.
npm install grunt-browserify --save-dev
然后在grunt.jsGruntfile 上:
// Add the task
grunt.loadNpmTasks('grunt-browserify');
// Add the configuration:
browserify: {
dist: {
options: {
// uncomment if you use babel
// transform: [
// ["babelify", { "presets": ["env"] }]
// ],
browserifyOptions: {
standalone: 'Bundle'
}
},
files: {
"bundle.js": ["main.js"]
}
}
},
如果你使用 gulp
// on your build task
var bundled = browserify('main.js', { standalone: 'Bundle' })
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest(outDir));
有关Chart.js gulp 文件,请参见此处。
如果你使用 babel
如果您正在使用 babel 并且es6可能正在导出您的Bundle课程。
// you should have something like that
class Bundle {
...
}
export default Bundle;
因此,由于 babel 现在要使用,Bundle您应该使用Bundle.default等:
// in script.js
var bundle = new Bundle.default();
为避免这种语法,您可以Bundle使用Bundle.default.
在 bundle.js 的末尾插入:
window.Bundle = window.Bundle.default;
所以现在你将拥有:
// in script.js
var bundle = new Bundle();
来源
独立的 browserify 构建