Open History to see the last commit
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]
28β
29/**
30 * Fields to remove from the package.json copy
31 * @type {string[]}
32 */
33const FIELDS_TO_REMOVE = [
34 'devDependencies',
35 'files',
36 'publishConfig',
37 'scripts',
38]
39β
40/**
41 * Replaces 'dist/' or './dist/' prefix from a file path with './'
42 * Only matches at the start of the path to avoid false matches
43 * @param {string} filePath - The file path to process
44 * @returns {string} The path without dist prefix
45 */
46function replaceDist(filePath) {
47 // Only match dist/ at the beginning of the path, followed by a filename
48 // This prevents matching strings like "distributed/file.js" or "some/dist/path"
49 return filePath.replace(/^(?:\.\/)?dist\/(?=.+)/, './')
50}
51β
52/**
53 * Recursively processes package.json `exports` to remove dist prefixes
54 * @param {Record<string, any>} exports - The exports object to process
55 * @returns {Record<string, any>} The processed exports object
56 */
57function processExports(exports) {
58 return Object.fromEntries(
59 Object.entries(exports)
60 .filter(([key]) => key !== '@tanstack/custom-condition')
61 .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 )
70}
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)
81}
82β
83if (modifiedPackageJson.module) {
84 modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
85}
86β
87if (modifiedPackageJson.exports) {
88 modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
89}
90β
91for (const field of FIELDS_TO_REMOVE) {
92 delete modifiedPackageJson[field]
93}
94β
95if (!fs.existsSync('dist')) {
96 fs.mkdirSync('dist', { recursive: true })
97}
98β
99fs.writeFileSync(
100 path.join('dist', 'package.json'),
101 JSON.stringify(modifiedPackageJson, null, 2),
102)
103β
104console.log('Copying other files')
105for (const file of FILES_TO_COPY) {
106 if (fs.existsSync(file)) {
107 const destination = path.join('dist', file)
108 fs.mkdirSync(path.dirname(destination), { recursive: true })
109 fs.copyFileSync(file, destination)
110 console.log(`${file}`)
111 } else {
112 console.log(`${file} not found, skipping`)
113 }
114}
115β
116console.log('prepack complete')