Skip to content

Commit

Permalink
fix: correctly declare types in package.json exports (#2972)
Browse files Browse the repository at this point in the history
Add the `types` value to the `exports` field of the `package.json` file.
This makes projects work when `moduleResolution` is set to `bundler` in `tsconfig.json`.

The previous configuration generated a TypeScript compiler error:

    TS7016: Could not find a declaration file for module 'bpmn-visualization'.
    There are types at 'node_modules/bpmn-visualization/dist/bpmn-visualization.d.ts',
    but this result could not be resolved when respecting package.json "exports".
    The 'bpmn-visualization' library may need to update its package.json or typings.

A reproduction test project has been added to validate the change.
  • Loading branch information
tbouffard authored Nov 24, 2023
1 parent fc584c2 commit da05316
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .github/workflows/test-npm-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
- 'test/config/**/*'
- 'test/bundles/**/*'
- 'test/typescript-support/**/*'
- 'test/typescript-moduleResolution-bundler/**/*'
- '.nvmrc'
- 'package.json'
- 'package-lock.json'
Expand All @@ -31,6 +32,7 @@ on:
- 'test/config/**/*'
- 'test/shared/**/*'
- 'test/typescript-support/**/*'
- 'test/typescript-moduleResolution-bundler/**/*'
- '.nvmrc'
- 'package.json'
- 'package-lock.json'
Expand Down Expand Up @@ -76,3 +78,10 @@ jobs:
- name: Run typescript-support test
working-directory: 'test/typescript-support'
run: npm test
# Check the "exports" field in package.json: project using moduleResolution=bundler in tsconfig.json
- name: Setup typescript-moduleResolution-bundler test
working-directory: 'test/typescript-moduleResolution-bundler'
run: npm install --ignore-scripts --prefer-offline --audit false
- name: Run typescript-moduleResolution-bundler test
working-directory: 'test/typescript-moduleResolution-bundler'
run: npm test
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@
"module": "dist/bpmn-visualization.esm.js",
"types": "dist/not-supported-ts-versions.d.ts",
"exports": {
".": "./dist/bpmn-visualization.esm.js",
".": {
"import": {
"types": "./dist/bpmn-visualization.d.ts",
"default": "./dist/bpmn-visualization.esm.js"
}
},
"./package.json": "./package.json"
},
"typesVersions": {
Expand Down
1 change: 1 addition & 0 deletions test/performance/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"lib": ["es2022", "esnext.disposable", "dom", "DOM.Iterable"]
},
"exclude": [
"../typescript-moduleResolution-bundler/**/*",
"../typescript-support/**/*"
]
}
1 change: 1 addition & 0 deletions test/typescript-moduleResolution-bundler/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
8 changes: 8 additions & 0 deletions test/typescript-moduleResolution-bundler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Validate bpmn-visualization when `moduleResolution` is set to `bundler`

Relates to [missing types configuration in the package.json "exports" field](https://github.com/process-analytics/bpmn-visualization-js/pull/2972).

## Run

- `npm install`
- `npm test`
13 changes: 13 additions & 0 deletions test/typescript-moduleResolution-bundler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "check-typescript-module_resolution-bundler",
"private": true,
"scripts": {
"test": "tsc --version && tsc"
},
"dependencies": {
"bpmn-visualization": "../../"
},
"devDependencies": {
"typescript": "5.2.2"
}
}
21 changes: 21 additions & 0 deletions test/typescript-moduleResolution-bundler/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2023 Bonitasoft S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// eslint-disable-next-line import/no-unresolved -- The bpmn-visualization package may not have been built prior running eslint (it happens when running GitHub Actions)
import { BpmnVisualization } from 'bpmn-visualization';

const bpmnVisualization = new BpmnVisualization({ container: 'id' });
bpmnVisualization.load('fake data');
15 changes: 15 additions & 0 deletions test/typescript-moduleResolution-bundler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2015",
"lib": ["dom", "dom.iterable", "ES2015"],
"allowJs": false,
"skipLibCheck": false,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler", // this configuration requires to have types declaration in the package.json "exports" field
"resolveJsonModule": false,
"isolatedModules": true,
},
}

0 comments on commit da05316

Please sign in to comment.