/
index.js
254 lines (219 loc) · 6.55 KB
/
index.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* External dependencies
*/
const { createHash } = require( 'crypto' );
const path = require( 'path' );
const webpack = require( 'webpack' );
// In webpack 5 there is a `webpack.sources` field but for webpack 4 we have to fallback to the `webpack-sources` package.
const { RawSource } = webpack.sources || require( 'webpack-sources' );
const json2php = require( 'json2php' );
const isWebpack4 = webpack.version.startsWith( '4.' );
/**
* Internal dependencies
*/
const {
defaultRequestToExternal,
defaultRequestToHandle,
} = require( './util' );
class DependencyExtractionWebpackPlugin {
constructor( options ) {
this.options = Object.assign(
{
combineAssets: false,
combinedOutputFile: null,
injectPolyfill: false,
outputFormat: 'php',
useDefaults: true,
},
options
);
/*
* Track requests that are externalized.
*
* Because we don't have a closed set of dependencies, we need to track what has
* been externalized so we can recognize them in a later phase when the dependency
* lists are generated.
*/
this.externalizedDeps = new Set();
// Offload externalization work to the ExternalsPlugin.
this.externalsPlugin = new webpack.ExternalsPlugin(
'window',
isWebpack4
? this.externalizeWpDeps.bind( this )
: this.externalizeWpDepsV5.bind( this )
);
}
externalizeWpDeps( _context, request, callback ) {
let externalRequest;
// Handle via options.requestToExternal first
if ( typeof this.options.requestToExternal === 'function' ) {
externalRequest = this.options.requestToExternal( request );
}
// Cascade to default if unhandled and enabled
if (
typeof externalRequest === 'undefined' &&
this.options.useDefaults
) {
externalRequest = defaultRequestToExternal( request );
}
if ( externalRequest ) {
this.externalizedDeps.add( request );
return callback( null, externalRequest );
}
return callback();
}
externalizeWpDepsV5( { context, request }, callback ) {
return this.externalizeWpDeps( context, request, callback );
}
mapRequestToDependency( request ) {
// Handle via options.requestToHandle first
if ( typeof this.options.requestToHandle === 'function' ) {
const scriptDependency = this.options.requestToHandle( request );
if ( scriptDependency ) {
return scriptDependency;
}
}
// Cascade to default if enabled
if ( this.options.useDefaults ) {
const scriptDependency = defaultRequestToHandle( request );
if ( scriptDependency ) {
return scriptDependency;
}
}
// Fall back to the request name
return request;
}
stringify( asset ) {
if ( this.options.outputFormat === 'php' ) {
return `<?php return ${ json2php(
JSON.parse( JSON.stringify( asset ) )
) };`;
}
return JSON.stringify( asset );
}
apply( compiler ) {
this.externalsPlugin.apply( compiler );
if ( isWebpack4 ) {
compiler.hooks.emit.tap( this.constructor.name, ( compilation ) =>
this.addAssets( compilation, compiler )
);
} else {
compiler.hooks.thisCompilation.tap(
this.constructor.name,
( compilation ) => {
compilation.hooks.processAssets.tap(
{
name: this.constructor.name,
stage:
compiler.webpack.Compilation
.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
() => this.addAssets( compilation, compiler )
);
}
);
}
}
addAssets( compilation, compiler ) {
const {
combineAssets,
combinedOutputFile,
injectPolyfill,
outputFormat,
} = this.options;
const combinedAssetsData = {};
// Process each entry point independently.
for ( const [
entrypointName,
entrypoint,
] of compilation.entrypoints.entries() ) {
const entrypointExternalizedWpDeps = new Set();
if ( injectPolyfill ) {
entrypointExternalizedWpDeps.add( 'wp-polyfill' );
}
const processModule = ( { userRequest } ) => {
if ( this.externalizedDeps.has( userRequest ) ) {
const scriptDependency = this.mapRequestToDependency(
userRequest
);
entrypointExternalizedWpDeps.add( scriptDependency );
}
};
// Search for externalized modules in all chunks.
for ( const chunk of entrypoint.chunks ) {
const modulesIterable = isWebpack4
? chunk.modulesIterable
: compilation.chunkGraph.getChunkModules( chunk );
for ( const chunkModule of modulesIterable ) {
processModule( chunkModule );
// loop through submodules of ConcatenatedModule
if ( chunkModule.modules ) {
for ( const concatModule of chunkModule.modules ) {
processModule( concatModule );
}
}
}
}
const runtimeChunk = entrypoint.getRuntimeChunk();
const assetData = {
// Get a sorted array so we can produce a stable, stringified representation.
dependencies: Array.from( entrypointExternalizedWpDeps ).sort(),
version: runtimeChunk.hash,
};
const assetString = this.stringify( assetData );
// Determine a filename for the asset file.
const [ filename, query ] = entrypointName.split( '?', 2 );
const buildFilename = compilation.getPath(
compiler.options.output.filename,
{
chunk: runtimeChunk,
filename,
query,
basename: basename( filename ),
contentHash: createHash( 'md4' )
.update( assetString )
.digest( 'hex' ),
}
);
if ( combineAssets ) {
combinedAssetsData[ buildFilename ] = assetData;
continue;
}
const assetFilename = buildFilename.replace(
/\.js$/i,
'.asset.' + ( outputFormat === 'php' ? 'php' : 'json' )
);
// Add source and file into compilation for webpack to output.
compilation.assets[ assetFilename ] = new RawSource( assetString );
runtimeChunk.files[ isWebpack4 ? 'push' : 'add' ]( assetFilename );
}
if ( combineAssets ) {
// Assert the `string` type for output path.
// The type indicates the option may be `undefined`.
// However, at this point in compilation, webpack has filled the options in if
// they were not provided.
const outputFolder = /** @type {{path:string}} */ ( compiler.options
.output ).path;
const assetsFilePath = path.resolve(
outputFolder,
combinedOutputFile ||
'assets.' + ( outputFormat === 'php' ? 'php' : 'json' )
);
const assetsFilename = path.relative(
outputFolder,
assetsFilePath
);
// Add source into compilation for webpack to output.
compilation.assets[ assetsFilename ] = new RawSource(
this.stringify( combinedAssetsData )
);
}
}
}
function basename( name ) {
if ( ! name.includes( '/' ) ) {
return name;
}
return name.substr( name.lastIndexOf( '/' ) + 1 );
}
module.exports = DependencyExtractionWebpackPlugin;