如何将我的库打包成一个自执行函数? (emscripten)
How to pack my library in a self executing function? (emscripten)
我使用 Embind 和 Emscripten 在 C++ 中创建了一个库。
一些手写的JS代码也被添加到库中使用--pre-js
图书馆工作。但我想重新排列代码,这样:
var MYLIB = (function(){
// ... Original Code ...
return Module;
})();
所以代码不会污染全局命名空间,代码压缩器可以做更好的优化。
emcc
中是否有针对此的内置函数?
该库仅会 运行 在网络浏览器中,不会在 nodejs 中。
您要查找的是 MODULARIZE
和 EXPORT_NAME
选项。查看 settings.js.
中的文档
引用该文件:
// By default we emit all code in a straightforward way into the output
// .js file. That means that if you load that in a script tag in a web
// page, it will use the global scope. With MODULARIZE set, we will instead emit
//
// var EXPORT_NAME = function(Module) {
// Module = Module || {};
// // .. all the emitted code from emscripten ..
// return Module;
// };
//
// where EXPORT_NAME is from the option of the same name
我使用 Embind 和 Emscripten 在 C++ 中创建了一个库。
一些手写的JS代码也被添加到库中使用--pre-js
图书馆工作。但我想重新排列代码,这样:
var MYLIB = (function(){
// ... Original Code ...
return Module;
})();
所以代码不会污染全局命名空间,代码压缩器可以做更好的优化。
emcc
中是否有针对此的内置函数?
该库仅会 运行 在网络浏览器中,不会在 nodejs 中。
您要查找的是 MODULARIZE
和 EXPORT_NAME
选项。查看 settings.js.
引用该文件:
// By default we emit all code in a straightforward way into the output
// .js file. That means that if you load that in a script tag in a web
// page, it will use the global scope. With MODULARIZE set, we will instead emit
//
// var EXPORT_NAME = function(Module) {
// Module = Module || {};
// // .. all the emitted code from emscripten ..
// return Module;
// };
//
// where EXPORT_NAME is from the option of the same name