Skip to content

refactor(angular-query): restructure package type declaration location #9519

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/angular-query-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"tanstack"
],
"scripts": {
"clean": "premove ./build ./dist ./coverage ./dist-ts",
"clean": "premove ./dist ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "npm-run-all --serial test:types:*",
Expand Down Expand Up @@ -59,9 +59,9 @@
},
"sideEffects": false,
"files": [
"dist",
"src",
"!src/__tests__"
"**/*.d.ts",
"**/*.mjs",
"**/*.mjs.map"
],
"dependencies": {
"@tanstack/query-core": "workspace:*",
Expand All @@ -82,5 +82,9 @@
"peerDependencies": {
"@angular/common": ">=16.0.0",
"@angular/core": ">=16.0.0"
},
"publishConfig": {
"directory": "dist",
"linkDirectory": false
}
}
105 changes: 105 additions & 0 deletions packages/angular-query-experimental/scripts/prepack.js
Original file line number Diff line number Diff line change
@@ -1 +1,106 @@
import fs from 'node:fs'
import path from 'node:path'

/**
* Prepack script that prepares the package for publishing by:
* 1. Creating a modified package.json without dev dependencies, publishConfig and build scripts
* 2. Updating file paths to remove 'dist/' prefixes (since files will be at root in published package)
* 3. Writing this modified package.json to the `dist` directory
* 4. Copying additional files like README.md to the dist directory
*
* Type declarations need to be in the package root or corresponding sub-path to support
* sub-path exports in applications still using `moduleResolution: node`.
*/

console.log('Running prepack script')

/**
* Files to copy to the dist directory
* @type {string[]}
*/
const FILES_TO_COPY = ['README.md']

/**
* Fields to remove from the package.json copy
* @type {string[]}
*/
const FIELDS_TO_REMOVE = [
'devDependencies',
'files',
'publishConfig',
'scripts',
]

/**
* Replaces 'dist/' or './dist/' prefix from a file path with './'
* Only matches at the start of the path to avoid false matches
* @param {string} filePath - The file path to process
* @returns {string} The path without dist prefix
*/
function replaceDist(filePath) {
// Only match dist/ at the beginning of the path, followed by a filename
// This prevents matching strings like "distributed/file.js" or "some/dist/path"
return filePath.replace(/^(?:\.\/)?dist\/(?=.+)/, './')
}

/**
* Recursively processes package.json `exports` to remove dist prefixes
* @param {Record<string, any>} exports - The exports object to process
* @returns {Record<string, any>} The processed exports object
*/
function processExports(exports) {
return Object.fromEntries(
Object.entries(exports).map(([key, value]) => [
key,
typeof value === 'string'
? replaceDist(value)
: typeof value === 'object' && value !== null
? processExports(value)
: value,
]),
)
}

console.log('Copying modified package.json')

/** @type {Record<string, any>} */
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))

const modifiedPackageJson = { ...packageJson }

if (modifiedPackageJson.types) {
modifiedPackageJson.types = replaceDist(modifiedPackageJson.types)
}

if (modifiedPackageJson.module) {
modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
}

if (modifiedPackageJson.exports) {
modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
}

for (const field of FIELDS_TO_REMOVE) {
delete modifiedPackageJson[field]
}

if (!fs.existsSync('dist')) {
fs.mkdirSync('dist', { recursive: true })
}

fs.writeFileSync(
path.join('dist', 'package.json'),
JSON.stringify(modifiedPackageJson, null, 2),
)

console.log('Copying other files')
for (const file of FILES_TO_COPY) {
if (fs.existsSync(file)) {
fs.copyFileSync(file, path.join('dist', file))
console.log(`${file}`)
} else {
console.log(`${file} not found, skipping`)
}
}

console.log('prepack complete')
2 changes: 1 addition & 1 deletion packages/angular-query-experimental/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"useDefineForClassFields": false,
"target": "ES2022"
},
"include": ["src", "*.config.js", "*.config.ts", "package.json"],
"include": ["src", "scripts", "*.config.js", "*.config.ts", "package.json"],
"references": [{ "path": "../query-core" }, { "path": "../query-devtools" }]
}
1 change: 1 addition & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading