File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11const webpack = require ( "../../../../" ) ;
22
3- /** @type {import("../../../../").Configuration[] } */
3+ /** @type {import("../../../../types ").Configuration[] } */
44module . exports = [
55 {
66 output : {
7- libraryTarget : "commonjs2 "
7+ libraryTarget : "global "
88 } ,
99 externals : {
10- external : [ "webpack " , "version" ]
10+ external : [ "process " , "version" ]
1111 } ,
1212 plugins : [
1313 new webpack . DefinePlugin ( {
14- EXPECTED : JSON . stringify ( webpack . version )
14+ EXPECTED : JSON . stringify ( process . version )
1515 } )
1616 ]
1717 } ,
Original file line number Diff line number Diff line change 1+ function getMajorVersion ( versionStr ) {
2+ const match = versionStr . match ( / ^ v ? ( \d + ) \. / ) ;
3+ if ( match ) {
4+ return parseInt ( match [ 1 ] , 10 ) ;
5+ }
6+ return null ;
7+ }
8+
9+ it ( "should not fail on optional externals" , function ( ) {
10+ if ( getMajorVersion ( NODE_VERSION ) <= 12 ) {
11+ const external = require ( "external" ) ;
12+ // The behavior of jest mock's require is different from that of node require, so it works fine here.
13+ expect ( external ) . toBe ( EXPECTED ) ;
14+ } else {
15+ try {
16+ require ( "external" ) ;
17+ } catch ( e ) {
18+ // Since there is no webpack in node_modules, node require will report an error here.
19+ expect ( e . message ) . toContain ( "Cannot find module 'webpack'" ) ;
20+ }
21+ }
22+ } ) ;
Original file line number Diff line number Diff line change 1+ const webpack = require ( "../../../../" ) ;
2+
3+ /** @type {import("../../../../types").Configuration } */
4+ module . exports = {
5+ output : {
6+ libraryTarget : "commonjs2"
7+ } ,
8+ externals : {
9+ external : [ "webpack" , "version" ]
10+ } ,
11+ plugins : [
12+ new webpack . DefinePlugin ( {
13+ NODE_VERSION : JSON . stringify ( process . version ) ,
14+ EXPECTED : JSON . stringify ( webpack . version )
15+ } )
16+ ]
17+ } ;
Original file line number Diff line number Diff line change 11const fs = require ( "fs" ) ;
2+ const { Module } = require ( "module" ) ;
23const path = require ( "path" ) ;
34const { fileURLToPath, pathToFileURL } = require ( "url" ) ;
45const vm = require ( "vm" ) ;
@@ -292,7 +293,11 @@ class TestRunner {
292293 }
293294 const moduleInfo = this . _resolveModule ( currentDirectory , module ) ;
294295 if ( ! moduleInfo ) {
295- return require ( module . startsWith ( "node:" ) ? module . slice ( 5 ) : module ) ;
296+ // node v12.2.0+ has Module.createRequire
297+ const rawRequire = Module . createRequire
298+ ? Module . createRequire ( currentDirectory )
299+ : require ;
300+ return rawRequire ( module . startsWith ( "node:" ) ? module . slice ( 5 ) : module ) ;
296301 }
297302 const { modulePath } = moduleInfo ;
298303 if (
You can’t perform that action at this time.
0 commit comments