简体   繁体   English

我有 2 个 javascript 文件。 如何将它们捆绑到一个文件中并将所有代码包含在一个封闭的范围内?

[英]I have 2 javascript files. How to bundle them into one file and enclose all code in one enclosed scope?

Say i have 2 files helpers.js index.js假设我有 2 个文件 helpers.js index.js

Loading helpers.js will create global variables.加载 helpers.js 将创建全局变量。 I don't want that我不想那样

I want one file.js that looks like我想要一个看起来像的 file.js

(function () {
    helpers.js code
    index.js code
})();
  1. Is this even possible?这甚至可能吗?
  2. Can I do it with .NET Core Bundle And Minify?我可以使用 .NET Core Bundle 和 Minify 来实现吗?

You can use webpack to bundle the js code.您可以使用 webpack 来打包 js 代码。

Or just use ES6 modules to modularize your code.或者只是使用ES6 模块来模块化你的代码。

Note: Not all browsers support ES6 modules.注意:并非所有浏览器都支持 ES6 模块。 For that you need babel .为此,您需要babel

Please refer this answer.请参考这个答案。 I have explain this in detail .我已经详细解释了这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 webpack 和 file-loader 在两个捆绑 (scss) 文件中使用一个图像? 最后一个返回 undefined - How can I use one image in two bundle (scss) files with webpack and file-loader? The last one of them returns undefined 两个不同文件中的两个异步代码。 我希望它们一个接一个地运行,但它们同时运行 - Two asynchronous codes in two different files. I want them to run one by one but they run at the same time 不能包含多个js文件。 Onload事件仅针对最后一个文件触发,但与我拥有文件的次数相同。 怎么解决呢? - Can't include more than one js file. Onload event fires only for the last file but as many times as I have files. How to solve it? 将JavaScript拆分为多个文件还是将其全部合并到一个文件中更好? - Is it better to split JavaScript into multiple files, or have it all in one file? Winston javascript logger 正在创建两个单独的日志文件。 如何将所有条目记录到一个文件中? - Winston javascript logger is creating two separate log files. How do I log all entries into a single file? Javascript - 我有两个事件侦听器,它们运行相同的全局 scope function,但其中一个不工作……为什么? - Javascript - I have two event listeners which run the same global scope function, but one of them isn't working…why? 如何将.html文件合并到一个主文件中,并包含Angularjs中发布的html文件中的所有作用域值 - how to Merge the .html file into one Master file with all scope values from releated html files in Angularjs 当一个文件需要一个全局窗口对象时,如何捆绑/构建javascript文件 - How to bundle/build javascript files when one file expects a global window object Swagger-Codegen:如何将所有文件合并到一个文件中以进行客户端代码生成 - Swagger-Codegen: How do I consolidate all files into one file for Client-Code-Generation 将所有 javascript 文件合并为一个文件 - Merge all javascript files to one file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM