gpt4 book ai didi

javascript - 使用 requirejs 和 grunt 构建多客户端项目

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:19 24 4
gpt4 key购买 nike

我正在从事一个项目,其中的主要代码应该由一堆不同的客户端使用。所以我们有一个 requirejs 项目,我最初的想法是有一个简单的 bootstrap.js 文件,它需要一个 app.js 文件,每个客户端都不同。

bootstrap.js

requirejs(['app'],function(app){
//some initial code here
app.start();
}

因此项目结构将如下所示:

|_bootstrap.js
|_commonModules
|_someModule.js
|_client1
|_app.js
|_modules
|_module.js
|_client2
|_app.js
|_modules
|_module.js

所以我的想法是使用 requirejs 的 r 编译器为每个客户端编译应用程序,并通过为每个步骤创建一个新的 build.js 将每次编译中的应用程序路径设置为 clientX/app.js:

({    
paths: {
"app": "client1/app"
}
})

所以目前我有一个 grunt 构建任务,它使用了一堆其他任务,如 uglify、usemin、md5 等等。我可以创建一个使用此任务但更改每个客户端的 requireJs 设置的新任务吗?还是有更好的方法来实现我的目标?

最佳答案

所以毕竟这并不难。很酷的是,您可以更改实际运行任务的配置,并且可以在运行任务中调用之前定义的任务。

//this was the old task to build one distribution
grunt.registerTask('build', ['clean:build', 'copy:build', 'useminPrepare', 'usemin', 'requirejs', 'concat', 'uglify', 'mincss', 'md5', 'manifest', 'copy:toClientFolder']);

grunt.registerTask('buildAll', function() {
['client1', 'client2'].forEach(function(client) {
//before every build task run a task to change the config
grunt.task.run('updateConfig:' + client, 'build');
});
});

//we need to change the config in a separate task,
//otherwise, change the config just in the forEach, would result in the same
//config for both task, using the client2 settings
grunt.registerTask('updateConfig', function(client) {
var requireJsName = 'requirejs.compile.options.name';
var clientFolder = 'copy.toClientFolder.files';
grunt.config(requireJsName, 'clients/' + client + '/Bootstrap');
grunt.config(clientFolder, [
{expand: true, cwd: 'dist', src: '**', dest: 'dist_' + client}
]);
});

客户端的 app.js 文件如下所示:

requirejs.config({
paths: {
'commonModules/someModule': 'clients1/modules/module'
}
});

requirejs(['boootstrap',
'commonModules/someModule1'],

function(boootstrap, someModule1) {
$(function() {
boootstrap();
someModule1();
});
});

关于javascript - 使用 requirejs 和 grunt 构建多客户端项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084247/

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