@@ -189,6 +189,65 @@ function normalizeV7Config(config) {
189189 } ;
190190}
191191
192+ function getCustomConfig (
193+ { config, codeSandboxPlugins } ,
194+ version : number ,
195+ path : string ,
196+ options : Object
197+ ) {
198+ if (
199+ / ^ \/ n o d e _ m o d u l e s / . test ( path ) &&
200+ / \. j s $ / . test ( path ) &&
201+ options . compileNodeModulesWithEnv
202+ ) {
203+ if ( version === 7 ) {
204+ return {
205+ parserOpts : {
206+ plugins : [ 'dynamicImport' , 'objectRestSpread' ] ,
207+ } ,
208+ presets : [ 'env' , 'react' ] ,
209+ plugins : [
210+ 'transform-modules-commonjs' ,
211+ 'proposal-class-properties' ,
212+ '@babel/plugin-transform-runtime' ,
213+ ...codeSandboxPlugins ,
214+ ] ,
215+ } ;
216+ }
217+
218+ return {
219+ presets : [ 'es2015' , 'react' , 'stage-0' ] ,
220+ plugins : [
221+ 'transform-es2015-modules-commonjs' ,
222+ 'transform-class-properties' ,
223+ [
224+ 'transform-runtime' ,
225+ {
226+ helpers : false ,
227+ polyfill : false ,
228+ regenerator : true ,
229+ } ,
230+ ] ,
231+ [
232+ 'transform-regenerator' ,
233+ {
234+ // Async functions are converted to generators by babel-preset-env
235+ async : false ,
236+ } ,
237+ ] ,
238+ ...codeSandboxPlugins ,
239+ ] ,
240+ } ;
241+ }
242+
243+ return {
244+ ...config ,
245+ plugins : config . plugins
246+ ? [ ...config . plugins , ...codeSandboxPlugins ]
247+ : codeSandboxPlugins ,
248+ } ;
249+ }
250+
192251async function compile ( code , customConfig , path ) {
193252 try {
194253 let result ;
@@ -357,70 +416,31 @@ self.addEventListener('message', async event => {
357416 lastConfig = stringifiedConfig ;
358417 }
359418
360- const plugins = [ ... ( config . plugins || [ ] ) ] ;
419+ const codeSandboxPlugins = [ ] ;
361420
362421 if ( ! disableCodeSandboxPlugins ) {
363- plugins . push ( 'dynamic-import-node' ) ;
422+ codeSandboxPlugins . push ( 'dynamic-import-node' ) ;
364423
365424 if ( loaderOptions . dynamicCSSModules ) {
366- plugins . push ( 'dynamic-css-modules' ) ;
425+ codeSandboxPlugins . push ( 'dynamic-css-modules' ) ;
367426 }
368427
369428 if ( ! sandboxOptions || sandboxOptions . infiniteLoopProtection ) {
370- plugins . push ( 'babel-plugin-transform-prevent-infinite-loops' ) ;
429+ codeSandboxPlugins . push ( 'babel-plugin-transform-prevent-infinite-loops' ) ;
371430 }
372431 }
373432
374- plugins . push ( [
433+ codeSandboxPlugins . push ( [
375434 'babel-plugin-detective' ,
376435 { source : true , nodes : true , generated : true } ,
377436 ] ) ;
378437
379- const customConfig =
380- / ^ \/ n o d e _ m o d u l e s / . test ( path ) && / \. j s $ / . test ( path )
381- ? {
382- parserOpts : version === 7 && {
383- plugins : [ 'dynamicImport' , 'objectRestSpread' ] ,
384- } ,
385- presets :
386- version === 7 ? [ 'env' , 'react' ] : [ 'es2015' , 'react' , 'stage-0' ] ,
387- plugins : [
388- version === 7
389- ? 'transform-modules-commonjs'
390- : 'transform-es2015-modules-commonjs' ,
391- version === 7
392- ? 'proposal-class-properties'
393- : 'transform-class-properties' ,
394- ...( version === 7
395- ? [ '@babel/plugin-transform-runtime' ]
396- : [
397- [
398- 'transform-runtime' ,
399- {
400- helpers : false ,
401- polyfill : false ,
402- regenerator : true ,
403- } ,
404- ] ,
405- [
406- 'transform-regenerator' ,
407- {
408- // Async functions are converted to generators by babel-preset-env
409- async : false ,
410- } ,
411- ] ,
412- ] ) ,
413- 'dynamic-import-node' ,
414- [
415- 'babel-plugin-detective' ,
416- { source : true , nodes : true , generated : true } ,
417- ] ,
418- ] . filter ( Boolean ) ,
419- }
420- : {
421- ...config ,
422- plugins,
423- } ;
438+ const customConfig = getCustomConfig (
439+ { config, codeSandboxPlugins } ,
440+ version ,
441+ path ,
442+ loaderOptions
443+ ) ;
424444
425445 const flattenedPresets = flatten ( customConfig . presets || [ ] ) ;
426446 const flattenedPlugins = flatten ( customConfig . plugins || [ ] ) ;
0 commit comments