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

BREAKING: Build packages as both CJS and ESM #1480

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions .github/workflows/lint-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ jobs:
exit 1
fi

build:
name: Build
build-source:
name: Build source
runs-on: ubuntu-latest
needs: prepare
strategy:
Expand All @@ -90,7 +90,31 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: yarn
- run: yarn --immutable
- run: yarn build
- run: yarn build:source
- name: Require clean working directory
shell: bash
run: |
if ! git diff --exit-code; then
echo "Working tree dirty at end of job"
exit 1
fi

build-types:
name: Build types
runs-on: ubuntu-latest
needs: prepare
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- run: yarn --immutable
- run: yarn build:types
- name: Require clean working directory
shell: bash
run: |
Expand Down
18 changes: 18 additions & 0 deletions .swcrc.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2020"
},
"sourceMaps": true,
"exclude": [
".*__fixtures__.*",
".*__mocks__.*",
".*__snapshots__.*",
".*__tests?__.*",
".*\\.test\\.?.*\\.tsx?",
".*\\.d\\.tsx?"
]
}
16 changes: 13 additions & 3 deletions constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -215,29 +215,39 @@ gen_enforced_field(WorkspaceCwd, 'license', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% The entrypoint for all published packages must be the same.
gen_enforced_field(WorkspaceCwd, 'main', './dist/index.js') :-
gen_enforced_field(WorkspaceCwd, 'main', './dist/cjs/index.js') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'module', './dist/esm/index.js') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
% Non-published packages must not specify an entrypoint.
gen_enforced_field(WorkspaceCwd, 'main', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% The type definitions entrypoint for all publishable packages must be the same.
gen_enforced_field(WorkspaceCwd, 'types', './dist/index.d.ts') :-
gen_enforced_field(WorkspaceCwd, 'types', './dist/types/index.d.ts') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
% Non-published packages must not specify a type definitions entrypoint.
gen_enforced_field(WorkspaceCwd, 'types', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% The list of files included in published packages must only include files
% generated during the build step.
gen_enforced_field(WorkspaceCwd, 'files', ['dist/']) :-
gen_enforced_field(WorkspaceCwd, 'files', ['dist/cjs/**', 'dist/esm/**', 'dist/types/**']) :-
\+ workspace_field(WorkspaceCwd, 'private', true).
% The root package must specify an empty set of published files. (This is
% required in order to be able to import anything in development-only scripts,
% as otherwise the `node/no-unpublished-require` ESLint rule will disallow it.)
gen_enforced_field(WorkspaceCwd, 'files', []) :-
WorkspaceCwd = '.'.

% All non-root packages must have the same build scripts.
gen_enforced_field(WorkspaceCwd, 'scripts.build:source', 'yarn build:esm && yarn build:cjs') :-
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'scripts.build:esm', 'swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6') :-
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'scripts.build:cjs', 'swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs') :-
WorkspaceCwd \= '.'.

% All non-root packages must have the same "build:docs" script.
gen_enforced_field(WorkspaceCwd, 'scripts.build:docs', 'typedoc') :-
WorkspaceCwd \= '.'.
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"packages/*"
],
"scripts": {
"build": "tsc --build tsconfig.build.json --verbose",
"build": "yarn build:source && yarn build:types",
"build:clean": "rimraf dist '**/*.tsbuildinfo' && yarn build",
"build:docs": "yarn workspaces foreach --parallel --interlaced --verbose run build:docs",
"build:source": "yarn workspaces foreach --parallel --interlaced --verbose run build:source",
"build:types": "tsc --build tsconfig.build.json --verbose",
"build:watch": "yarn run build --watch",
"changelog:validate": "yarn workspaces foreach --parallel --interlaced --verbose run changelog:validate",
"child-workspace-package-names-as-json": "ts-node scripts/child-workspace-package-names-as-json.ts",
Expand All @@ -40,6 +42,8 @@
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@metamask/eth-json-rpc-provider": "^1.0.0",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.66",
"@types/node": "^16.18.24",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
Expand Down Expand Up @@ -88,7 +92,8 @@
"web3-provider-engine>eth-sig-util>ethereumjs-abi>ethereumjs-util>keccak": false,
"web3-provider-engine>eth-sig-util>ethereumjs-abi>ethereumjs-util>secp256k1": false,
"web3-provider-engine>ethereumjs-vm>ethereumjs-util>keccak": false,
"web3-provider-engine>ethereumjs-vm>ethereumjs-util>secp256k1": false
"web3-provider-engine>ethereumjs-vm>ethereumjs-util>secp256k1": false,
"@swc/core": true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've enabled the script for @swc/core here, since it uses native packages to build the files.

}
}
}
12 changes: 9 additions & 3 deletions packages/address-book-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/address-book-controller",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/address-book-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
12 changes: 9 additions & 3 deletions packages/announcement-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/announcement-controller",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/announcement-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [{ "path": "../base-controller/tsconfig.build.json" }],
Expand Down
12 changes: 9 additions & 3 deletions packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/approval-controller",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/approval-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
12 changes: 9 additions & 3 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/assets-controllers",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/assets-controllers/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
12 changes: 9 additions & 3 deletions packages/base-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/base-controller",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/base-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
12 changes: 9 additions & 3 deletions packages/composable-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/composable-controller",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/composable-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
12 changes: 9 additions & 3 deletions packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/controller-utils",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/controller-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"baseUrl": "./",
"lib": ["ES2017", "DOM"],
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"include": ["../../types", "./src"]
Expand Down
12 changes: 9 additions & 3 deletions packages/ens-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/cjs/**",
"dist/esm/**",
"dist/types/**"
],
"scripts": {
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
"build:source": "yarn build:esm && yarn build:cjs",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/ens-controller",
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
"publish:preview": "yarn npm publish --tag preview",
Expand Down
2 changes: 1 addition & 1 deletion packages/ens-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
Loading