@@ -139,21 +139,65 @@ describe("test option", () => {
139139 : "[name].js#[fullhash]" ,
140140 assetModuleFilename : "[name][ext][query][fragment]" ,
141141 } ,
142+ plugins : [
143+ {
144+ // Emitted as a copy would be: no `javascriptModule`, so the name decides.
145+ apply ( childCompiler ) {
146+ childCompiler . hooks . thisCompilation . tap ( "Copy" , ( compilation ) => {
147+ compilation . hooks . processAssets . tap (
148+ {
149+ name : "Copy" ,
150+ stage :
151+ childCompiler . webpack . Compilation
152+ . PROCESS_ASSETS_STAGE_ADDITIONAL ,
153+ } ,
154+ ( ) => {
155+ const { RawSource } = childCompiler . webpack . sources ;
156+
157+ for ( const name of [ "copy.mjs#m" , "copy.cjs#c" ] ) {
158+ compilation . emitAsset (
159+ name ,
160+ new RawSource ( "var foo = 12;\nconsole.log(foo);\n" ) ,
161+ ) ;
162+ }
163+ } ,
164+ ) ;
165+ } ) ;
166+ } ,
167+ } ,
168+ ] ,
142169 } ) ;
143170
171+ // What `.mjs` / `.cjs` read as, which the name decides past the fragment.
172+ const moduleByName = new Map ( ) ;
173+ const terserMinify = ( input , sourceMap , minimizerOptions ) => {
174+ for ( const name of Object . keys ( input ) ) {
175+ moduleByName . set ( name . replace ( / # .* $ / , "" ) , minimizerOptions . module ) ;
176+ }
177+
178+ return MinimizerPlugin . terserMinify ( input , sourceMap , minimizerOptions ) ;
179+ } ;
180+ Object . assign ( terserMinify , MinimizerPlugin . terserMinify ) ;
181+
144182 new MinimizerPlugin ( {
183+ parallel : false ,
145184 test : / \. (?: [ c m ] ? j s | j s o n ) $ / i,
146- minify : [ MinimizerPlugin . terserMinify , MinimizerPlugin . jsonMinify ] ,
185+ minify : [ terserMinify , MinimizerPlugin . jsonMinify ] ,
147186 } ) . apply ( compiler ) ;
148187
149188 const stats = await compile ( compiler ) ;
150189 const assets = readsAssets ( compiler , stats ) ;
151190 const names = Object . keys ( assets ) ;
152191
192+ expect ( moduleByName . get ( "copy.mjs" ) ) . toBe ( true ) ;
193+ expect ( moduleByName . get ( "copy.cjs" ) ) . toBe ( false ) ;
194+
153195 expect ( names ) . toEqual (
154196 expect . arrayContaining ( [
155197 expect . stringMatching ( / ^ j s \. j s # [ 0 - 9 a - f ] + $ / ) ,
156198 expect . stringMatching ( / ^ m j s \. m j s # [ 0 - 9 a - f ] + $ / ) ,
199+ "copy.mjs#m" ,
200+ "copy.cjs#c" ,
157201 "file.json#data" ,
158202 ] ) ,
159203 ) ;
0 commit comments