Skip to content

Commit e858db9

Browse files
committed
compatibility with explicit extensions imports
1 parent 7a4525a commit e858db9

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

resources/build-npm.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (require.main === module) {
5252
fs.copyFileSync('./README.md', './npmDist/README.md');
5353

5454
// Should be done as the last step so only valid packages can be published
55-
const packageJSON = buildPackageJSON();
55+
const packageJSON = buildPackageJSON(srcFiles);
5656
writeGeneratedFile('./npmDist/package.json', JSON.stringify(packageJSON));
5757

5858
showDirStats('./npmDist');
@@ -72,7 +72,7 @@ function babelBuild(srcPath, options) {
7272
return code + '\n';
7373
}
7474

75-
function buildPackageJSON() {
75+
function buildPackageJSON(srcFiles) {
7676
const packageJSON = JSON.parse(
7777
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
7878
);
@@ -103,5 +103,19 @@ function buildPackageJSON() {
103103
packageJSON.publishConfig = { tag: publishTag };
104104
}
105105

106+
/**
107+
* This prevents breaking previous versions of imports with explicit extensions
108+
* Like `require("graphql/language/parser.js")`
109+
*/
110+
for (const srcFile of srcFiles) {
111+
if (srcFile.endsWith('.ts')) {
112+
const srcFilePath = srcFile.slice(0, srcFile.length - 3);
113+
packageJSON.exports[`./${srcFilePath}.js`] = {
114+
require: `./${srcFilePath}.js`,
115+
import: `./${srcFilePath}.mjs`,
116+
};
117+
}
118+
}
119+
106120
return packageJSON;
107121
}

0 commit comments

Comments
 (0)