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

Build package as both CJS and ESM #203

Merged
merged 13 commits into from
Jul 11, 2023
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]
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't need to run build on all these Node versions. But can be fixed in another PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, but out of scope for this PR.

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 @@
{
Copy link
Member Author

Choose a reason for hiding this comment

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

Just leaving a comment here for future reference.

When we change the package to use SWC for other parts of the stack, such as testing, we need to keep in mind to use a different .swcrc file. This one is explicitly ignoring test files and such, so will not work with Jest.

"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2020"
},
"sourceMaps": true,
"exclude": [
".*__fixtures__.*",
Mrtenz marked this conversation as resolved.
Show resolved Hide resolved
".*__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').
Mrtenz marked this conversation as resolved.
Show resolved Hide resolved

% 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,
mcmire marked this conversation as resolved.
Show resolved Hide resolved
"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