Repository navigation
Expand file tree
/
Copy pathVersionTask.js
More file actions
134 lines (103 loc) · 3.12 KB
/
Copy pathVersionTask.js
File metadata and controls
134 lines (103 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
let fs, del, glob;
class VersionTask extends Elixir.Task {
/**
* Create a new CssTask instance.
*
* @param {string} name
* @param {GulpPaths} paths
*/
constructor(name, paths) {
super(name, null, paths);
this.publicPath = Elixir.config.publicPath;
this.buildPath = this.output.baseDir;
if (this.src.baseDir == this.buildPath) {
if (this.src.path.find(path => /\*/.test(path))) {
Elixir.fail(
'Because you\'ve overridden the "mix.version()" build path ' +
'to be the same as your source path, you cannot pass a ' +
'regular expression. Please use full file paths instead.'
);
}
}
}
/**
* Lazy load dependencies.
*/
loadDependencies() {
fs = require('fs');
del = require('del');
glob = require('glob');
}
/**
* Build the Gulp task.
*
* @param {Elixir.Plugins} $
*/
gulpTask($) {
this.recordStep('Versioning');
this.deleteManifestFiles();
return (
gulp
.src(this.src.path, { base: `./${this.publicPath}` })
.pipe($.rev())
.pipe(this.updateVersionedPathInFiles($))
.pipe(gulp.dest(this.buildPath))
.pipe($.rev.manifest())
.pipe(this.saveAs(gulp))
.on('end', this.copyMaps.bind(this))
);
}
/**
* Register file watchers.
*/
registerWatchers() {
this.watch(this.src.path);
}
/**
* Update files to point to the newly versioned file name.
*
* @param {Elixir.Plugins} $
*/
updateVersionedPathInFiles($) {
let buildFolder = this.buildPath.replace(this.publicPath, '').replace('\\', '/');
this.recordStep('Rewriting File Paths');
return $.revReplace({ prefix: buildFolder + '/' });
}
/**
* Empty all relevant files from the build directory.
*/
deleteManifestFiles() {
let manifest = `${this.buildPath}/rev-manifest.json`;
if (! fs.existsSync(manifest)) return;
manifest = JSON.parse(fs.readFileSync(manifest));
for (let key in manifest) {
del.sync(`${this.buildPath}/${manifest[key]}`, { force: true });
}
}
/**
* Copy source maps to the build directory.
*/
copyMaps() {
this.recordStep('Copying Source Maps');
this.src.path.forEach(file => {
glob(file, {}, (error, files) => {
if (error) return;
files.filter(file => fs.existsSync(`${file}.map`))
.forEach(this.copyMap.bind(this));
});
});
}
/**
* Copy a single map file over.
*
* @param {string} srcMap
*/
copyMap(srcMap) {
let destMap = srcMap.replace(this.publicPath, this.buildPath +'/').replace('//', '/');
if (destMap != srcMap) {
fs.createReadStream(`${srcMap}.map`)
.pipe(fs.createWriteStream(`${destMap}.map`));
}
}
}
export default VersionTask;