gpt4 book ai didi

javascript - 避免在 RequireJS 主文件和 r.js 构建文件中重复 "paths"配置?

转载 作者:可可西里 更新时间:2023-11-01 02:58:10 25 4
gpt4 key购买 nike

这是(部分)我的文件夹结构:

  • 节点测试
    • bower_components
    • build
    • 公开
      • 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",
paths: {
'domready': 'domready/ready',
'jquery': 'jquery/jquery',
},
preserveLicenseComments: false
})

但是,如果我删除 build.js 中的 paths 配置,它就不再起作用了:

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

at Object.fs.openSync (fs.js:427:18)

我想保持干爽,避免两次添加依赖项。这可能吗?

最佳答案

如果您想使用运行时代码中的相同配置来查找您的库的位置,您可以使用 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",
include: "./../public/main",
out: "build/main.js",
preserveLicenseComments: false
})

关于javascript - 避免在 RequireJS 主文件和 r.js 构建文件中重复 "paths"配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17456329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com