Skip to content

Commit 025f080

Browse files
authored
refactor(angular-query): restructure package type declaration path (#9519)
To support future sub-path exports in apps still using moduleResolution: node type declarations should be located in the same path as the corresponding implementation .mjs file
1 parent 711010c commit 025f080

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

packages/angular-query-experimental/package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"tanstack"
2828
],
2929
"scripts": {
30-
"clean": "premove ./build ./dist ./coverage ./dist-ts",
30+
"clean": "premove ./dist ./coverage ./dist-ts",
3131
"compile": "tsc --build",
3232
"test:eslint": "eslint ./src",
3333
"test:types": "npm-run-all --serial test:types:*",
@@ -59,9 +59,9 @@
5959
},
6060
"sideEffects": false,
6161
"files": [
62-
"dist",
63-
"src",
64-
"!src/__tests__"
62+
"**/*.d.ts",
63+
"**/*.mjs",
64+
"**/*.mjs.map"
6565
],
6666
"dependencies": {
6767
"@tanstack/query-core": "workspace:*",
@@ -82,5 +82,9 @@
8282
"peerDependencies": {
8383
"@angular/common": ">=16.0.0",
8484
"@angular/core": ">=16.0.0"
85+
},
86+
"publishConfig": {
87+
"directory": "dist",
88+
"linkDirectory": false
8589
}
8690
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
4+
/**
5+
* Prepack script that prepares the package for publishing by:
6+
* 1. Creating a modified package.json without dev dependencies, publishConfig and build scripts
7+
* 2. Updating file paths to remove 'dist/' prefixes (since files will be at root in published package)
8+
* 3. Writing this modified package.json to the `dist` directory
9+
* 4. Copying additional files like README.md to the dist directory
10+
*
11+
* Type declarations need to be in the package root or corresponding sub-path to support
12+
* sub-path exports in applications still using `moduleResolution: node`.
13+
*/
14+
115
console.log('Running prepack script')
16+
17+
/**
18+
* Files to copy to the dist directory
19+
* @type {string[]}
20+
*/
21+
const FILES_TO_COPY = ['README.md']
22+
23+
/**
24+
* Fields to remove from the package.json copy
25+
* @type {string[]}
26+
*/
27+
const FIELDS_TO_REMOVE = [
28+
'devDependencies',
29+
'files',
30+
'publishConfig',
31+
'scripts',
32+
]
33+
34+
/**
35+
* Replaces 'dist/' or './dist/' prefix from a file path with './'
36+
* Only matches at the start of the path to avoid false matches
37+
* @param {string} filePath - The file path to process
38+
* @returns {string} The path without dist prefix
39+
*/
40+
function replaceDist(filePath) {
41+
// Only match dist/ at the beginning of the path, followed by a filename
42+
// This prevents matching strings like "distributed/file.js" or "some/dist/path"
43+
return filePath.replace(/^(?:\.\/)?dist\/(?=.+)/, './')
44+
}
45+
46+
/**
47+
* Recursively processes package.json `exports` to remove dist prefixes
48+
* @param {Record<string, any>} exports - The exports object to process
49+
* @returns {Record<string, any>} The processed exports object
50+
*/
51+
function processExports(exports) {
52+
return Object.fromEntries(
53+
Object.entries(exports).map(([key, value]) => [
54+
key,
55+
typeof value === 'string'
56+
? replaceDist(value)
57+
: typeof value === 'object' && value !== null
58+
? processExports(value)
59+
: value,
60+
]),
61+
)
62+
}
63+
64+
console.log('Copying modified package.json')
65+
66+
/** @type {Record<string, any>} */
67+
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
68+
69+
const modifiedPackageJson = { ...packageJson }
70+
71+
if (modifiedPackageJson.types) {
72+
modifiedPackageJson.types = replaceDist(modifiedPackageJson.types)
73+
}
74+
75+
if (modifiedPackageJson.module) {
76+
modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
77+
}
78+
79+
if (modifiedPackageJson.exports) {
80+
modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
81+
}
82+
83+
for (const field of FIELDS_TO_REMOVE) {
84+
delete modifiedPackageJson[field]
85+
}
86+
87+
if (!fs.existsSync('dist')) {
88+
fs.mkdirSync('dist', { recursive: true })
89+
}
90+
91+
fs.writeFileSync(
92+
path.join('dist', 'package.json'),
93+
JSON.stringify(modifiedPackageJson, null, 2),
94+
)
95+
96+
console.log('Copying other files')
97+
for (const file of FILES_TO_COPY) {
98+
if (fs.existsSync(file)) {
99+
fs.copyFileSync(file, path.join('dist', file))
100+
console.log(`${file}`)
101+
} else {
102+
console.log(`${file} not found, skipping`)
103+
}
104+
}
105+
106+
console.log('prepack complete')

packages/angular-query-experimental/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"useDefineForClassFields": false,
88
"target": "ES2022"
99
},
10-
"include": ["src", "*.config.js", "*.config.ts", "package.json"],
10+
"include": ["src", "scripts", "*.config.js", "*.config.ts", "package.json"],
1111
"references": [{ "path": "../query-core" }, { "path": "../query-devtools" }]
1212
}

pnpm-lock.yaml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)