Skip to content

Commit 4cea6cc

Browse files
authored
Support ES modules (#145)
1 parent 079f45a commit 4cea6cc

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/to-javascript.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ function makeBundleJS(psModule) {
6262
return Promise.resolve(result);
6363
}
6464

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
6666
// to purescript sources, which are then also run through this loader.
6767
// Additionally, the imports replaced are tracked so that in the event
6868
// the compiler fails to compile the PureScript source, we can tack on
6969
// any new imports in order to allow webpack to watch the new files
7070
// before they have been successfully compiled.
7171
function makeJS(psModule, psModuleMap, js) {
72-
const requireRE = /require\(['"]\.\.\/([\w\.]+)(?:\/index\.js)?['"]\)/g;
73-
74-
const foreignRE = /require\(['"]\.\/foreign(?:\.js)?['"]\)/g;
72+
const dependencyRE = /(?:require\(|from\s*)['"]\.\.\/([\w\.]+)(?:\/index\.js)?['"]/g;
73+
const requireRE = /^require/;
74+
const foreignRE = /(?:require\(|from\s*)['"]\.\/foreign(?:\.js)?['"]/g;
7575

7676
const name = psModule.name;
7777

@@ -80,11 +80,11 @@ function makeJS(psModule, psModuleMap, js) {
8080
var replacedImports = [];
8181

8282
const result = js
83-
.replace(requireRE, (m, p1) => {
83+
.replace(dependencyRE, (m, p1) => {
8484
const moduleValue = psModuleMap[p1];
8585

8686
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);
8888

8989
return 'null';
9090
}
@@ -93,13 +93,17 @@ function makeJS(psModule, psModuleMap, js) {
9393

9494
replacedImports.push(p1);
9595

96-
return `require("${escapedPath}")`;
96+
return requireRE.test(m)
97+
? `require("${escapedPath}"`
98+
: `from "${escapedPath}"`;
9799
}
98100
})
99-
.replace(foreignRE, () => {
101+
.replace(foreignRE, (m) => {
100102
const escapedPath = jsStringEscape(psModuleMap[name].ffi);
101103

102-
return `require("${escapedPath}")`;
104+
return requireRE.test(m)
105+
? `require("${escapedPath}"`
106+
: `from "${escapedPath}"`;
103107
})
104108
;
105109

0 commit comments

Comments
 (0)