javascript – 避免在RequireJS主文件和r.js构建文件中复制“路径”配置?

前端之家收集整理的这篇文章主要介绍了javascript – 避免在RequireJS主文件和r.js构建文件中复制“路径”配置?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的文件夹结构(的一部分)

>节点测试

> bower_components
>建立
>公开

> main.js

> build.js

运行优化器与r.js -o build.js和以下配置工作正常:

// main.js file
requirejs.config({
    baseUrl: '../bower_components',paths: {
        'domready': 'domready/ready','jquery': 'jquery/jquery',}
});

requirejs(['domready','jquery'],function (domReady,$) {
    domReady(function () {

    });
});

// build.js file
({
    baseUrl: "bower_components",name: "./almond/almond",include: "./../public/main",out: "build/main.js",},preserveLicenseComments: false
})

但是,如果我删除build.js中的路径配置,它不再工作:

Tracing dependencies for: ./almond/almond Error: ENOENT,no such file
or directory
‘C:\Users\Marco\Documents\Progetti\nodejs-opt\bower_components\domready.js’
In module tree:
../public/main

Error: Error: ENOENT,no such file or directory
‘C:\Users\Marco\Documents\Progetti\nodejs-opt\bower_components\domready.js’
In module tree:
../public/main

06001

我想要干,避免添加一个依赖关系两次.这可能吗?

解决方法

如果要使用运行时代码中的相同配置来查找库的位置,可以使用 mainConfigFile选项:

…if you prefer the “main” JS file configuration to be read for the build so that you do not have to duplicate the values in a separate configuration,set this property to the location of that main JS file. The first requirejs({}),require({}),requirejs.config({}),or require.config({}) call found in that file will be used.

这样的东西

({
    baseUrl: "bower_components",mainConfigFile: '/some/path/main.js',// adjust path as needed
    name: "./almond/almond",preserveLicenseComments: false
})
原文链接:https://www.f2er.com/js/152435.html

猜你在找的JavaScript相关文章