Skip to content

Commit 389d256

Browse files
authored
Merge pull request #8000 from plotly/cam/7995/convert-ts-js-publish
fix: Compile TS to JS during packaging
2 parents 56a3e18 + d0454f7 commit 389d256

8 files changed

Lines changed: 96 additions & 15 deletions

File tree

.github/workflows/ci.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,56 @@ jobs:
596596
- name: Verify generated types are in sync with schema
597597
run: npm run schema-typegen-diff-check
598598

599+
package-resolution:
600+
needs: install-and-cibuild
601+
runs-on: ubuntu-latest
602+
steps:
603+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
604+
- uses: ./.github/actions/setup-workspace
605+
606+
- name: Pack the package
607+
run: npm pack --pack-destination "$RUNNER_TEMP"
608+
609+
- name: Install the tarball into a scratch project
610+
working-directory: ${{ runner.temp }}
611+
run: |
612+
mkdir consumer && cd consumer
613+
npm init -y
614+
npm install "$RUNNER_TEMP"/plotly.js-*.tgz
615+
616+
- name: Load every compiled module under Node
617+
shell: node {0}
618+
working-directory: ${{ runner.temp }}/consumer
619+
run: |
620+
const assert = require('node:assert');
621+
const fs = require('node:fs');
622+
623+
// src/lib/index.js reaches for these before it touches the DOM.
624+
globalThis.self = globalThis;
625+
globalThis.window = globalThis;
626+
627+
const paths = [process.cwd()];
628+
const load = (name) => require(require.resolve(name, { paths }));
629+
630+
const modules = fs
631+
.globSync('src/**/*.ts', { cwd: process.env.GITHUB_WORKSPACE })
632+
.filter((file) => !file.endsWith('.d.ts'))
633+
.map((file) => 'plotly.js/' + file.replace(/[.]ts$/, ''));
634+
635+
if (modules.length === 0) throw new Error('Found no TypeScript sources to check');
636+
637+
for (const name of modules) load(name);
638+
639+
const lib = load('plotly.js/src/lib/index');
640+
641+
assert.strictEqual(lib.mod(-1, 4), 3);
642+
assert.strictEqual(lib.modHalf(3, 4), -1);
643+
assert.deepStrictEqual(lib.sortObjectKeys({ b: 1, a: 2 }), ['a', 'b']);
644+
assert.strictEqual(lib.cleanNumber(' 12 '), 12);
645+
assert.strictEqual(typeof lib.counterRegex, 'function');
646+
647+
console.log('Loaded ' + modules.length + ' compiled modules and src/lib/index.js');
648+
599649
# ============================================================
600650
# Standalone jobs (no dependencies on install-and-cibuild)
601651
# ============================================================

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ stackgl_modules/node_modules
1414
tasks
1515
test
1616
topojson
17+
18+
# Exclude the TypeScript files (but not declarations) because Node doesn't
19+
# parse TS when installed in node_modules.
20+
src/**/*.ts
21+
!src/**/*.d.ts

draftlogs/8000_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Compile TypeScript files under `src/` to JavaScript during packaging to fix Node resolution [[#8000](https://github.com/plotly/plotly.js/pull/8000)]

lib/index.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ export type {
6161
YAxisName
6262
} from '../src/types/core/layout';
6363

64-
// ---------------------------------------------------------------------------
65-
// Trace data
66-
// ---------------------------------------------------------------------------
67-
68-
export type { Data } from '../src/types/core/data';
69-
7064
// ---------------------------------------------------------------------------
7165
// Configuration
7266
// ---------------------------------------------------------------------------

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
"preversion": "check-node-version --node 22 --npm 10 && npm-link-check && npm ls --prod --all",
6464
"version": "npm run build && git add -A lib dist build src/version.js",
6565
"postversion": "node -e \"console.log('Version bumped and committed. If ok, run: git push && git push --tags')\"",
66-
"postpublish": "node tasks/sync_packages.js"
66+
"postpublish": "node tasks/sync_packages.js",
67+
"prepack": "tsc -b tsconfig.build.json --force",
68+
"postpack": "tsc -b tsconfig.build.json --clean"
6769
},
6870
"dependencies": {
6971
"@plotly/d3": "3.8.2",
@@ -73,6 +75,7 @@
7375
"@turf/area": "^7.3.5",
7476
"@turf/centroid": "^7.3.5",
7577
"@turf/meta": "^7.3.5",
78+
"@types/d3": "^3.5.53",
7679
"base64-arraybuffer": "^1.0.2",
7780
"country-iso-search": "^0.1.2",
7881
"culori": "^4.0.2",
@@ -111,7 +114,6 @@
111114
"@biomejs/biome": "^2.5.5",
112115
"@plotly/mathjax-v3": "npm:mathjax@^3.2.2",
113116
"@plotly/mathjax-v4": "npm:mathjax@^4.1.3",
114-
"@types/d3": "3.5.34",
115117
"@types/node": "^26.1.1",
116118
"assert": "^2.1.0",
117119
"buffer": "^6.0.3",

src/types/core/data.internal.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Internal data/trace types (not in public API)
33
*
44
* These are runtime-resolved versions of trace data with internal state
5-
* properties. For public trace types, see data.d.ts.
5+
* properties. For public trace types, see generated/schema.d.ts.
66
*/
77

8+
import type { Data } from '../generated/schema';
89
import type { Datum } from '../lib/common';
9-
import type { Data } from './data';
1010

1111
/**
1212
* Calculated trace data (internal).

tsconfig.build.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
// Emit configuration for the published package.
3+
//
4+
// The repository authors a growing share of `src/` in TypeScript, but the
5+
// published package must contain only JavaScript. Node's CommonJS resolver
6+
// never tries a `.ts` extension, and Node refuses to strip types from any
7+
// file below `node_modules`. So the `prepack` script writes a `.js` sibling
8+
// for each `.ts` source, and `postpack` deletes it again.
9+
//
10+
// No `outDir` is set, so each `.js` lands next to its `.ts`. That is what
11+
// makes `require('./mod')` resolve in the tarball.
12+
//
13+
// Build mode drives both scripts. `tsc -b` emits, and `tsc -b --clean`
14+
// removes every generated file. Build mode also writes a state file, which
15+
// `tsBuildInfoFile` parks below `build/`, because `build/` is already
16+
// ignored by both git and npm.
17+
//
18+
// Type errors are not reported here. `npm run typecheck` owns that job and
19+
// reads the whole program, including the JavaScript files.
20+
"extends": "./tsconfig.json",
21+
"compilerOptions": {
22+
"noEmit": false,
23+
"noCheck": true,
24+
"allowJs": false,
25+
"module": "commonjs",
26+
"tsBuildInfoFile": "build/tsconfig.build.tsbuildinfo"
27+
},
28+
"include": ["src/**/*.ts"],
29+
"exclude": ["src/types/**"]
30+
}

0 commit comments

Comments
 (0)