Open History to see the last commit
1import fs from 'node:fs'
2import 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β
15console.log('Running prepack script')
16β
17/**
18 * Files to copy to the dist directory
19 * @type {string[]}
20 */
21const FILES_TO_COPY = ['README.md']
22β
23/**
24 * Fields to remove from the package.json copy
25 * @type {string[]}
26 */
27const 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 */
40function 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 */
51function processExports(exports) {
52 return Object.fromEntries(
53 Object.entries(exports)
54 .filter(([key]) => key !== '@tanstack/custom-condition')
55 .map(([key, value]) => [
56 key,
57 typeof value === 'string'
58 ? replaceDist(value)
59 : typeof value === 'object' && value !== null
60 ? processExports(value)
61 : value,
62 ]),
63 )
64}
65β
66console.log('Copying modified package.json')
67β
68/** @type {Record<string, any>} */
69const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
70β
71const modifiedPackageJson = { ...packageJson }
72β
73if (modifiedPackageJson.types) {
74 modifiedPackageJson.types = replaceDist(modifiedPackageJson.types)
75}
76β
77if (modifiedPackageJson.module) {
78 modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
79}
80β
81if (modifiedPackageJson.exports) {
82 modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
83}
84β
85for (const field of FIELDS_TO_REMOVE) {
86 delete modifiedPackageJson[field]
87}
88β
89if (!fs.existsSync('dist')) {
90 fs.mkdirSync('dist', { recursive: true })
91}
92β
93fs.writeFileSync(
94 path.join('dist', 'package.json'),
95 JSON.stringify(modifiedPackageJson, null, 2),
96)
97β
98console.log('Copying other files')
99for (const file of FILES_TO_COPY) {
100 if (fs.existsSync(file)) {
101 fs.copyFileSync(file, path.join('dist', file))
102 console.log(`${file}`)
103 } else {
104 console.log(`${file} not found, skipping`)
105 }
106}
107β
108console.log('prepack complete')