Files
test-repo/scripts/vendors/README.md
T
Sosuke Suzuki 0abd1907bb Bundle Pure ESM Packages into /vendors/*.js as CommonJS (#12217)
* string-width

* Add bundle script

* Update package.json

* mem

* Add version validator

* Throw error

* Change filename

* Add documents

* Generate d.ts

* Fix lint problems

* globby

* Add workflow to validate vendor versions

* Fix eslintignore

* Fix

* Fix spell

* Address review

* process.exitCode = 1

* test validation

* Revert "test validation"

This reverts commit 5a86d5f71212e0e900709238a8cc109716678864.

* Remove exists files

* Fix conflicts

* Clean

* Remove globby
2022-02-06 17:31:50 +09:00

2.1 KiB

./scripts/vendors

Scripts for bundling Pure ESM Packages to CommonJS so that Prettier can use them.

Context

https://github.com/prettier/prettier/issues/12209#issuecomment-1028212785

Currently, Prettier is developed using CommonJS Modules. This is for historical reasons, and we are considering migrating to ECMAScript Modules in the future.

However, migrating to ECMAScript Modules is not an easy task.

While we are working on it, several important packages are being migrated to Pure ESM. Please see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c from @sindresorhus about Pure ESM Packages.

It would be a problem if Prettier could not use the new Pure ESM Package until we have completed that work.

So we will put the files bundled with Pure ESM Packages as CommonJS Modules under /vendors, and Prettier will use them.

This will be removed during the migration to ESM.

Usage

/scripts/vendors/bundle-vendors.mjs

node ./scripts/vendors/bundle-vendors.mjs

This script do the following 4 things:

1. Bundle ESM Packages to CommonJS

This bundles listed packages in vendors.mjs into /vendors/*.js as CommonJS.

2. Update package.json

This updates "imports" in package.json to enables aliases to resolve packages put in /vendors/*.js.

3. Lock ESM Packages version to vendor-versions.json

This locks versions of packages that is bundled to vendor-versions.json. It is used to validate by validate-vendor-versions.mjs.

4. Generate /vendors/types.d.ts

This Generates a minimal type definition file that satisfies lint:typecheck.

/scripts/vendors/validate-vendor-versions.mjs

node ./scripts/vendors/validate-vendor-versions.mjs

This script compares the version of packages locked in vendor-versions.json with the version of packages actually installed, and fails if they are different.

This is mainly run on CI to keep consistency between bundled and installed packages.