@@ -6,7 +6,19 @@ module.exports = function (grunt) {
6
6
lang = require ( libDir + "lang" ) ,
7
7
normalizeCfg = require ( libDir + "normalizeConfig" ) ;
8
8
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 ;
10
22
var buildConfig = normalizeCfg . build ( grunt . config ( buildCfg ) ) ;
11
23
var layerConfig = buildConfig . layersByName [ layerName ] ;
12
24
var dir = buildConfig . dir ;
@@ -19,27 +31,33 @@ module.exports = function (grunt) {
19
31
rel : [ ]
20
32
} ;
21
33
34
+
35
+ var trimBaseUrlLeadingDots = getTrimBaseUrlLeadingDots ( baseUrl ) ;
36
+
22
37
layerConfig . shim . forEach ( function ( shim ) {
23
- var absPath = dir + shim . filepath ;
38
+ var path = trimBaseUrlLeadingDots ( shim . filepath ) ;
39
+ var absPath = dir + path ;
24
40
25
41
grunt . file . write ( absPath , shim . content ) ;
26
42
modulesFiles . abs . push ( absPath ) ;
27
- modulesFiles . rel . push ( shim . filepath ) ;
43
+ modulesFiles . rel . push ( path ) ;
28
44
} ) ;
29
45
30
46
lang . forEachModules ( layerConfig . modules , layerName , function ( module ) {
31
47
if ( ! module . filepath ) {
32
48
grunt . fail . warn ( "Undefined Path " + module . mid ) ;
33
49
}
34
50
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 ) ;
39
56
} ) ;
40
57
41
58
lang . eachProp ( layerConfig . pluginsFiles , function ( filepath , content ) {
42
- var absPath = dir + filepath ;
59
+ var destPath = trimBaseUrlLeadingDots ( filepath ) ;
60
+ var absDestPath = dir + destPath ;
43
61
44
62
// Process images included in css with url() param.
45
63
if ( / .c s s $ / . test ( filepath ) ) {
@@ -56,17 +74,18 @@ module.exports = function (grunt) {
56
74
// The assignment is required here to access the matched groups of a global regexp.
57
75
while ( ( match = urlRE . exec ( content ) ) ) {
58
76
var src = fileDir + match [ 1 ] ;
59
- grunt . file . copy ( src , dir + src , {
77
+ var dest = dir + trimBaseUrlLeadingDots ( src ) ;
78
+ grunt . file . copy ( src , dest , {
60
79
encoding : null
61
80
} ) ;
62
81
pluginsFiles . abs . push ( dir + src ) ;
63
82
pluginsFiles . rel . push ( src ) ;
64
83
}
65
84
}
66
85
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 ) ;
70
89
} ) ;
71
90
72
91
outputProp = outputProp || "amdoutput" ;
@@ -78,6 +97,6 @@ module.exports = function (grunt) {
78
97
grunt . config ( [ outputProp , "modules" ] , modulesFiles ) ;
79
98
grunt . config ( [ outputProp , "plugins" ] , pluginsFiles ) ;
80
99
grunt . config ( [ outputProp , "layerName" ] , layerName ) ;
81
- grunt . config ( [ outputProp , "layerPath" ] , layerConfig . outputPath ) ;
100
+ grunt . config ( [ outputProp , "layerPath" ] , trimBaseUrlLeadingDots ( layerConfig . outputPath ) ) ;
82
101
} ) ;
83
102
} ;
0 commit comments