Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

prepack.js

117 lines3.3 KB

1import fs from 'node:fs'
2import path from 'node:path'
3import './inject-schematic-package-info.js'
4​
5/**
6 * Prepack script that prepares the package for publishing by:
7 * 1. Creating a modified package.json without dev dependencies, publishConfig and build scripts
8 * 2. Updating file paths to remove 'dist/' prefixes (since files will be at root in published package)
9 * 3. Writing this modified package.json to the `dist` directory
10 * 4. Copying additional files like README.md to the dist directory
11 *
12 * Type declarations need to be in the package root or corresponding sub-path to support
13 * sub-path exports in applications still using `moduleResolution: node`.
14 */
15​
16console.log('Running prepack script')
17​
18/**
19 * Files to copy to the dist directory
20 * @type {string[]}
21 */
22const FILES_TO_COPY = [
23 'README.md',
24 'schematics/collection.json',
25 'schematics/package.json',
26 'schematics/ng-add/schema.json',
27 'schematics/migrate-from-experimental/schema.json',
29​
30/**
31 * Fields to remove from the package.json copy
32 * @type {string[]}
33 */
34const FIELDS_TO_REMOVE = [
35 'devDependencies',
36 'files',
37 'publishConfig',
38 'scripts',
40​
41/**
42 * Replaces 'dist/' or './dist/' prefix from a file path with './'
43 * Only matches at the start of the path to avoid false matches
44 * @param {string} filePath - The file path to process
45 * @returns {string} The path without dist prefix
46 */
47function replaceDist(filePath) {
48 // Only match dist/ at the beginning of the path, followed by a filename
49 // This prevents matching strings like "distributed/file.js" or "some/dist/path"
50 return filePath.replace(/^(?:\.\/)?dist\/(?=.+)/, './')
52​
53/**
54 * Recursively processes package.json `exports` to remove dist prefixes
55 * @param {Record<string, any>} exports - The exports object to process
56 * @returns {Record<string, any>} The processed exports object
57 */
58function processExports(exports) {
59 return Object.fromEntries(
60 Object.entries(exports)
61 .filter(([key]) => key !== '@tanstack/custom-condition')
62 .map(([key, value]) => [
63 key,
64 typeof value === 'string'
65 ? replaceDist(value)
66 : typeof value === 'object' && value !== null
67 ? processExports(value)
68 : value,
69 ]),
70 )
72​
73console.log('Copying modified package.json')
74​
75/** @type {Record<string, any>} */
76const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
77​
78const modifiedPackageJson = { ...packageJson }
79​
80if (modifiedPackageJson.types) {
81 modifiedPackageJson.types = replaceDist(modifiedPackageJson.types)
83​
84if (modifiedPackageJson.module) {
85 modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
87​
88if (modifiedPackageJson.exports) {
89 modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
91​
92for (const field of FIELDS_TO_REMOVE) {
93 delete modifiedPackageJson[field]
95​
96if (!fs.existsSync('dist')) {
97 fs.mkdirSync('dist', { recursive: true })
99​
100fs.writeFileSync(
101 path.join('dist', 'package.json'),
102 JSON.stringify(modifiedPackageJson, null, 2),
104​
105console.log('Copying other files')
106for (const file of FILES_TO_COPY) {
107 if (fs.existsSync(file)) {
108 const destination = path.join('dist', file)
109 fs.mkdirSync(path.dirname(destination), { recursive: true })
110 fs.copyFileSync(file, destination)
111 console.log(`${file}`)
112 } else {
113 console.log(`${file} not found, skipping`)
114 }
116​
117console.log('prepack complete')