简体   繁体   English

vanilla JS:是否捆绑了模块

[英]vanilla JS : module bundled or not

Given a set of module-patterned files (a main one, and various subs), 给定一组模块模式的文件(一个主文件和多个子文件),

is there a way to detect if modules are bundled together into one single file OR not ? 有没有一种方法可以检测模块是否捆绑在一起成为一个文件? In the last case, I will load manually modules with head script src="" tag added via JS. 在最后一种情况下,我将手动加载带有通过JS添加的头脚本src =“”标签的模块。

Non-bundled files are for development, bundled one is for production use. 非捆绑文件用于开发,捆绑文件供生产使用。

EDIT Code is ES 5 ! 编辑代码是ES 5!

I don't consider using Browserify, Require or so on... 我不考虑使用Browserify,Require等。

I assemble my code with (very basic) gulpfiles (using concat, wrap) 我用(非常基本的)gulpfiles汇编代码(使用concat,wrap)

someone have an idea ? 有人有主意吗?

Assuming your bundled file has something in the name to indicate that it is the bundle you can check which script is loaded. 假设捆绑文件的名称中包含一些内容,以表明它是捆绑软件,则您可以检查加载了哪个脚本。 For example if you load TopModule.js in development mode and TopLevel.min.js in production you can put this detection in the TopLevel module: 例如,如果您在开发模式下加载TopModule.js,在生产环境中加载TopLevel.min.js,则可以将此检测结果放入TopLevel模块中:

var isBundled = (function () {
    var src = '';

    if ('undefined' !== typeof (document.currentScript) && document.currentScript.src) {
        src = document.currentScript.src;
    }
    else {
        (function () {
            var allScriptElements = document.scripts || document.getElementsByTagName('script'),
                    i;

            for (i = 0; i < allScriptElements.length; ++i) {
                if (/(TopLevel(\.min)*)\.js/i.test(allScriptElements[i].src)) {
                    src = allScriptElements[i].src;

                    break;
                }
            }
        }());
    }

    return /\.min\.js/i.test(src);
}());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM