Skip to content

Commit

Permalink
compatibility with explicit extensions imports
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Aug 1, 2021
1 parent 7a4525a commit e858db9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions resources/build-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if (require.main === module) {
fs.copyFileSync('./README.md', './npmDist/README.md');

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

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

function buildPackageJSON() {
function buildPackageJSON(srcFiles) {
const packageJSON = JSON.parse(
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
);
Expand Down Expand Up @@ -103,5 +103,19 @@ function buildPackageJSON() {
packageJSON.publishConfig = { tag: publishTag };
}

/**
* This prevents breaking previous versions of imports with explicit extensions
* Like `require("graphql/language/parser.js")`
*/
for (const srcFile of srcFiles) {
if (srcFile.endsWith('.ts')) {
const srcFilePath = srcFile.slice(0, srcFile.length - 3);
packageJSON.exports[`./${srcFilePath}.js`] = {
require: `./${srcFilePath}.js`,
import: `./${srcFilePath}.mjs`,
};
}
}

return packageJSON;
}

0 comments on commit e858db9

Please sign in to comment.