Skip to content

Commit 4a7aaf8

Browse files
committed
remove the leading ../ from baseUrl before outputing the files with amdserialize. Changes the signature of amdserialize task so it can access loader config. fixes #16
1 parent 32b609c commit 4a7aaf8

File tree

15 files changed

+153
-24
lines changed

15 files changed

+153
-24
lines changed

docs/Walkthrough.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ grunt.registerTask("amdbuild", function (amdloader) {
156156

157157
layers.forEach(function (layer) {
158158
grunt.task.run("amddepsscan:" + layer.name + ":" + name + ":" + amdloader);
159-
grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + outprop);
159+
grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + amdloader + ":" + outprop);
160160
grunt.task.run("uglify");
161161
grunt.task.run("copy:plugins");
162162
});

docs/api/amdserialiaze.md renamed to docs/api/amdserialize.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
amdserialize is the task used to output the results of a construction task.
33
This output will be combine into a layer by a minifier (like UglifyJS) or a concatenation task (like grunt-contrib-concat).
44

5-
### amdserialize (layerName, buildConfig, outputProp)
5+
### amdserialize (layerName, buildConfig, loaderConfig, outputProp)
66
Write all the files generated by a construction task to the `buildConfig.dir` directory.
77
This task will also find the images that may have been included in the css files required with a plugin.
88
Set the `outputProp` with the list of written files.
99

1010
#### Arguments
1111
1. layerName _(String)_: The layer name.
1212
1. buildConfig _(String)_: Name of the property where the build configuration is stored.
13+
1. loaderConfig _(String)_: Name of the property where the amd loader configuration is stored.
1314
1. outputProp _(String)_: Name of the property where the list of generated files will be stored.
1415

1516
#### Task configuration

