14
14
* limitations under the License.
15
15
*/
16
16
const util = require ( 'util' ) ;
17
+ const CommonJsRequireDependency = require ( "webpack/lib/dependencies/CommonJsRequireDependency" ) ;
17
18
18
19
function containsModule ( chunk , module ) {
19
20
if ( chunk . containsModule ) {
@@ -27,43 +28,56 @@ module.exports = class DojoLoaderEnsurePlugin {
27
28
constructor ( options ) {
28
29
this . options = options ;
29
30
}
30
- apply ( compilation ) {
31
- // Ensure that the Dojo loader, and optionally the loader config, are included
32
- // in the entry chunks, and only the entry chunks.
33
- compilation . plugin ( "after-optimize-chunks" , ( chunks ) => {
34
- // Get the loader and loader config
35
- const loaderModule = compilation . modules . find ( ( module ) => { return module . rawRequest === this . options . loader ; } ) ;
36
- if ( ! loaderModule ) {
37
- throw Error ( "Can't locate " + this . options . loader + " in compilation" ) ;
38
- }
39
- let configModule ;
40
- if ( util . isString ( this . options . loaderConfig ) ) {
41
- configModule = compilation . modules . find ( ( module ) => { return module . rawRequest === this . options . loaderConfig ; } ) ;
42
- if ( ! configModule ) {
43
- throw Error ( "Can't locate " + this . options . loaderConfig + " in compilation" ) ;
44
- }
45
- }
46
- chunks . forEach ( ( chunk ) => {
47
- if ( chunk . hasRuntime ( ) ) {
48
- if ( ! containsModule ( chunk , loaderModule ) ) {
49
- chunk . addModule ( loaderModule ) ;
50
- loaderModule . addChunk ( chunk ) ;
51
- }
52
- if ( configModule && ! containsModule ( chunk , configModule ) ) {
53
- chunk . addModule ( configModule ) ;
54
- configModule . addChunk ( chunk ) ;
55
- }
56
- } else {
57
- if ( containsModule ( chunk , loaderModule ) ) {
58
- chunk . removeModule ( loaderModule ) ;
59
- loaderModule . removeChunk ( chunk ) ;
60
- }
61
- if ( configModule && containsModule ( chunk , configModule ) ) {
62
- chunk . removeModule ( configModule ) ;
63
- configModule . removeChunk ( chunk ) ;
31
+ apply ( compiler ) {
32
+ compiler . plugin ( "compilation" , ( compilation ) => {
33
+ compilation . plugin ( "succeed-module" , ( module ) => {
34
+ if ( ! module . issuer ) {
35
+ // No issuer generally means an entry module, so add a Dojo loader dependency. It doesn't
36
+ // hurt to add extra dependencies because the Dojo loader module will be removed from chunks
37
+ // that don't need it in the 'after-optimize-chunks' handler below.
38
+ module . addDependency ( new CommonJsRequireDependency ( this . options . loader ) ) ;
39
+ if ( util . isString ( this . options . loaderConfig ) ) {
40
+ module . addDependency ( new CommonJsRequireDependency ( this . options . loaderConfig ) ) ;
64
41
}
65
42
}
66
43
} ) ;
44
+
45
+ compilation . plugin ( "after-optimize-chunks" , ( chunks ) => {
46
+ // Get the loader and loader config
47
+ const loaderModule = compilation . modules . find ( ( module ) => { return module . rawRequest === this . options . loader ; } ) ;
48
+ const configModule = util . isString ( this . options . loaderConfig ) &&
49
+ compilation . modules . find ( ( module ) => { return module . rawRequest === this . options . loaderConfig ; } ) ;
50
+
51
+ // Ensure that the Dojo loader, and optionally the loader config, are included
52
+ // only in the entry chunks that contain the webpack runtime.
53
+ chunks . forEach ( ( chunk ) => {
54
+ if ( chunk . hasRuntime ( ) ) {
55
+ if ( ! loaderModule ) {
56
+ throw Error ( "Can't locate " + this . options . loader + " in compilation" ) ;
57
+ }
58
+ if ( util . isString ( this . options . loaderConfig ) && ! configModule ) {
59
+ throw Error ( "Can't locate " + this . options . loaderConfig + " in compilation" ) ;
60
+ }
61
+ if ( ! containsModule ( chunk , loaderModule ) ) {
62
+ chunk . addModule ( loaderModule ) ;
63
+ loaderModule . addChunk ( chunk ) ;
64
+ }
65
+ if ( configModule && ! containsModule ( chunk , configModule ) ) {
66
+ chunk . addModule ( configModule ) ;
67
+ configModule . addChunk ( chunk ) ;
68
+ }
69
+ } else if ( loaderModule ) {
70
+ if ( containsModule ( chunk , loaderModule ) ) {
71
+ chunk . removeModule ( loaderModule ) ;
72
+ loaderModule . removeChunk ( chunk ) ;
73
+ }
74
+ if ( configModule && containsModule ( chunk , configModule ) ) {
75
+ chunk . removeModule ( configModule ) ;
76
+ configModule . removeChunk ( chunk ) ;
77
+ }
78
+ }
79
+ } ) ;
80
+ } ) ;
67
81
} ) ;
68
82
}
69
83
} ;
0 commit comments