11import intersection from 'lodash/intersection' ;
22import once from 'lodash/once' ;
3- import NodeTemplatePlugin from 'webpack/lib/node/NodeTemplatePlugin' ;
4- import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin' ;
5- import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin' ;
6- import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin' ;
7- import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin' ;
8- import ExternalsPlugin from 'webpack/lib/ExternalsPlugin' ;
93import Promise from 'bluebird' ;
104import chalk from 'chalk' ;
115import dedent from 'dedent' ;
@@ -29,7 +23,7 @@ const logMultiWebpackError = once(() => {
2923 ) ;
3024} ) ;
3125
32- export default ( ) => {
26+ export default ( compat ) => {
3327 const cache = new Map ( ) ;
3428
3529 const expireCache = ( changedFiles ) => {
@@ -51,7 +45,7 @@ export default () => {
5145 ) ;
5246 } ;
5347
54- const getSource = async ( loader , request ) => {
48+ const getSource = async ( loader ) => {
5549 const identifier = loader . _module . identifier ( ) ;
5650 trace ( 'Get compiled source: %i' , identifier ) ;
5751
@@ -63,17 +57,16 @@ export default () => {
6357 }
6458
6559 trace ( 'No cached source. Compiling: %i' , identifier ) ;
66- const compilationResult = await compileTreatSource ( loader , request ) ;
60+ const compilationResult = await compileTreatSource ( loader , compat ) ;
6761
6862 cache . set ( identifier , compilationResult ) ;
6963
7064 return compilationResult ;
7165 } ;
7266
73- const getCompiledSource = async ( loader , request ) => {
67+ const getCompiledSource = async ( loader ) => {
7468 const { source, fileDependencies, contextDependencies } = await getSource (
7569 loader ,
76- request ,
7770 ) ;
7871
7972 // Set loader dependencies to dependecies of the child compiler
@@ -106,48 +99,86 @@ function getRootCompilation(loader) {
10699 return compilation ;
107100}
108101
109- function compileTreatSource ( loader ) {
102+ function compileTreatSource ( loader , compat ) {
110103 return new Promise ( ( resolve , reject ) => {
111104 // Child compiler will compile treat files to be evaled during compilation
112105 const outputOptions = { filename : loader . resourcePath } ;
113106
114107 const childCompiler = getRootCompilation ( loader ) . createChildCompiler (
115108 TWL ,
116109 outputOptions ,
117- [
118- new NodeTemplatePlugin ( outputOptions ) ,
119- new LibraryTemplatePlugin ( null , 'commonjs2' ) ,
120- new NodeTargetPlugin ( ) ,
121- new SingleEntryPlugin ( loader . context , loader . resourcePath ) ,
122- new LimitChunkCountPlugin ( { maxChunks : 1 } ) ,
123- new ExternalsPlugin ( 'commonjs' , 'treat' ) ,
124- ] ,
125110 ) ;
126111
127- const subCache = 'subcache ' + __dirname + ' ' + loader . resourcePath ;
128- childCompiler . hooks . compilation . tap ( TWL , ( compilation ) => {
129- if ( compilation . cache ) {
130- if ( ! compilation . cache [ subCache ] ) compilation . cache [ subCache ] = { } ;
131- compilation . cache = compilation . cache [ subCache ] ;
132- }
133- } ) ;
112+ const NodeTemplatePlugin = compat . getNodeTemplatePlugin ( loader . _compiler ) ;
113+ const NodeTargetPlugin = compat . getNodeTargetPlugin ( loader . _compiler ) ;
114+ const LimitChunkCountPlugin = compat . getLimitChunkCountPlugin (
115+ loader . _compiler ,
116+ ) ;
117+ const ExternalsPlugin = compat . getExternalsPlugin ( loader . _compiler ) ;
118+
119+ new NodeTemplatePlugin ( outputOptions ) . apply ( childCompiler ) ;
120+ new NodeTargetPlugin ( ) . apply ( childCompiler ) ;
121+
122+ if ( compat . isWebpack5 ) {
123+ const {
124+ EntryOptionPlugin,
125+ library : { EnableLibraryPlugin } ,
126+ } = loader . _compiler . webpack ;
127+
128+ new EnableLibraryPlugin ( 'commonjs2' ) . apply ( childCompiler ) ;
129+
130+ EntryOptionPlugin . applyEntryOption ( childCompiler , loader . context , {
131+ child : {
132+ library : {
133+ type : 'commonjs2' ,
134+ } ,
135+ import : [ loader . resourcePath ] ,
136+ } ,
137+ } ) ;
138+ } else {
139+ // Webpack 4 code. Remove once support is removed
140+ const { LibraryTemplatePlugin, SingleEntryPlugin } = require ( 'webpack' ) ;
141+
142+ new LibraryTemplatePlugin ( null , 'commonjs2' ) . apply ( childCompiler ) ;
143+ new SingleEntryPlugin ( loader . context , loader . resourcePath ) . apply (
144+ childCompiler ,
145+ ) ;
146+ }
134147
135- let source ;
148+ new LimitChunkCountPlugin ( { maxChunks : 1 } ) . apply ( childCompiler ) ;
149+ new ExternalsPlugin ( 'commonjs' , 'treat' ) . apply ( childCompiler ) ;
136150
137- childCompiler . hooks . afterCompile . tapAsync ( TWL , ( compilation , callback ) => {
138- source =
139- compilation . assets [ loader . resourcePath ] &&
140- compilation . assets [ loader . resourcePath ] . source ( ) ;
151+ let source ;
141152
142- // Remove all chunk assets
143- compilation . chunks . forEach ( ( chunk ) => {
144- chunk . files . forEach ( ( file ) => {
145- delete compilation . assets [ file ] ;
153+ if ( compat . isWebpack5 ) {
154+ childCompiler . hooks . compilation . tap ( TWL , ( compilation ) => {
155+ compilation . hooks . processAssets . tap ( TWL , ( ) => {
156+ source =
157+ compilation . assets [ loader . resourcePath ] &&
158+ compilation . assets [ loader . resourcePath ] . source ( ) ;
159+
160+ // Remove all chunk assets
161+ compilation . chunks . forEach ( ( chunk ) => {
162+ chunk . files . forEach ( ( file ) => {
163+ compilation . deleteAsset ( file ) ;
164+ } ) ;
165+ } ) ;
146166 } ) ;
147167 } ) ;
148-
149- callback ( ) ;
150- } ) ;
168+ } else {
169+ childCompiler . hooks . afterCompile . tap ( TWL , ( compilation ) => {
170+ source =
171+ compilation . assets [ loader . resourcePath ] &&
172+ compilation . assets [ loader . resourcePath ] . source ( ) ;
173+
174+ // Remove all chunk assets
175+ compilation . chunks . forEach ( ( chunk ) => {
176+ chunk . files . forEach ( ( file ) => {
177+ delete compilation . assets [ file ] ;
178+ } ) ;
179+ } ) ;
180+ } ) ;
181+ }
151182
152183 try {
153184 childCompiler . runAsChild ( ( err , entries , compilation ) => {
0 commit comments