writeFile.js
5.24 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
135
136
137
138
139
140
141
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = writeFile;
var _loaderUtils = require('loader-utils');
var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _cacache = require('cacache');
var _cacache2 = _interopRequireDefault(_cacache);
var _serializeJavascript = require('serialize-javascript');
var _serializeJavascript2 = _interopRequireDefault(_serializeJavascript);
var _package = require('../package.json');
var _findCacheDir = require('find-cache-dir');
var _findCacheDir2 = _interopRequireDefault(_findCacheDir);
var _promisify = require('./utils/promisify');
var _crypto = require('crypto');
var _crypto2 = _interopRequireDefault(_crypto);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function writeFile(globalRef, pattern, file) {
var info = globalRef.info,
debug = globalRef.debug,
compilation = globalRef.compilation,
fileDependencies = globalRef.fileDependencies,
written = globalRef.written,
inputFileSystem = globalRef.inputFileSystem,
copyUnmodified = globalRef.copyUnmodified;
return (0, _promisify.stat)(inputFileSystem, file.absoluteFrom).then(function (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 (0, _promisify.readFile)(inputFileSystem, file.absoluteFrom).then(function (content) {
if (pattern.transform) {
var transform = function transform(content, absoluteFrom) {
return pattern.transform(content, absoluteFrom);
};
if (pattern.cache) {
if (!globalRef.cacheDir) {
globalRef.cacheDir = (0, _findCacheDir2.default)({ name: 'copy-webpack-plugin' });
}
var cacheKey = pattern.cache.key ? pattern.cache.key : (0, _serializeJavascript2.default)({
name: _package.name,
version: _package.version,
pattern: pattern,
hash: _crypto2.default.createHash('md4').update(content).digest('hex')
});
return _cacache2.default.get(globalRef.cacheDir, cacheKey).then(function (result) {
return result.data;
}, function () {
return Promise.resolve().then(function () {
return transform(content, file.absoluteFrom);
}).then(function (content) {
return _cacache2.default.put(globalRef.cacheDir, cacheKey, content).then(function () {
return content;
});
});
});
}
content = transform(content, file.absoluteFrom);
}
return content;
}).then(function (content) {
var hash = _loaderUtils2.default.getHashDigest(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 (!_path2.default.extname(file.relativeFrom)) {
file.webpackTo = file.webpackTo.replace(/\.?\[ext\]/g, '');
}
file.webpackTo = _loaderUtils2.default.interpolateName({ resourcePath: file.absoluteFrom }, file.webpackTo, {
content: content,
regExp: file.webpackToRegExp,
context: pattern.context
});
}
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 size() {
return stat.size;
},
source: function source() {
return content;
}
};
});
});
}