This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
getEntryOutputPathFromStats.ts
182 lines (147 loc) · 5.04 KB
/
getEntryOutputPathFromStats.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2018 Steven Enten. All rights reserved. Licensed under the MIT license.
import { join } from 'path';
import * as webpack from 'webpack';
export type GetEntryOutputPathFromStatsInput = webpack.Stats | WebpackStatsJson;
export interface GetEntryOutputPathOptions {
entryName?: string;
outputPath?: string;
}
export interface WebpackCompilationEntrypoint {
name: string;
chunks: WebpackCompilationEntrypointChunk[];
}
export interface WebpackCompilationEntrypointsV3 {
[name: string]: WebpackCompilationEntrypoint;
}
export interface WebpackCompilationEntrypointsV4 extends Map<string, WebpackCompilationEntrypoint> {
}
export interface WebpackCompilationEntrypointChunk {
name: string;
files: string[];
}
export interface WebpackStatsJson {
assetsByChunkName?: { [key: string]: string[]; };
assets?: WebpackStatsJsonAsset[];
chunks?: WebpackStatsJsonChunk[];
entrypoints?: WebpackStatsJsonEntrypoints;
[key: string]: any; // tslint:disable-line:no-any
}
export interface WebpackStatsJsonAsset {
name: string;
chunkNames: string[];
}
export interface WebpackStatsJsonEntrypoints {
[key: string]: WebpackStatsJsonEntrypoint;
}
export interface WebpackStatsJsonEntrypoint {
chunks: string[];
}
export interface WebpackStatsJsonChunk {
id: string;
files: string[];
names: string[];
}
// re-écrire plus simplement
export function getEntryOutputPathFromStats(
stats: GetEntryOutputPathFromStatsInput,
entryNameOrOptions?: string | GetEntryOutputPathOptions,
): string|null {
const options: GetEntryOutputPathOptions = typeof entryNameOrOptions === 'string'
? { entryName: entryNameOrOptions }
: entryNameOrOptions || {};
let { entryName, outputPath } = options;
let entryFiles: string[]|null = null;
let entryOutputPath: string;
// try get entry files from compilation
if ('compilation' in stats && (stats as webpack.Stats).compilation) {
const { compilation } = stats as webpack.Stats;
const { entrypoints } = compilation;
let entry: WebpackCompilationEntrypoint | undefined;
// Compatibility note:
// * in webpack4, entrypoints is a Map object
// * in webpack3, entrypoints is a plain object
if (typeof entrypoints.get === 'function') {
const entrypoinsMap = entrypoints as WebpackCompilationEntrypointsV4;
entry = entryName
? entrypoinsMap.get(entryName)
: entrypoinsMap.get('main')
|| entrypoinsMap.get('index')
|| entrypoinsMap.get('server');
} else {
const entrypointsObj = (entrypoints as object) as WebpackCompilationEntrypointsV3;
entry = entryName
? entrypointsObj[entryName]
: entrypointsObj.main
|| entrypointsObj.index
|| entrypointsObj.server;
}
if (!outputPath && compilation.outputOptions) {
outputPath = compilation.outputOptions.path;
}
if (entry) {
const entryNameFound = entry.name;
const entryChunk = entry.chunks.find(chunk => chunk.name === entryNameFound);
if (entryChunk) {
entryFiles = entryChunk.files;
}
}
}
// try get entry files from stats json entrypoints and chunks
if (!entryFiles && 'chunks' in stats && 'entrypoints' in stats) {
const { chunks, entrypoints } = stats as {} as {
chunks: WebpackStatsJsonChunk[],
entrypoints: WebpackStatsJsonEntrypoints,
};
if (!entryName) {
entryName = entrypoints.main
? 'main'
: entrypoints.index
? 'index'
: entrypoints.server
? 'server'
: undefined;
}
if (entryName) {
const entry = entrypoints[entryName];
entryFiles = entry.chunks
.map((chunkId: string) => {
return chunks.find(chunk => chunk.id === chunkId);
})
.filter(chunk => {
return chunk && chunk.names && ~chunk.names.indexOf(entryName as string);
})
.reduce((acc, chunk) => {
return acc.concat(chunk && chunk.files || []);
}, [] as string[]);
}
}
// try get entry files from stats json assetsByChunkName
if (!entryFiles && 'assetsByChunkName' in stats && stats.assetsByChunkName) {
const { assetsByChunkName } = stats;
entryFiles = entryName
? assetsByChunkName[entryName]
: assetsByChunkName.main
|| assetsByChunkName.index
|| assetsByChunkName.server;
}
// try get entry files from stats json assets
if (!entryFiles && 'assets' in stats && stats.assets) {
const { assets } = stats;
entryFiles = ([] as string[])
.concat(entryName || ['main', 'index', 'server'])
.map((chunkName) => {
return assets.filter(asset => ~asset.chunkNames.indexOf(chunkName));
})
.reduce((acc, value) => {
return acc.concat(value.map(asset => asset.name));
}, [] as string[]);
}
entryOutputPath = '';
if (entryFiles) {
entryOutputPath = entryFiles.find((file) => file.endsWith('.js')) || '';
}
if (entryOutputPath && outputPath) {
entryOutputPath = join(outputPath, entryOutputPath);
}
return entryOutputPath ? entryOutputPath : null;
}