@@ -10,93 +10,87 @@ class PolyfillInjectorPlugin {
10
10
}
11
11
12
12
apply ( compiler ) {
13
- compiler . plugin ( 'invalid ', ( ) => {
13
+ compiler . hooks . invalid . tap ( 'webpack-polyfill-injector ', ( ) => {
14
14
compiler . __WebpackPolyfillInjectorInWatchRun = true ;
15
15
} ) ;
16
16
17
- compiler . plugin ( 'this-compilation ', ( compilation ) => {
17
+ compiler . hooks . thisCompilation . tap ( 'webpack-polyfill-injector ', ( compilation ) => {
18
18
const pluginState = new PluginState ( compilation , this . options ) ;
19
19
compilation . __WebpackPolyfillInjector = pluginState ;
20
20
21
- compilation . plugin ( 'additional-assets' , async ( callback ) => {
22
- try {
23
- if ( compiler . __WebpackPolyfillInjectorInWatchRun ) {
24
- // Nothing to do for successive compilations
25
- callback ( ) ;
26
- return ;
27
- }
21
+ compilation . hooks . additionalAssets . tapPromise ( 'webpack-polyfill-injector' , async ( ) => {
22
+ if ( compiler . __WebpackPolyfillInjectorInWatchRun ) {
23
+ // Nothing to do for successive compilations
24
+ return ;
25
+ }
28
26
29
- if ( ! pluginState . hasLoader ) {
30
- throw new Error ( '[webpack-polyfill-injector] The plugin must be used together with the loader!' ) ;
31
- }
27
+ if ( ! pluginState . hasLoader ) {
28
+ throw new Error ( '[webpack-polyfill-injector] The plugin must be used together with the loader!' ) ;
29
+ }
32
30
33
- // Create the additional assets
34
- await pluginState . iteratePolyfillSets (
35
- async ( { polyfills, singleFile, banner} , filename ) => {
36
- // Load all polyfill sources and meta data
37
- const tasks = polyfills . map ( polyfill => pluginState . getPolyfillSource ( polyfill ) ) ;
38
- tasks . push ( ...polyfills . map ( polyfill => pluginState . getPolyfillMeta ( polyfill ) ) ) ;
31
+ // Create the additional assets
32
+ await pluginState . iteratePolyfillSets (
33
+ async ( { polyfills, singleFile, banner} , filename ) => {
34
+ // Load all polyfill sources and meta data
35
+ const tasks = polyfills . map ( polyfill => pluginState . getPolyfillSource ( polyfill ) ) ;
36
+ tasks . push ( ...polyfills . map ( polyfill => pluginState . getPolyfillMeta ( polyfill ) ) ) ;
39
37
40
- // Run all tasks and split the results into their appropriate arrays
41
- const polyfillSources = await Promise . all ( tasks ) ;
42
- const polyfillMetas = polyfillSources . splice ( polyfills . length ) ;
38
+ // Run all tasks and split the results into their appropriate arrays
39
+ const polyfillSources = await Promise . all ( tasks ) ;
40
+ const polyfillMetas = polyfillSources . splice ( polyfills . length ) ;
43
41
44
- // Collect internal dependencies (i.e. polyfills whose names start with underscore)
45
- const dependencySources = { } ;
46
- async function resolveDependencies ( dependencies , fetched ) {
47
- const deps = dependencies . filter ( d => d . startsWith ( '_' ) && ! fetched . includes ( d ) ) ;
48
- const tasks = deps . map ( dep => pluginState . getPolyfillSource ( dep ) ) ;
49
- tasks . push ( ...deps . map ( dep => pluginState . getPolyfillMeta ( dep ) ) ) ;
50
- const depsSources = await Promise . all ( tasks ) ;
51
- const depsMetas = depsSources . splice ( deps . length ) ;
52
- deps . forEach ( ( dep , i ) => {
53
- dependencySources [ dep ] = depsSources [ i ] ;
54
- } ) ;
55
- fetched . push ( ...deps ) ;
56
- const recursiveDeps = await Promise . all (
57
- depsMetas . map ( meta => resolveDependencies ( meta . dependencies || [ ] , fetched ) )
58
- ) ;
59
- return [ ] . concat ( deps , ...recursiveDeps ) ;
60
- }
61
- const polyfillDependencies = await Promise . all (
62
- polyfillMetas . map ( meta => resolveDependencies ( meta . dependencies || [ ] , [ ] ) )
42
+ // Collect internal dependencies (i.e. polyfills whose names start with underscore)
43
+ const dependencySources = { } ;
44
+ async function resolveDependencies ( dependencies , fetched ) {
45
+ const deps = dependencies . filter ( d => d . startsWith ( '_' ) && ! fetched . includes ( d ) ) ;
46
+ const tasks = deps . map ( dep => pluginState . getPolyfillSource ( dep ) ) ;
47
+ tasks . push ( ...deps . map ( dep => pluginState . getPolyfillMeta ( dep ) ) ) ;
48
+ const depsSources = await Promise . all ( tasks ) ;
49
+ const depsMetas = depsSources . splice ( deps . length ) ;
50
+ deps . forEach ( ( dep , i ) => {
51
+ dependencySources [ dep ] = depsSources [ i ] ;
52
+ } ) ;
53
+ fetched . push ( ...deps ) ;
54
+ const recursiveDeps = await Promise . all (
55
+ depsMetas . map ( meta => resolveDependencies ( meta . dependencies || [ ] , fetched ) )
63
56
) ;
57
+ return [ ] . concat ( deps , ...recursiveDeps ) ;
58
+ }
59
+ const polyfillDependencies = await Promise . all (
60
+ polyfillMetas . map ( meta => resolveDependencies ( meta . dependencies || [ ] , [ ] ) )
61
+ ) ;
64
62
65
- if ( singleFile ) {
66
- // Create one file containing all requested polyfills
67
- compilation . assets [ filename ] = constructFile (
68
- polyfills . length > 1 ,
63
+ if ( singleFile ) {
64
+ // Create one file containing all requested polyfills
65
+ compilation . assets [ filename ] = constructFile (
66
+ polyfills . length > 1 ,
67
+ banner ,
68
+ polyfills ,
69
+ polyfillSources ,
70
+ polyfillMetas . map ( meta => meta . detectSource ) ,
71
+ polyfillDependencies ,
72
+ dependencySources
73
+ ) ;
74
+ addAsChunk ( filename , compilation ) ;
75
+ } else {
76
+ // Create one file for each possible subset of polyfills
77
+ const choices = Math . pow ( 2 , polyfills . length ) ;
78
+ for ( let choiceId = 1 ; choiceId < choices ; choiceId ++ ) {
79
+ const choice = choiceId . toString ( 2 ) . padStart ( polyfills . length , '0' ) ;
80
+ const outputFile = filename . replace ( / \. j s $ / , '.' ) + choice + '.js' ;
81
+ compilation . assets [ outputFile ] = constructFileWithoutTests (
69
82
banner ,
70
83
polyfills ,
71
84
polyfillSources ,
72
- polyfillMetas . map ( meta => meta . detectSource ) ,
73
85
polyfillDependencies ,
74
- dependencySources
86
+ dependencySources ,
87
+ choice
75
88
) ;
76
- addAsChunk ( filename , compilation ) ;
77
- } else {
78
- // Create one file for each possible subset of polyfills
79
- const choices = Math . pow ( 2 , polyfills . length ) ;
80
- for ( let choiceId = 1 ; choiceId < choices ; choiceId ++ ) {
81
- const choice = choiceId . toString ( 2 ) . padStart ( polyfills . length , '0' ) ;
82
- const outputFile = filename . replace ( / \. j s $ / , '.' ) + choice + '.js' ;
83
- compilation . assets [ outputFile ] = constructFileWithoutTests (
84
- banner ,
85
- polyfills ,
86
- polyfillSources ,
87
- polyfillDependencies ,
88
- dependencySources ,
89
- choice
90
- ) ;
91
- addAsChunk ( outputFile , compilation ) ;
92
- }
89
+ addAsChunk ( outputFile , compilation ) ;
93
90
}
94
91
}
95
- ) ;
96
- callback ( ) ; // eslint-disable-line callback-return
97
- } catch ( error ) {
98
- callback ( error ) ; // eslint-disable-line callback-return
99
- }
92
+ }
93
+ ) ;
100
94
} ) ;
101
95
} ) ;
102
96
}
0 commit comments