Skip to content

Commit

Permalink
Build package as both CJS and ESM (#203)
Browse files Browse the repository at this point in the history
* Build source with SWC and types with TSC

* Update package.json to reflect new dist folders

* Build source and types in a separate CI job

* Update Yarn constraints

* yarn lint:fix

* Add constraint for module field

* Synchronise `.swcrc.build.json` and `tsconfig.build.json` excludes

* Add constraints for exports field

* Add wildcard export

* Remove test file

* Remove exports field

* Remove constraints for exports field

* Remove comment from SWC config
  • Loading branch information
Mrtenz authored Jul 11, 2023
1 parent df8ac97 commit 9f4537c
Show file tree
Hide file tree
Showing 6 changed files with 867 additions and 30 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
- name: Install Yarn dependencies
run: yarn --immutable

build:
name: Build
build-source:
name: Build source
runs-on: ubuntu-latest
needs:
- prepare
Expand All @@ -33,7 +33,32 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn --immutable --immutable-cache
- 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: [16.x, 18.x, 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 --immutable-cache
- 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?"
]
}
13 changes: 8 additions & 5 deletions constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,18 @@ gen_enforced_field(WorkspaceCwd, 'repository.url', 'https://github.com/MetaMask/
% The license for the package must be specified.
gen_enforced_field(WorkspaceCwd, 'license').

% The entrypoint for the package must be `./dist/index.js`.
gen_enforced_field(WorkspaceCwd, 'main', './dist/index.js').
% The type definitions entrypoint the package must be `./dist/types/index.d.ts`.
gen_enforced_field(WorkspaceCwd, 'types', './dist/types/index.d.ts').

% The type definitions entrypoint the package must be `./dist/index.d.ts`.
gen_enforced_field(WorkspaceCwd, 'types', './dist/index.d.ts').
% The entrypoint for the package must be `./dist/cjs/index.js`.
gen_enforced_field(WorkspaceCwd, 'main', './dist/cjs/index.js').

% The module entrypoint for the package must be `./dist/esm/index.js`.
gen_enforced_field(WorkspaceCwd, 'module', './dist/esm/index.js').

% The list of files included in the package 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/**']).

% If a dependency is listed under "dependencies", it should not be listed under
% "devDependencies".
Expand Down
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
"url": "https://github.com/MetaMask/metamask-module-template.git"
},
"sideEffects": false,
"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": "tsc --project tsconfig.build.json",
"build": "yarn build:source && yarn build:types",
"build:cjs": "swc src --out-dir dist/cjs --config-file .swcrc.build.json --config module.type=commonjs",
"build:clean": "rimraf dist && yarn build",
"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",
"build:types": "tsc --project tsconfig.build.json",
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
"lint:changelog": "auto-changelog validate",
"lint:constraints": "yarn constraints",
Expand All @@ -39,6 +46,8 @@
"@metamask/eslint-config-jest": "^11.0.0",
"@metamask/eslint-config-nodejs": "^11.0.1",
"@metamask/eslint-config-typescript": "^11.0.0",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.66",
"@types/jest": "^28.1.6",
"@types/node": "^16",
"@typescript-eslint/eslint-plugin": "^5.43.0",
Expand Down Expand Up @@ -71,7 +80,8 @@
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false
"@lavamoat/preinstall-always-fail": false,
"@swc/core": true
}
}
}
13 changes: 11 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"inlineSources": true,
"noEmit": false,
"outDir": "dist",
"outDir": "dist/types",
"rootDir": "src",
"sourceMap": true
},
"include": ["./src/**/*.ts"],
"exclude": ["./src/**/*.test.ts"]
"exclude": [
"./src/**/__fixtures__/**/*",
"./src/**/__mocks__/**/*",
"./src/**/__test__/**/*",
"./src/**/__tests__/**/*",
"./src/**/__snapshots__/**/*",
"./src/**/*.test.ts",
"./src/**/*.test.*.ts"
]
}
Loading

0 comments on commit 9f4537c

Please sign in to comment.