RequireJS-skipDirOptimize:true设置不会优化我的主模块

RequireJS - skipDirOptimize: true setting does not optimize my main module

本文关键字:优化 我的 模块 true 设置 RequireJS-skipDirOptimize      更新时间:2023-09-26

如果我使用RequireJS来优化我的整个项目,如果我使用设置skipDirOptimize:true,我的主模块将不会得到优化/u。根据我的理解,除了非构建层JS文件之外,所有东西都应该进行优化。这是一个错误还是我不理解这个参数的正确用法?

这是我的requirejs配置:

{
    appDir: '../project',
    mainConfigFile: '../project/assets/js/main.js',
    dir: '../httpdocs',
    optimize: "uglify",
    //Introduced in 2.1.2: If using "dir" for an output directory, normally the
    //optimize setting is used to optimize the build layers (the "modules"
    //section of the config) and any other JS file in the directory. However, if
    //the non-build layer JS files will not be loaded after a build, you can
    //skip the optimization of those files, to speed up builds. Set this value
    //to true if you want to skip optimizing those other non-build layer JS
    //files.
    skipDirOptimize: true,
    generateSourceMaps: false,
    normalizeDirDefines: "skip",
    uglify: {
        toplevel: true,
        ascii_only: true,
        beautify: false,
        max_line_length: 1000,
        defines: {
            DEBUG: ['name', 'false']
        },
        no_mangle: false
    },
    optimizeCss: "standard",
    removeCombined: true,
    modules: [
        {
            name: '../main'
        }
    ]
}

在模块中使用相对路径可能会导致r.js在决定是否对其进行优化时无法将其识别为构建捆绑包。

我也遇到了类似的问题(构建捆绑包没有得到优化(,不是使用相对的模块路径,而是使用路径配置来允许我的模块以不同于我的文件夹结构的方式命名:

({
    ...
    skipDirOptimize: true,
    paths: {
        'MyLibrary': ''
    },
    modules: [
       { name: 'MyLibrary/Main' }
    ],
    ...
})

这导致r.js(2.1.8(中的模块名称变为/Main,因此当它构建其_buildPathToModuleIndex映射时,由于有两个斜杠(例如C:'dev'project'output''Main(,密钥将不正确。

优化循环决定模块是否是构建捆绑包(因此即使在skipDirOptimize: true时也需要优化(的方式是使用其文件名(例如C:'dev'project'output'Main(在_buildPathToModuleIndex映射中查找模块。由于它在地图中有两个斜杠,它找不到它。因此,它不会被认为是一个构建捆绑包,也不会被优化。

试着在r.js中放入一些console.log,在那里它构建并访问_buildPathToModuleIndex,看看它在放什么以及用什么来查找它。

对于我的问题,解决方案是为'MyLibrary/Main': 'Main'添加一个路径条目(遗憾的是重复(。我不确定你的项目结构是什么,但如果你设置baseUrl: '../,然后简单地调用你的模块main,怎么样?