@@ -2,6 +2,7 @@ import test from "ava";
22import fs from "fs" ;
33import path from "path" ;
44import rimraf from "rimraf" ;
5+ import { satisfies } from "semver" ;
56import webpack from "webpack" ;
67import createTestDirectory from "./helpers/createTestDirectory" ;
78
@@ -127,3 +128,56 @@ test.cb(
127128 } ) ;
128129 } ,
129130) ;
131+
132+ test . cb ( "should load ESM config files" , t => {
133+ const config = Object . assign ( { } , globalConfig , {
134+ entry : path . join ( __dirname , "fixtures/constant.js" ) ,
135+ output : {
136+ path : t . context . directory ,
137+ } ,
138+ module : {
139+ rules : [
140+ {
141+ test : / \. j s $ / ,
142+ loader : babelLoader ,
143+ exclude : / n o d e _ m o d u l e s / ,
144+ options : {
145+ // Use relative path starting with a dot to satisfy module loader.
146+ // https://github.com/nodejs/node/issues/31710
147+ // File urls doesn't work with current resolve@1.12.0 package.
148+ extends : (
149+ "." +
150+ path . sep +
151+ path . relative (
152+ process . cwd ( ) ,
153+ path . resolve ( __dirname , "fixtures/babelrc.mjs" ) ,
154+ )
155+ ) . replace ( / \\ / g, "/" ) ,
156+ babelrc : false ,
157+ } ,
158+ } ,
159+ ] ,
160+ } ,
161+ } ) ;
162+
163+ webpack ( config , ( err , stats ) => {
164+ t . is ( err , null ) ;
165+ // Node supports ESM without a flag starting from 12.13.0 and 13.2.0.
166+ if ( satisfies ( process . version , `^12.13.0 || >=13.2.0` ) ) {
167+ t . deepEqual (
168+ stats . compilation . errors . map ( e => e . message ) ,
169+ [ ] ,
170+ ) ;
171+ } else {
172+ t . is ( stats . compilation . errors . length , 1 ) ;
173+ const moduleBuildError = stats . compilation . errors [ 0 ] ;
174+ const babelLoaderError = moduleBuildError . error ;
175+ t . true ( babelLoaderError instanceof Error ) ;
176+ // Error messages are slightly different between versions:
177+ // "modules aren't supported" or "modules not supported".
178+ t . regex ( babelLoaderError . message , / s u p p o r t e d / i) ;
179+ }
180+ t . is ( stats . compilation . warnings . length , 0 ) ;
181+ t . end ( ) ;
182+ } ) ;
183+ } ) ;
0 commit comments