source: trip-planner-front/node_modules/copy-webpack-plugin/dist/index.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 22.3 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _path = _interopRequireDefault(require("path"));
9
10var _crypto = _interopRequireDefault(require("crypto"));
11
12var _schemaUtils = require("schema-utils");
13
14var _pLimit = _interopRequireDefault(require("p-limit"));
15
16var _globby = _interopRequireDefault(require("globby"));
17
18var _serializeJavascript = _interopRequireDefault(require("serialize-javascript"));
19
20var _normalizePath = _interopRequireDefault(require("normalize-path"));
21
22var _globParent = _interopRequireDefault(require("glob-parent"));
23
24var _fastGlob = _interopRequireDefault(require("fast-glob"));
25
26var _package = require("../package.json");
27
28var _options = _interopRequireDefault(require("./options.json"));
29
30var _promisify = require("./utils/promisify");
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34const template = /\[\\*([\w:]+)\\*\]/i;
35
36class CopyPlugin {
37 constructor(options = {}) {
38 (0, _schemaUtils.validate)(_options.default, options, {
39 name: "Copy Plugin",
40 baseDataPath: "options"
41 });
42 this.patterns = options.patterns;
43 this.options = options.options || {};
44 }
45
46 static async createSnapshot(compilation, startTime, dependency) {
47 // eslint-disable-next-line consistent-return
48 return new Promise((resolve, reject) => {
49 compilation.fileSystemInfo.createSnapshot(startTime, [dependency], // eslint-disable-next-line no-undefined
50 undefined, // eslint-disable-next-line no-undefined
51 undefined, null, (error, snapshot) => {
52 if (error) {
53 reject(error);
54 return;
55 }
56
57 resolve(snapshot);
58 });
59 });
60 }
61
62 static async checkSnapshotValid(compilation, snapshot) {
63 // eslint-disable-next-line consistent-return
64 return new Promise((resolve, reject) => {
65 compilation.fileSystemInfo.checkSnapshotValid(snapshot, (error, isValid) => {
66 if (error) {
67 reject(error);
68 return;
69 }
70
71 resolve(isValid);
72 });
73 });
74 }
75
76 static getContentHash(compiler, compilation, source) {
77 const {
78 outputOptions
79 } = compilation;
80 const {
81 hashDigest,
82 hashDigestLength,
83 hashFunction,
84 hashSalt
85 } = outputOptions;
86 const hash = compiler.webpack.util.createHash(hashFunction);
87
88 if (hashSalt) {
89 hash.update(hashSalt);
90 }
91
92 hash.update(source);
93 const fullContentHash = hash.digest(hashDigest);
94 return fullContentHash.slice(0, hashDigestLength);
95 }
96
97 static async runPattern(compiler, compilation, logger, cache, inputPattern, index) {
98 const {
99 RawSource
100 } = compiler.webpack.sources;
101 const pattern = typeof inputPattern === "string" ? {
102 from: inputPattern
103 } : { ...inputPattern
104 };
105 pattern.fromOrigin = pattern.from;
106 pattern.from = _path.default.normalize(pattern.from);
107 pattern.context = typeof pattern.context === "undefined" ? compiler.context : _path.default.isAbsolute(pattern.context) ? pattern.context : _path.default.join(compiler.context, pattern.context);
108 logger.log(`starting to process a pattern from '${pattern.from}' using '${pattern.context}' context`);
109
110 if (_path.default.isAbsolute(pattern.from)) {
111 pattern.absoluteFrom = pattern.from;
112 } else {
113 pattern.absoluteFrom = _path.default.resolve(pattern.context, pattern.from);
114 }
115
116 logger.debug(`getting stats for '${pattern.absoluteFrom}'...`);
117 const {
118 inputFileSystem
119 } = compiler;
120 let stats;
121
122 try {
123 stats = await (0, _promisify.stat)(inputFileSystem, pattern.absoluteFrom);
124 } catch (error) {// Nothing
125 }
126
127 if (stats) {
128 if (stats.isDirectory()) {
129 pattern.fromType = "dir";
130 logger.debug(`determined '${pattern.absoluteFrom}' is a directory`);
131 } else if (stats.isFile()) {
132 pattern.fromType = "file";
133 logger.debug(`determined '${pattern.absoluteFrom}' is a file`);
134 } else {
135 logger.debug(`determined '${pattern.absoluteFrom}' is a glob`);
136 }
137 } // eslint-disable-next-line no-param-reassign
138
139
140 pattern.globOptions = { ...{
141 followSymbolicLinks: true
142 },
143 ...(pattern.globOptions || {}),
144 ...{
145 cwd: pattern.context,
146 objectMode: true
147 }
148 };
149 pattern.globOptions.fs = inputFileSystem;
150
151 switch (pattern.fromType) {
152 case "dir":
153 compilation.contextDependencies.add(pattern.absoluteFrom);
154 logger.debug(`added '${pattern.absoluteFrom}' as a context dependency`);
155 /* eslint-disable no-param-reassign */
156
157 pattern.context = pattern.absoluteFrom;
158 pattern.glob = _path.default.posix.join(_fastGlob.default.escapePath((0, _normalizePath.default)(_path.default.resolve(pattern.absoluteFrom))), "**/*");
159 pattern.absoluteFrom = _path.default.join(pattern.absoluteFrom, "**/*");
160
161 if (typeof pattern.globOptions.dot === "undefined") {
162 pattern.globOptions.dot = true;
163 }
164 /* eslint-enable no-param-reassign */
165
166
167 break;
168
169 case "file":
170 compilation.fileDependencies.add(pattern.absoluteFrom);
171 logger.debug(`added '${pattern.absoluteFrom}' as a file dependency`);
172 /* eslint-disable no-param-reassign */
173
174 pattern.context = _path.default.dirname(pattern.absoluteFrom);
175 pattern.glob = _fastGlob.default.escapePath((0, _normalizePath.default)(_path.default.resolve(pattern.absoluteFrom)));
176
177 if (typeof pattern.globOptions.dot === "undefined") {
178 pattern.globOptions.dot = true;
179 }
180 /* eslint-enable no-param-reassign */
181
182
183 break;
184
185 default:
186 {
187 const contextDependencies = _path.default.normalize((0, _globParent.default)(pattern.absoluteFrom));
188
189 compilation.contextDependencies.add(contextDependencies);
190 logger.debug(`added '${contextDependencies}' as a context dependency`);
191 /* eslint-disable no-param-reassign */
192
193 pattern.fromType = "glob";
194 pattern.glob = _path.default.isAbsolute(pattern.fromOrigin) ? pattern.fromOrigin : _path.default.posix.join(_fastGlob.default.escapePath((0, _normalizePath.default)(_path.default.resolve(pattern.context))), pattern.fromOrigin);
195 /* eslint-enable no-param-reassign */
196 }
197 }
198
199 logger.log(`begin globbing '${pattern.glob}'...`);
200 let paths;
201
202 try {
203 paths = await (0, _globby.default)(pattern.glob, pattern.globOptions);
204 } catch (error) {
205 compilation.errors.push(error);
206 return;
207 }
208
209 if (paths.length === 0) {
210 if (pattern.noErrorOnMissing) {
211 logger.log(`finished to process a pattern from '${pattern.from}' using '${pattern.context}' context to '${pattern.to}'`);
212 return;
213 }
214
215 const missingError = new Error(`unable to locate '${pattern.glob}' glob`);
216 compilation.errors.push(missingError);
217 return;
218 }
219
220 const filteredPaths = (await Promise.all(paths.map(async item => {
221 // Exclude directories
222 if (!item.dirent.isFile()) {
223 return false;
224 }
225
226 if (pattern.filter) {
227 let isFiltered;
228
229 try {
230 isFiltered = await pattern.filter(item.path);
231 } catch (error) {
232 compilation.errors.push(error);
233 return false;
234 }
235
236 if (!isFiltered) {
237 logger.log(`skip '${item.path}', because it was filtered`);
238 }
239
240 return isFiltered ? item : false;
241 }
242
243 return item;
244 }))).filter(item => item);
245
246 if (filteredPaths.length === 0) {
247 if (pattern.noErrorOnMissing) {
248 logger.log(`finished to process a pattern from '${pattern.from}' using '${pattern.context}' context to '${pattern.to}'`);
249 return;
250 }
251
252 const missingError = new Error(`unable to locate '${pattern.glob}' glob after filtering paths`);
253 compilation.errors.push(missingError);
254 return;
255 }
256
257 const files = await Promise.all(filteredPaths.map(async item => {
258 const from = item.path;
259 logger.debug(`found '${from}'`); // `globby`/`fast-glob` return the relative path when the path contains special characters on windows
260
261 const absoluteFilename = _path.default.resolve(pattern.context, from);
262
263 pattern.to = typeof pattern.to === "function" ? await pattern.to({
264 context: pattern.context,
265 absoluteFilename
266 }) : _path.default.normalize(typeof pattern.to !== "undefined" ? pattern.to : "");
267
268 const isToDirectory = _path.default.extname(pattern.to) === "" || pattern.to.slice(-1) === _path.default.sep;
269
270 const toType = pattern.toType ? pattern.toType : template.test(pattern.to) ? "template" : isToDirectory ? "dir" : "file";
271 logger.log(`'to' option '${pattern.to}' determinated as '${toType}'`);
272
273 const relativeFrom = _path.default.relative(pattern.context, absoluteFilename);
274
275 let filename = toType === "dir" ? _path.default.join(pattern.to, relativeFrom) : pattern.to;
276
277 if (_path.default.isAbsolute(filename)) {
278 filename = _path.default.relative(compiler.options.output.path, filename);
279 }
280
281 logger.log(`determined that '${from}' should write to '${filename}'`);
282 const sourceFilename = (0, _normalizePath.default)(_path.default.relative(compiler.context, absoluteFilename));
283 return {
284 absoluteFilename,
285 sourceFilename,
286 filename,
287 toType
288 };
289 }));
290 let assets;
291
292 try {
293 assets = await Promise.all(files.map(async file => {
294 const {
295 absoluteFilename,
296 sourceFilename,
297 filename,
298 toType
299 } = file;
300 const info = typeof pattern.info === "function" ? pattern.info(file) || {} : pattern.info || {};
301 const result = {
302 absoluteFilename,
303 sourceFilename,
304 filename,
305 force: pattern.force,
306 info
307 }; // If this came from a glob or dir, add it to the file dependencies
308
309 if (pattern.fromType === "dir" || pattern.fromType === "glob") {
310 compilation.fileDependencies.add(absoluteFilename);
311 logger.debug(`added '${absoluteFilename}' as a file dependency`);
312 }
313
314 let cacheEntry;
315 logger.debug(`getting cache for '${absoluteFilename}'...`);
316
317 try {
318 cacheEntry = await cache.getPromise(`${sourceFilename}|${index}`, null);
319 } catch (error) {
320 compilation.errors.push(error);
321 return;
322 }
323
324 if (cacheEntry) {
325 logger.debug(`found cache for '${absoluteFilename}'...`);
326 let isValidSnapshot;
327 logger.debug(`checking snapshot on valid for '${absoluteFilename}'...`);
328
329 try {
330 isValidSnapshot = await CopyPlugin.checkSnapshotValid(compilation, cacheEntry.snapshot);
331 } catch (error) {
332 compilation.errors.push(error);
333 return;
334 }
335
336 if (isValidSnapshot) {
337 logger.debug(`snapshot for '${absoluteFilename}' is valid`);
338 result.source = cacheEntry.source;
339 } else {
340 logger.debug(`snapshot for '${absoluteFilename}' is invalid`);
341 }
342 } else {
343 logger.debug(`missed cache for '${absoluteFilename}'`);
344 }
345
346 if (!result.source) {
347 const startTime = Date.now();
348 logger.debug(`reading '${absoluteFilename}'...`);
349 let data;
350
351 try {
352 data = await (0, _promisify.readFile)(inputFileSystem, absoluteFilename);
353 } catch (error) {
354 compilation.errors.push(error);
355 return;
356 }
357
358 logger.debug(`read '${absoluteFilename}'`);
359 result.source = new RawSource(data);
360 let snapshot;
361 logger.debug(`creating snapshot for '${absoluteFilename}'...`);
362
363 try {
364 snapshot = await CopyPlugin.createSnapshot(compilation, startTime, absoluteFilename);
365 } catch (error) {
366 compilation.errors.push(error);
367 return;
368 }
369
370 if (snapshot) {
371 logger.debug(`created snapshot for '${absoluteFilename}'`);
372 logger.debug(`storing cache for '${absoluteFilename}'...`);
373
374 try {
375 await cache.storePromise(`${sourceFilename}|${index}`, null, {
376 source: result.source,
377 snapshot
378 });
379 } catch (error) {
380 compilation.errors.push(error);
381 return;
382 }
383
384 logger.debug(`stored cache for '${absoluteFilename}'`);
385 }
386 }
387
388 if (pattern.transform) {
389 const transform = typeof pattern.transform === "function" ? {
390 transformer: pattern.transform
391 } : pattern.transform;
392
393 if (transform.transformer) {
394 logger.log(`transforming content for '${absoluteFilename}'...`);
395 const buffer = result.source.buffer();
396
397 if (transform.cache) {
398 const defaultCacheKeys = {
399 version: _package.version,
400 sourceFilename,
401 transform: transform.transformer,
402 contentHash: _crypto.default.createHash("md4").update(buffer).digest("hex"),
403 index
404 };
405 const cacheKeys = `transform|${(0, _serializeJavascript.default)(typeof transform.cache.keys === "function" ? await transform.cache.keys(defaultCacheKeys, absoluteFilename) : { ...defaultCacheKeys,
406 ...pattern.transform.cache.keys
407 })}`;
408 logger.debug(`getting transformation cache for '${absoluteFilename}'...`);
409 const cacheItem = cache.getItemCache(cacheKeys, cache.getLazyHashedEtag(result.source));
410 result.source = await cacheItem.getPromise();
411 logger.debug(result.source ? `found transformation cache for '${absoluteFilename}'` : `no transformation cache for '${absoluteFilename}'`);
412
413 if (!result.source) {
414 const transformed = await transform.transformer(buffer, absoluteFilename);
415 result.source = new RawSource(transformed);
416 logger.debug(`caching transformation for '${absoluteFilename}'...`);
417 await cacheItem.storePromise(result.source);
418 logger.debug(`cached transformation for '${absoluteFilename}'`);
419 }
420 } else {
421 result.source = new RawSource(await transform.transformer(buffer, absoluteFilename));
422 }
423 }
424 }
425
426 if (toType === "template") {
427 logger.log(`interpolating template '${filename}' for '${sourceFilename}'...`);
428 const contentHash = CopyPlugin.getContentHash(compiler, compilation, result.source.buffer());
429
430 const ext = _path.default.extname(result.sourceFilename);
431
432 const base = _path.default.basename(result.sourceFilename);
433
434 const name = base.slice(0, base.length - ext.length);
435 const data = {
436 filename: (0, _normalizePath.default)(_path.default.relative(pattern.context, absoluteFilename)),
437 contentHash,
438 chunk: {
439 name,
440 id: result.sourceFilename,
441 hash: contentHash,
442 contentHash
443 }
444 };
445 const {
446 path: interpolatedFilename,
447 info: assetInfo
448 } = compilation.getPathWithInfo((0, _normalizePath.default)(result.filename), data);
449 result.info = { ...result.info,
450 ...assetInfo
451 };
452 result.filename = interpolatedFilename;
453 logger.log(`interpolated template '${filename}' for '${sourceFilename}'`);
454 } else {
455 // eslint-disable-next-line no-param-reassign
456 result.filename = (0, _normalizePath.default)(result.filename);
457 } // eslint-disable-next-line consistent-return
458
459
460 return result;
461 }));
462 } catch (error) {
463 compilation.errors.push(error);
464 return;
465 }
466
467 logger.log(`finished to process a pattern from '${pattern.from}' using '${pattern.context}' context to '${pattern.to}'`); // eslint-disable-next-line consistent-return
468
469 return assets;
470 }
471
472 apply(compiler) {
473 const pluginName = this.constructor.name;
474 const limit = (0, _pLimit.default)(this.options.concurrency || 100);
475 compiler.hooks.thisCompilation.tap(pluginName, compilation => {
476 const logger = compilation.getLogger("copy-webpack-plugin");
477 const cache = compilation.getCache("CopyWebpackPlugin");
478 compilation.hooks.processAssets.tapAsync({
479 name: "copy-webpack-plugin",
480 stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
481 }, async (unusedAssets, callback) => {
482 logger.log("starting to add additional assets...");
483 const assetMap = new Map();
484 await Promise.all(this.patterns.map((item, index) => limit(async () => {
485 let assets;
486
487 try {
488 assets = await CopyPlugin.runPattern(compiler, compilation, logger, cache, item, index);
489 } catch (error) {
490 compilation.errors.push(error);
491 return;
492 }
493
494 if (assets && assets.length > 0) {
495 if (item.transformAll) {
496 if (typeof item.to === "undefined") {
497 compilation.errors.push(new Error(`Invalid "pattern.to" for the "pattern.from": "${item.from}" and "pattern.transformAll" function. The "to" option must be specified.`));
498 return;
499 }
500
501 assets.sort((a, b) => a.absoluteFilename > b.absoluteFilename ? 1 : a.absoluteFilename < b.absoluteFilename ? -1 : 0);
502 const mergedEtag = assets.length === 1 ? cache.getLazyHashedEtag(assets[0].source.buffer()) : assets.reduce((accumulator, asset, i) => {
503 // eslint-disable-next-line no-param-reassign
504 accumulator = cache.mergeEtags(i === 1 ? cache.getLazyHashedEtag(accumulator.source.buffer()) : accumulator, cache.getLazyHashedEtag(asset.source.buffer()));
505 return accumulator;
506 });
507 const cacheKeys = `transformAll|${(0, _serializeJavascript.default)({
508 version: _package.version,
509 from: item.from,
510 to: item.to,
511 transformAll: item.transformAll
512 })}`;
513 const eTag = cache.getLazyHashedEtag(mergedEtag);
514 const cacheItem = cache.getItemCache(cacheKeys, eTag);
515 let transformedAsset = await cacheItem.getPromise();
516
517 if (!transformedAsset) {
518 transformedAsset = {
519 filename: item.to
520 };
521
522 try {
523 transformedAsset.data = await item.transformAll(assets.map(asset => {
524 return {
525 data: asset.source.buffer(),
526 sourceFilename: asset.sourceFilename,
527 absoluteFilename: asset.absoluteFilename
528 };
529 }));
530 } catch (error) {
531 compilation.errors.push(error);
532 return;
533 }
534
535 if (template.test(item.to)) {
536 const contentHash = CopyPlugin.getContentHash(compiler, compilation, transformedAsset.data);
537 const {
538 path: interpolatedFilename,
539 info: assetInfo
540 } = compilation.getPathWithInfo((0, _normalizePath.default)(item.to), {
541 contentHash,
542 chunk: {
543 hash: contentHash,
544 contentHash
545 }
546 });
547 transformedAsset.filename = interpolatedFilename;
548 transformedAsset.info = assetInfo;
549 }
550
551 const {
552 RawSource
553 } = compiler.webpack.sources;
554 transformedAsset.source = new RawSource(transformedAsset.data);
555 transformedAsset.force = item.force;
556 await cacheItem.storePromise(transformedAsset);
557 }
558
559 assets = [transformedAsset];
560 }
561
562 const priority = item.priority || 0;
563
564 if (!assetMap.has(priority)) {
565 assetMap.set(priority, []);
566 }
567
568 assetMap.get(priority).push(...assets);
569 }
570 })));
571 const assets = [...assetMap.entries()].sort((a, b) => a[0] - b[0]); // Avoid writing assets inside `p-limit`, because it creates concurrency.
572 // It could potentially lead to an error - 'Multiple assets emit different content to the same filename'
573
574 assets.reduce((acc, val) => acc.concat(val[1]), []).filter(Boolean).forEach(asset => {
575 const {
576 absoluteFilename,
577 sourceFilename,
578 filename,
579 source,
580 force
581 } = asset;
582 const existingAsset = compilation.getAsset(filename);
583
584 if (existingAsset) {
585 if (force) {
586 const info = {
587 copied: true,
588 sourceFilename
589 };
590 logger.log(`force updating '${filename}' from '${absoluteFilename}' to compilation assets, because it already exists...`);
591 compilation.updateAsset(filename, source, { ...info,
592 ...asset.info
593 });
594 logger.log(`force updated '${filename}' from '${absoluteFilename}' to compilation assets, because it already exists`);
595 return;
596 }
597
598 logger.log(`skip adding '${filename}' from '${absoluteFilename}' to compilation assets, because it already exists`);
599 return;
600 }
601
602 const info = {
603 copied: true,
604 sourceFilename
605 };
606 logger.log(`writing '${filename}' from '${absoluteFilename}' to compilation assets...`);
607 compilation.emitAsset(filename, source, { ...info,
608 ...asset.info
609 });
610 logger.log(`written '${filename}' from '${absoluteFilename}' to compilation assets`);
611 });
612 logger.log("finished to adding additional assets");
613 callback();
614 });
615
616 if (compilation.hooks.statsPrinter) {
617 compilation.hooks.statsPrinter.tap(pluginName, stats => {
618 stats.hooks.print.for("asset.info.copied").tap("copy-webpack-plugin", (copied, {
619 green,
620 formatFlag
621 }) => // eslint-disable-next-line no-undefined
622 copied ? green(formatFlag("copied")) : undefined);
623 });
624 }
625 });
626 }
627
628}
629
630var _default = CopyPlugin;
631exports.default = _default;
Note: See TracBrowser for help on using the repository browser.