Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Try converting packages to ESM #30510

Closed
wants to merge 14 commits into from
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"@typescript-eslint/eslint-plugin": "^5.60.0",
"@typescript-eslint/parser": "^5.60.0",
"babel-loader": "^9.1.2",
"babel-plugin-add-import-extension": "^1.6.0",
"babel-plugin-istanbul": "^6.1.1",
"babel-plugin-macros": "^3.1.0",
"babel-plugin-module-resolver": "^5.0.0",
Expand Down
4 changes: 4 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ async function run(argv) {
'--ignore',
// Need to put these patterns in quotes otherwise they might be evaluated by the used terminal.
`"${ignore.join('","')}"`,
'--plugins',
// Make sure this plugin doesn't get picked up by the rollup build, rollup bundler would try to resolve
// non-existing javascript files.
'babel-plugin-add-import-extension',
];
if (largeFiles) {
babelArgs.push('--compact false');
Expand Down
69 changes: 50 additions & 19 deletions scripts/copyFiles.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,32 @@ async function includeFileInBuild(file) {
async function createModulePackages({ from, to }) {
const directoryPackages = glob.sync('*/index.{js,ts,tsx}', { cwd: from }).map(path.dirname);

await Promise.all(
const modulePackages = await Promise.all(
directoryPackages.map(async (directoryPackage) => {
const packageJsonPath = path.join(to, directoryPackage, 'package.json');
const packageDir = path.dirname(packageJsonPath);
const topLevelPathImportsAreCommonJSModules = await fse.pathExists(
path.resolve(path.dirname(packageJsonPath), '../esm'),
path.resolve(packageDir, '../esm'),
);

const esm = topLevelPathImportsAreCommonJSModules
? path.posix.join('../esm', directoryPackage, 'index.js')
: './index.js';

const cjs = topLevelPathImportsAreCommonJSModules
? './index.js'
: path.posix.join('../node', directoryPackage, 'index.js');

const packageJson = {
sideEffects: false,
module: topLevelPathImportsAreCommonJSModules
? path.posix.join('../esm', directoryPackage, 'index.js')
: './index.js',
main: topLevelPathImportsAreCommonJSModules
? './index.js'
: path.posix.join('../node', directoryPackage, 'index.js'),
module: esm,
main: cjs,
type: 'module',
exports: {
'.': {
require: cjs,
},
},
types: './index.d.ts',
};

Expand All @@ -68,9 +79,16 @@ async function createModulePackages({ from, to }) {
throw new Error(`${packageJsonPath}:\n${manifestErrorMessages.join('\n')}`);
}

return packageJsonPath;
return {
[`./${path.basename(packageDir)}`]: {
import: `./${path.relative(to, path.resolve(packageDir, esm))}`,
require: `./${path.relative(to, path.resolve(packageDir, cjs))}`,
},
};
}),
);

return modulePackages;
}

async function typescriptCopy({ from, to }) {
Expand All @@ -84,24 +102,37 @@ async function typescriptCopy({ from, to }) {
return Promise.all(cmds);
}

async function createPackageFile() {
async function createPackageFile({ innerModules }) {
const packageData = await fse.readFile(path.resolve(packagePath, './package.json'), 'utf8');
const { nyc, scripts, devDependencies, workspaces, ...packageDataOther } =
JSON.parse(packageData);

const cjs = fse.existsSync(path.resolve(buildPath, './node/index.js'))
? './node/index.js'
: './index.js';

const esm = fse.existsSync(path.resolve(buildPath, './esm/index.js'))
? './esm/index.js'
: './index.js';

const newPackageData = {
...packageDataOther,
private: false,
...(packageDataOther.main
? {
main: fse.existsSync(path.resolve(buildPath, './node/index.js'))
? './node/index.js'
: './index.js',
module: fse.existsSync(path.resolve(buildPath, './esm/index.js'))
? './esm/index.js'
: './index.js',
main: cjs,
module: esm,
}
: {}),
type: 'module',
exports: {
'.': {
import: esm,
require: cjs,
},
...Object.assign({}, ...innerModules),
},
types: './index.d.ts',
};

const typeDefinitionsFilePath = path.resolve(buildPath, './index.d.ts');
Expand Down Expand Up @@ -158,7 +189,9 @@ async function run() {
// TypeScript
await typescriptCopy({ from: srcPath, to: buildPath });

const packageData = await createPackageFile();
const innerModules = await createModulePackages({ from: srcPath, to: buildPath });

const packageData = await createPackageFile({ innerModules });

await Promise.all(
[
Expand All @@ -170,8 +203,6 @@ async function run() {
);

await addLicense(packageData);

await createModulePackages({ from: srcPath, to: buildPath });
} catch (err) {
console.error(err);
process.exit(1);
Expand Down
5 changes: 0 additions & 5 deletions test/bundling/fixtures/node-esm/development.js

This file was deleted.

7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4833,6 +4833,13 @@ babel-loader@^9.1.2:
find-cache-dir "^3.3.2"
schema-utils "^4.0.0"

babel-plugin-add-import-extension@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/babel-plugin-add-import-extension/-/babel-plugin-add-import-extension-1.6.0.tgz#807ce65b38d4763797c1616cb4e8372da167cdd1"
integrity sha512-JVSQPMzNzN/S4wPRoKQ7+u8PlkV//BPUMnfWVbr63zcE+6yHdU2Mblz10Vf7qe+6Rmu4svF5jG7JxdcPi9VvKg==
dependencies:
"@babel/helper-plugin-utils" "^7.14.5"

babel-plugin-istanbul@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-6.1.1.tgz#fa88ec59232fd9b4e36dbbc540a8ec9a9b47da73"
Expand Down