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

prepack.js

114 lines3.0 KB

1import fs from 'node:fs'
2import path from 'node:path'
3​
4/**
5 * Prepack script that prepares the package for publishing by:
6 *
7 * 1. Creating a modified package.json without dev dependencies, publishConfig and
8 * build scripts
9 * 2. Updating file paths to remove 'dist/' prefixes (since files will be at root
10 * in published package)
11 * 3. Writing this modified package.json to the `dist` directory
12 * 4. Copying additional files like README.md to the dist directory
13 *
14 * Type declarations need to be in the package root or corresponding sub-path to
15 * support sub-path exports in applications still using `moduleResolution:
16 * node`.
17 */
18​
19console.log('Running prepack script')
20​
21/**
22 * Files to copy to the dist directory
23 *
24 * @type {string[]}
25 */
26const FILES_TO_COPY = ['README.md']
27​
28/**
29 * Fields to remove from the package.json copy
30 *
31 * @type {string[]}
32 */
33const FIELDS_TO_REMOVE = [
34 'devDependencies',
35 'files',
36 'publishConfig',
37 'scripts',
39​
40/**
41 * Replaces 'dist/' or './dist/' prefix from a file path with './' Only matches
42 * at the start of the path to avoid false matches
43 *
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 *
56 * @param {Record<string, any>} exports - The exports object to process
57 * @returns {Record<string, any>} The processed exports object
58 */
59function processExports(exports) {
60 return Object.fromEntries(
61 Object.entries(exports).map(([key, value]) => [
62 key,
63 typeof value === 'string'
64 ? replaceDist(value)
65 : typeof value === 'object' && value !== null
66 ? processExports(value)
67 : value,
68 ]),
69 )
71​
72console.log('Copying modified package.json')
73​
74/** @type {Record<string, any>} */
75const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
76​
77const modifiedPackageJson = { ...packageJson }
78​
79if (modifiedPackageJson.types) {
80 modifiedPackageJson.types = replaceDist(modifiedPackageJson.types)
82​
83if (modifiedPackageJson.module) {
84 modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
86​
87if (modifiedPackageJson.exports) {
88 modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
90​
91for (const field of FIELDS_TO_REMOVE) {
92 delete modifiedPackageJson[field]
94​
95if (!fs.existsSync('dist')) {
96 fs.mkdirSync('dist', { recursive: true })
98​
99fs.writeFileSync(
100 path.join('dist', 'package.json'),
101 JSON.stringify(modifiedPackageJson, null, 2),
103​
104console.log('Copying other files')
105for (const file of FILES_TO_COPY) {
106 if (fs.existsSync(file)) {
107 fs.copyFileSync(file, path.join('dist', file))
108 console.log(`${file}`)
109 } else {
110 console.log(`${file} not found, skipping`)
111 }
113​
114console.log('prepack complete')