samples/Gruntfile-full.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = function (grunt) {
109109

110110
layers.forEach(function (layer) {
111111
grunt.task.run("amddepsscan:" + layer.name + ":" + name + ":" + amdloader);
112-
grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + outprop);
112+
grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + amdloader + ":" + outprop);
113113
// Generate a minified layer only if the name ends with ".min".
114114
if (layer.name.search(/\.min$/) !== -1) {
115115
grunt.task.run("uglify");

samples/Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = function (grunt) {
6969

7070
layers.forEach(function (layer) {
7171
grunt.task.run("amddepsscan:" + layer.name + ":" + name + ":" + amdloader);
72-
grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + outprop);
72+
grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + amdloader + ":" + outprop);
7373
grunt.task.run("uglify");
7474
grunt.task.run("copy:plugins");
7575
});

tasks/amdserialize.js

+32-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@ module.exports = function (grunt) {
66
lang = require(libDir + "lang"),
77
normalizeCfg = require(libDir + "normalizeConfig");
88

9-
grunt.registerTask("amdserialize", function (layerName, buildCfg, outputProp) {
9+
// Trim the leading dots to avoid writing files outside of dir if baseUrl start with ../
10+
// Since all paths should start with baseUrl their should be no collision.
11+
function getTrimBaseUrlLeadingDots(baseUrl) {
12+
var trimmedBaseUrl = baseUrl.replace(/^(\.\/)*(\.\.\/)*/, "");
13+
return function (string) {
14+
// match baseUrl and replace with the trimmed baseUrl
15+
return string.replace(baseUrl, trimmedBaseUrl);
16+
};
17+
}
18+
19+
grunt.registerTask("amdserialize", function (layerName, buildCfg, loaderCfg, outputProp) {
20+
var loaderConfig = normalizeCfg.loader(grunt.config(loaderCfg));
21+
var baseUrl = loaderConfig.baseUrl;
1022
var buildConfig = normalizeCfg.build(grunt.config(buildCfg));
1123
var layerConfig = buildConfig.layersByName[layerName];
1224
var dir = buildConfig.dir;
@@ -19,27 +31,33 @@ module.exports = function (grunt) {
1931
rel: []
2032
};
2133

34+
35+
var trimBaseUrlLeadingDots = getTrimBaseUrlLeadingDots(baseUrl);
36+
2237
layerConfig.shim.forEach(function (shim) {
23-
var absPath = dir + shim.filepath;
38+
var path = trimBaseUrlLeadingDots(shim.filepath);
39+
var absPath = dir + path;
2440

2541
grunt.file.write(absPath, shim.content);
2642
modulesFiles.abs.push(absPath);
27-
modulesFiles.rel.push(shim.filepath);
43+
modulesFiles.rel.push(path);
2844
});
2945

3046
lang.forEachModules(layerConfig.modules, layerName, function (module) {
3147
if (!module.filepath) {
3248
grunt.fail.warn("Undefined Path " + module.mid);
3349
}
3450

35-
var path = dir + module.filepath;
36-
grunt.file.write(path, module.content);
37-
modulesFiles.abs.push(path);
38-
modulesFiles.rel.push(module.filepath);
51+
var path = trimBaseUrlLeadingDots(module.filepath);
52+
var absPath = dir + path;
53+
grunt.file.write(absPath, module.content);
54+
modulesFiles.abs.push(absPath);
55+
modulesFiles.rel.push(path);
3956
});
4057

4158
lang.eachProp(layerConfig.pluginsFiles, function (filepath, content) {
42-
var absPath = dir + filepath;
59+
var destPath = trimBaseUrlLeadingDots(filepath);
60+
var absDestPath = dir + destPath;
4361

4462
// Process images included in css with url() param.
4563
if (/.css$/.test(filepath)) {
@@ -56,17 +74,18 @@ module.exports = function (grunt) {
5674
// The assignment is required here to access the matched groups of a global regexp.
5775
while ((match = urlRE.exec(content))) {
5876
var src = fileDir + match[1];
59-
grunt.file.copy(src, dir + src, {
77+
var dest = dir + trimBaseUrlLeadingDots(src);
78+
grunt.file.copy(src, dest, {
6079
encoding: null
6180
});
6281
pluginsFiles.abs.push(dir + src);
6382
pluginsFiles.rel.push(src);
6483
}
6584
}
6685

67-
grunt.file.write(absPath, content);
68-
pluginsFiles.abs.push(absPath);
69-
pluginsFiles.rel.push(filepath);
86+
grunt.file.write(absDestPath, content);
87+
pluginsFiles.abs.push(absDestPath);
88+
pluginsFiles.rel.push(destPath);
7089
});
7190

7291
outputProp = outputProp || "amdoutput";
@@ -78,6 +97,6 @@ module.exports = function (grunt) {
7897
grunt.config([outputProp, "modules"], modulesFiles);
7998
grunt.config([outputProp, "plugins"], pluginsFiles);
8099
grunt.config([outputProp, "layerName"], layerName);
81-
grunt.config([outputProp, "layerPath"], layerConfig.outputPath);
100+
grunt.config([outputProp, "layerPath"], trimBaseUrlLeadingDots(layerConfig.outputPath));
82101
});
83102
};

tests/build/amddirscan/src/Gruntfile.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ module.exports = function (grunt) {
6161
grunt.registerTask('amdbuild', function (amdloader) {
6262
var name = this.name, layers = grunt.config(name).layers;
6363
layers.forEach(function (layer) {
64-
grunt.task.run("amdshim:" + layer.name + ":" + name + ":" + amdloader);
6564
grunt.task.run('amddirscan:' + layer.name + ':' + name + ':' + amdloader);
66-
grunt.task.run('amdserialize:' + layer.name + ':' + name + ':' + outprop);
65+
grunt.task.run('amdserialize:' + layer.name + ':' + name + ":" + amdloader + ':' + outprop);
6766
grunt.task.run('concat');
6867
grunt.task.run('copy:plugins');
6968
});

tests/build/app/src/Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module.exports = function (grunt) {
8080
layers.forEach(function (layer) {
8181
grunt.task.run("amdshim:" + layer.name + ":" + name + ":" + amdloader);
8282
grunt.task.run('amddepsscan:' + layer.name + ':' + name + ':' + amdloader);
83-
grunt.task.run('amdserialize:' + layer.name + ':' + name + ':' + outprop);
83+
grunt.task.run('amdserialize:' + layer.name + ':' + name + ":" + amdloader + ':' + outprop);
8484
grunt.task.run('concat');
8585
grunt.task.run('copy:plugins');
8686
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"myapp/layer": {
3+
"modules": {
4+
"myLib/main": true,
5+
"myapp/src": true
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
define('myapp/src',[
2+
"myLib/main"
3+
], function () {
4+
return {
5+
log: function () {
6+
console.log("src !");
7+
}
8+
};
9+
});
10+
;
11+
define('myLib/main',[],function () {
12+
return {
13+
init: function () {
14+
console.log("main !");
15+
}
16+
17+
};
18+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define(function () {
2+
return {
3+
init: function () {
4+
console.log("main !");
5+
}
6+
7+
};
8+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module.exports = function (grunt) {
2+
'use strict';
3+
grunt.loadNpmTasks('grunt-contrib-clean');
4+
grunt.loadNpmTasks('grunt-contrib-copy');
5+
grunt.loadNpmTasks('grunt-contrib-concat');
6+
grunt.loadNpmTasks('grunt-amd-build');
7+
var outprop = 'amdoutput';
8+
var outdir = '../results/';
9+
var tmpdir = './tmp/';
10+
var common = {
11+
options: { banner: '<%= ' + outprop + '.header%>' },
12+
src: '<%= ' + outprop + '.modules.abs %>',
13+
dest: outdir + '<%= ' + outprop + '.layerPath %>'
14+
};
15+
grunt.initConfig({
16+
amdloader: {
17+
baseUrl: '../libs/',
18+
packages: [
19+
{
20+
name: 'myapp',
21+
location: '../src/myapp'
22+
}
23+
]
24+
},
25+
amdbuild: {
26+
dir: tmpdir,
27+
layers: [{
28+
name: 'myapp/layer',
29+
include: ["myapp/src"]
30+
}]
31+
},
32+
amdreportjson: {
33+
dir: outdir
34+
},
35+
concat: {
36+
options: { separator: ';\n' },
37+
dist: common
38+
},
39+
clean: {
40+
finish: [tmpdir]
41+
}
42+
});
43+
grunt.registerTask('amdbuild', function (amdloader) {
44+
var name = this.name, layers = grunt.config(name).layers;
45+
layers.forEach(function (layer) {
46+
grunt.task.run('amddepsscan:' + layer.name + ':' + name + ':' + amdloader);
47+
grunt.task.run('amdserialize:' + layer.name + ':' + name + ":" + amdloader + ':' + outprop);
48+
grunt.task.run('concat');
49+
});
50+
});
51+
grunt.registerTask('build', [
52+
'amdbuild:amdloader',
53+
'amdreportjson:amdbuild'/*,
54+
'clean:finish'*/
55+
]);
56+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define([
2+
"myLib/main"
3+
], function () {
4+
return {
5+
log: function () {
6+
console.log("src !");
7+
}
8+
};
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "grunt-amd-build-test",
3+
"description": "A test project for Grunt plugin to build AMD applications.",
4+
"version": "0.1.0-dev",
5+
"dependencies": {
6+
"clean-css": "2.2.x",
7+
"grunt": "0.4.x",
8+
"grunt-amd-build": "git://github.com/ibm-js/grunt-amd-build.git#master",
9+
"grunt-contrib-clean": "0.6.x",
10+
"grunt-contrib-concat": "0.5.x",
11+
"grunt-contrib-copy": "0.5.x"
12+
}
13+
}

tests/build/includeExclude/src/Gruntfile.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ module.exports = function (grunt) {
179179
grunt.registerTask('amdbuild', function (amdloader) {
180180
var name = this.name, layers = grunt.config(name).layers;
181181
layers.forEach(function (layer) {
182-
grunt.task.run("amdshim:" + layer.name + ":" + name + ":" + amdloader);
183182
grunt.task.run('amddepsscan:' + layer.name + ':' + name + ':' + amdloader);
184-
grunt.task.run('amdserialize:' + layer.name + ':' + name + ':' + outprop);
183+
grunt.task.run('amdserialize:' + layer.name + ':' + name + ":" + amdloader + ':' + outprop);
185184
grunt.task.run('concat');
186185
grunt.task.run('copy:plugins');
187186
});

tests/build/plugins/src/Gruntfile.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ module.exports = function (grunt) {
6060
grunt.registerTask('amdbuild', function (amdloader) {
6161
var name = this.name, layers = grunt.config(name).layers;
6262
layers.forEach(function (layer) {
63-
grunt.task.run("amdshim:" + layer.name + ":" + name + ":" + amdloader);
6463
grunt.task.run('amddepsscan:' + layer.name + ':' + name + ':' + amdloader);
65-
grunt.task.run('amdserialize:' + layer.name + ':' + name + ':' + outprop);
64+
grunt.task.run('amdserialize:' + layer.name + ':' + name + ":" + amdloader + ':' + outprop);
6665
grunt.task.run('concat');
6766
grunt.task.run('copy:plugins');
6867
});

0 commit comments

Comments
 (0)