UNPKG

1.92 kBJavaScriptView Raw
1const toSafePath = require('./safe-path');
2const getWebpackModuleName = require('./module-name');
3
4let uniqueId = 1;
5module.exports = function getChunkSources(chunk, compilation) {
6 if (chunk.isEmpty()) {
7 const emptyId = uniqueId;
8 uniqueId += 1;
9 return [
10 {
11 path: `__empty_${emptyId}__`,
12 src: '',
13 },
14 ];
15 }
16
17 const getModuleSrcObject = (webpackModule) => {
18 const modulePath = getWebpackModuleName(webpackModule);
19 let src = '';
20 let sourceMap = null;
21 if (/javascript/.test(webpackModule.type)) {
22 try {
23 const souceAndMap = webpackModule
24 .source(compilation.dependencyTemplates, compilation.runtimeTemplate)
25 .sourceAndMap();
26 src = souceAndMap.source;
27 if (souceAndMap.map) {
28 sourceMap = souceAndMap.map;
29 }
30 } catch (e) {
31 compilation.errors.push(e);
32 }
33 }
34
35 return {
36 path: toSafePath(modulePath),
37 src,
38 sourceMap,
39 webpackId:
40 webpackModule.id !== null &&
41 webpackModule.id !== undefined && // eslint-disable-line no-undefined
42 webpackModule.id.toString().length > 0
43 ? `${webpackModule.id}`
44 : null,
45 };
46 };
47
48 const getChunkModuleSources = (chunkModules, webpackModule) => {
49 const moduleDeps =
50 webpackModule.type === 'multi entry'
51 ? webpackModule.dependencies
52 : [webpackModule];
53
54 // Multi entry modules have no userRequest or id, but they do have multiple
55 // nested dependencies. Traverse all of them.
56 moduleDeps.forEach((subModule) => {
57 chunkModules.push(getModuleSrcObject(subModule));
58 });
59
60 return chunkModules;
61 };
62
63 return chunk
64 .getModules()
65 .reduce(getChunkModuleSources, [])
66 .filter(
67 (moduleJson) =>
68 !(
69 moduleJson.path === '__unknown__' &&
70 moduleJson.src === '/* (ignored) */'
71 )
72 );
73};