是否有任何命令行工具可以将单个 JavaScript 文件分解为多个较小的文件?

发布于 2025-01-06 03:24:08 字数 104 浏览 3 评论 0原文

假设我有一个巨大的 JavaScript 文件,我希望找到一个工具,可以通过指定 MAX_SIZE 自动将该文件转换为一组较小的文件。

有这样的工具吗?这样的工具实施起来容易吗?

Provided I have a huge JavaScript file I would like to find a tool that automatically, by specifying MAX_SIZE, for instance, converts that file into a set of smaller files.

Does that kind of tool exist? Would it be easy to implement such a tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2025-01-13 03:24:08

我认为这样的工具不存在,并且尝试创建一个工具将是一大堆蠕虫。显然,您不能将代码分解为特定大小,因为这很可能发生在某个函数或其他函数的中间。因此,这样的工具需要能够解析 javascript,以免破坏它。更糟糕的是,无法保证不同文件中的函数彼此有任何关系。一个文件中的函数可能依赖于任意数量其他文件中定义的常量和函数,而这些文件又会依赖于其他文件,从而导致巨大的混乱。

如果您需要将代码分解为更小的文件,请开始对语义相关的函数进行分组并从中形成类,并确定它们对其他文件的依赖关系。这只能由人类程序员完成。

I don't think such a tool exists, and trying to create one would be a huge can of worms. You obviously cannot break up the code at a specific size, since that would most likely happen in the middle of some function or other. Therefore, such a tool would need to be able to parse javascript in order to not break it. Even worse, there would be no guarantee whatsoever that the functions in different files would have anything to do with each other. A function in one file could depend on constants and functions defined in any number of other files, which in turn would depend on other files, resulting in a colossal mess.

If you need to break your code up into smaller files, start grouping semantically related functions and form classes out of them, and determine their dependencies on other files. This can only be done by a human programmer.

活泼老夫 2025-01-13 03:24:08

我同意前面的答案,逻辑细分应该由程序员来完成。
另外,我想在不删除任何内容的情况下将一个文件分解为更多文件根本没有任何优势,因为您只会获得一些网络传输开销。

顺便说一句,不问就回答问题...
实际上你可以。

最常见的 JS 库由单个巨大对象声明组成,实际上是单个顶级语句脚本,如

libraryname = {
    fn1: function() { /* do something */ },
    fn2: function() { /* do something else */ },
    fn3: function() { /* do something else */ }
}

这就是为什么您不能简单地将文件拆分为两个有效脚本文件的原因。

如果在执行前重新组合,代码无论如何都可以拆分为非 js 有效块。
只需将代码拆分为许多文本文件,分别检索它们,然后按正确的顺序连接字符串,然后将其加载到页面中即可。
加载它可以通过多种方式完成,具体取决于您想要完成的任务。
您可以创建一个新的 script 元素并将其附加到 head 元素,或者对其调用 eval。

代码可能看起来像这样(即使可以优化):

function loadScript(name, n) {
    var chunks = new Array(n);
    for(var i=0; i<n; i++)
        chunks[i] = getChunk(name, i); // simple blocking ajax call retrieving chunk content
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.innerHTML = chunks.join('');
    document.getElementsByTagName('head')[0].appendChild(script);
}

I agree with the previous answer, a logical subdivision should be done by a programmer.
Also, I guess there would be no advantage at all in breaking a file into more files, without removing anything, as you only would get some web transfer overhead.

By the way, to reply to the question without asking...
actually you could.

Most common JS libraries are made of a single huge object declaration, effectively being a single top-level statement script, as in

libraryname = {
    fn1: function() { /* do something */ },
    fn2: function() { /* do something else */ },
    fn3: function() { /* do something else */ }
}

This is the reason why you can't trivially split the file into two valid script files.

The code can be split anyway into non js valid chunks, if recombined before execution.
Just split the code into many text files, retrieve them separately, then concatenate the strings in the right order and then load it into the page.
Loading it can be done in more than one way, depending on what you want to accomplish.
You can either create a new script element and append it to the head element, or call eval on it.

The code could look something like this (even if it could be optimized):

function loadScript(name, n) {
    var chunks = new Array(n);
    for(var i=0; i<n; i++)
        chunks[i] = getChunk(name, i); // simple blocking ajax call retrieving chunk content
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.innerHTML = chunks.join('');
    document.getElementsByTagName('head')[0].appendChild(script);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文