![](/img/trans.png)
[英]Exporting and bundling a function using webpack and calling it inside html script tag
[英]Bundling with webpack from script
我正在使用webpack将Javascript文件捆绑在项目中:
webpack --config myconfig.webpack.config.
从命令行可以。
但是我想创建一个构建任务,我正在使用jake ,所以为了创建包,我需要从Javascript调用webpack
。
我找不到在线API,基本上我需要这样的东西:
// Jakefile.js
var webpack = require("webpack");
desc('This is the default build task which also bundles stuff.');
task('default', function (params) {
webpack.bundle("path-to-config"); // Something like this?
});
我该如何实现?
我尝试了以下方法:
// Jakefile.js
var webpack = require("webpack");
var config = require("./webpack.config.js");
desc('This is the default build task which also bundles stuff.');
task('default', function (params) {
webpack(config);
});
webpack.config.js
是我的webpack配置。 当我从命令行使用并引用该文件时,该捆绑包已正确创建。 但是,使用上面的代码时,它不起作用。 当我执行它时,没有错误,但是没有发出捆绑。
在您的Attempt 1中 ,您似乎通过将配置传递给webpack方法来使用webpack的Node.js API。 如果采用这种方法,则webpack方法将返回一个编译器对象,您需要正确处理它。 例如
import webpack from 'webpack';
var config = {}; // Your webpack config
var wpInstanceCompiler = webpack(config);
wpInstanceCompiler.run(function(err, stats) {
if (stats.hasErrors()) {
console.log(stats.toJson("verbose");
}
});
这是通过Node.js API执行webpack配置的方式。 除非您运行编译器实例,否则不会生成输出。
这也为我工作:
var webpack = require("webpack");
var lib = require(path.join(__dirname, "webpack.config.js"));
desc('Builds the projects and generates the library.');
task('default', function() {
webpack(lib, function() {
console.log("Bundle successfully created!");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.