-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
writeFile.js
131 lines (115 loc) · 4.85 KB
/
writeFile.js
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
import loaderUtils from 'loader-utils';
import path from 'path';
import cacache from 'cacache';
import serialize from 'serialize-javascript';
import { name, version } from '../package.json';
import findCacheDir from 'find-cache-dir';
import { stat, readFile } from './utils/promisify';
import crypto from 'crypto';
export default function writeFile(globalRef, pattern, file) {
const {info, debug, compilation, fileDependencies, written, inputFileSystem, copyUnmodified} = globalRef;
return stat(inputFileSystem, file.absoluteFrom)
.then((stat) => {
// We don't write empty directories
if (stat.isDirectory()) {
return;
}
// If this came from a glob, add it to the file watchlist
if (pattern.fromType === 'glob') {
fileDependencies.push(file.absoluteFrom);
}
info(`reading ${file.absoluteFrom} to write to assets`);
return readFile(inputFileSystem, file.absoluteFrom)
.then((content) => {
if (pattern.transform) {
const transform = (content, absoluteFrom) => {
return pattern.transform(content, absoluteFrom);
};
if (pattern.cache) {
if (!globalRef.cacheDir) {
globalRef.cacheDir = findCacheDir({ name: 'copy-webpack-plugin' });
}
const cacheKey = pattern.cache.key
? pattern.cache.key
: serialize({
name,
version,
pattern,
hash: crypto.createHash('md4').update(content).digest('hex')
});
return cacache
.get(globalRef.cacheDir, cacheKey)
.then(
(result) => result.data,
() => {
return Promise
.resolve()
.then(() => transform(content, file.absoluteFrom))
.then((content) => cacache.put(globalRef.cacheDir, cacheKey, content)
.then(() => content));
}
);
}
content = transform(content, file.absoluteFrom);
}
return content;
}).then((content) => {
if (pattern.toType === 'template') {
info(`interpolating template '${file.webpackTo}' for '${file.relativeFrom}'`);
// If it doesn't have an extension, remove it from the pattern
// ie. [name].[ext] or [name][ext] both become [name]
if (!path.extname(file.relativeFrom)) {
file.webpackTo = file.webpackTo.replace(/\.?\[ext\]/g, '');
}
file.webpackTo = loaderUtils.interpolateName(
{resourcePath: file.absoluteFrom},
file.webpackTo,
{
content,
regExp: file.webpackToRegExp,
context: pattern.context
}
);
}
return content;
}).then((content) => {
if (pattern.transformPath) {
return Promise.resolve(
pattern.transformPath(file.webpackTo, file.absoluteFrom)
).then((newPath) => {
file.webpackTo = newPath;
}).then(() => content);
}
return content;
}).then((content) => {
const hash = loaderUtils.getHashDigest(content);
if (!copyUnmodified &&
written[file.absoluteFrom] &&
written[file.absoluteFrom]['hash'] === hash &&
written[file.absoluteFrom]['webpackTo'] === file.webpackTo
) {
info(`skipping '${file.webpackTo}', because it hasn't changed`);
return;
} else {
debug(`added ${hash} to written tracking for '${file.absoluteFrom}'`);
written[file.absoluteFrom] = {
hash: hash,
webpackTo: file.webpackTo
};
}
if (compilation.assets[file.webpackTo] && !file.force) {
info(`skipping '${file.webpackTo}', because it already exists`);
return;
}
info(`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`);
compilation.assets[file.webpackTo] = {
size: function() {
return stat.size;
},
source: function() {
return content;
}
};
});
});
}