@@ -62,16 +62,16 @@ function makeBundleJS(psModule) {
62
62
return Promise . resolve ( result ) ;
63
63
}
64
64
65
- // Replace require paths to output files generated by psc with paths
65
+ // Replace require and import paths to output files generated by psc with paths
66
66
// to purescript sources, which are then also run through this loader.
67
67
// Additionally, the imports replaced are tracked so that in the event
68
68
// the compiler fails to compile the PureScript source, we can tack on
69
69
// any new imports in order to allow webpack to watch the new files
70
70
// before they have been successfully compiled.
71
71
function makeJS ( psModule , psModuleMap , js ) {
72
- const requireRE = / r e q u i r e \( [ ' " ] \. \. \/ ( [ \w \. ] + ) (?: \/ i n d e x \. j s ) ? [ ' " ] \) / g;
73
-
74
- const foreignRE = / r e q u i r e \( [ ' " ] \. \/ f o r e i g n (?: \. j s ) ? [ ' " ] \) / g;
72
+ const dependencyRE = / (?: r e q u i r e \( | f r o m \s * ) [ ' " ] \. \. \/ ( [ \w \. ] + ) (?: \/ i n d e x \. j s ) ? [ ' " ] / g;
73
+ const requireRE = / ^ r e q u i r e / ;
74
+ const foreignRE = / (?: r e q u i r e \( | f r o m \s * ) [ ' " ] \. \/ f o r e i g n (?: \. j s ) ? [ ' " ] / g;
75
75
76
76
const name = psModule . name ;
77
77
@@ -80,11 +80,11 @@ function makeJS(psModule, psModuleMap, js) {
80
80
var replacedImports = [ ] ;
81
81
82
82
const result = js
83
- . replace ( requireRE , ( m , p1 ) => {
83
+ . replace ( dependencyRE , ( m , p1 ) => {
84
84
const moduleValue = psModuleMap [ p1 ] ;
85
85
86
86
if ( ! moduleValue ) {
87
- debug ( 'module %s was not found in the map, replacing require with null' , p1 ) ;
87
+ debug ( 'module %s was not found in the map, replacing dependency with null' , p1 ) ;
88
88
89
89
return 'null' ;
90
90
}
@@ -93,13 +93,17 @@ function makeJS(psModule, psModuleMap, js) {
93
93
94
94
replacedImports . push ( p1 ) ;
95
95
96
- return `require("${ escapedPath } ")` ;
96
+ return requireRE . test ( m )
97
+ ? `require("${ escapedPath } "`
98
+ : `from "${ escapedPath } "` ;
97
99
}
98
100
} )
99
- . replace ( foreignRE , ( ) => {
101
+ . replace ( foreignRE , ( m ) => {
100
102
const escapedPath = jsStringEscape ( psModuleMap [ name ] . ffi ) ;
101
103
102
- return `require("${ escapedPath } ")` ;
104
+ return requireRE . test ( m )
105
+ ? `require("${ escapedPath } "`
106
+ : `from "${ escapedPath } "` ;
103
107
} )
104
108
;
105
109
0 commit comments