Skip to content

Commit 658d5fa

Browse files
authored
refactor(angular-query): build with vite, publish d.ts files to package root (#9292)
1 parent 1a0ab99 commit 658d5fa

File tree

13 files changed

+372
-150
lines changed

13 files changed

+372
-150
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"ignoreRules": ["cjs-resolves-to-esm", "no-resolution"]
2+
"ignoreRules": ["cjs-resolves-to-esm"]
33
}

packages/angular-query-experimental/package.json

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"tanstack"
2828
],
2929
"scripts": {
30-
"clean": "premove ./build ./coverage ./dist-ts",
30+
"clean": "premove ./dist ./coverage ./dist-ts",
3131
"compile": "tsc --build",
3232
"test:eslint": "eslint ./src",
3333
"test:types": "npm-run-all --serial test:types:*",
@@ -37,33 +37,30 @@
3737
"test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js --build",
3838
"test:types:ts54": "node ../../node_modules/typescript54/lib/tsc.js --build",
3939
"test:types:ts55": "node ../../node_modules/typescript55/lib/tsc.js --build",
40-
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js --build",
41-
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --build",
40+
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js --build",
41+
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --build",
4242
"test:types:tscurrent": "tsc --build",
4343
"test:lib": "vitest",
4444
"test:lib:dev": "pnpm run test:lib --watch",
45-
"test:build": "publint --strict && attw --pack",
46-
"build": "pnpm build:tsup",
47-
"build:tsup": "tsup --tsconfig tsconfig.prod.json"
45+
"test:build": "pnpm pack && publint ./dist/*.tgz --strict && attw ./dist/*.tgz; premove ./dist/*.tgz",
46+
"build": "vite build",
47+
"prepack": "node ./scripts/prepack.js"
4848
},
4949
"type": "module",
50-
"types": "build/index.d.ts",
51-
"module": "build/index.mjs",
50+
"types": "dist/index.d.ts",
51+
"module": "dist/index.mjs",
5252
"exports": {
5353
".": {
5454
"@tanstack/custom-condition": "./src/index.ts",
55-
"types": "./build/index.d.ts",
56-
"default": "./build/index.mjs"
55+
"types": "./dist/index.d.ts",
56+
"default": "./dist/index.mjs"
5757
},
58-
"./package.json": {
59-
"default": "./package.json"
60-
}
58+
"./package.json": "./package.json"
6159
},
6260
"sideEffects": false,
6361
"files": [
64-
"build",
65-
"src",
66-
"!src/__tests__"
62+
"**/*.d.ts",
63+
"**/*.mjs.*"
6764
],
6865
"dependencies": {
6966
"@tanstack/query-core": "workspace:*",
@@ -74,13 +71,19 @@
7471
"@angular/compiler": "^20.0.0",
7572
"@angular/core": "^20.0.0",
7673
"@angular/platform-browser": "^20.0.0",
77-
"@angular/platform-browser-dynamic": "^20.0.0",
7874
"@tanstack/query-test-utils": "workspace:*",
7975
"eslint-plugin-jsdoc": "^50.5.0",
80-
"npm-run-all2": "^5.0.0"
76+
"npm-run-all2": "^5.0.0",
77+
"vite-plugin-dts": "4.2.3",
78+
"vite-plugin-externalize-deps": "^0.9.0",
79+
"vite-tsconfig-paths": "^5.1.4"
8180
},
8281
"peerDependencies": {
8382
"@angular/common": ">=16.0.0",
8483
"@angular/core": ">=16.0.0"
84+
},
85+
"publishConfig": {
86+
"directory": "dist",
87+
"linkDirectory": false
8588
}
8689
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
4+
console.log('Running prepack script')
5+
6+
/**
7+
* Files to copy to the dist directory
8+
* @type {string[]}
9+
*/
10+
const FILES_TO_COPY = ['README.md']
11+
12+
/**
13+
* Fields to remove from the package.json copy
14+
* @type {string[]}
15+
*/
16+
const FIELDS_TO_REMOVE = [
17+
'devDependencies',
18+
'files',
19+
'publishConfig',
20+
'scripts',
21+
]
22+
23+
/**
24+
* Replaces 'dist/' or './dist/' prefix from a file path with './'
25+
* @param {string} filePath - The file path to process
26+
* @returns {string} The path without dist prefix
27+
*/
28+
function removeDist(filePath) {
29+
return filePath.replace(/^(\.\/)?dist\//, './')
30+
}
31+
32+
/**
33+
* Recursively processes exports object to remove dist prefixes
34+
* @param {Record<string, any>} exports - The exports object to process
35+
* @returns {Record<string, any>} The processed exports object
36+
*/
37+
function processExports(exports) {
38+
return Object.fromEntries(
39+
Object.entries(exports).map(([key, value]) => [
40+
key,
41+
typeof value === 'string'
42+
? removeDist(value)
43+
: typeof value === 'object' && value !== null
44+
? processExports(value)
45+
: value,
46+
]),
47+
)
48+
}
49+
50+
console.log('Copying modified package.json')
51+
52+
/** @type {Record<string, any>} */
53+
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
54+
55+
const distPackageJson = { ...packageJson }
56+
57+
if (distPackageJson.types) {
58+
distPackageJson.types = removeDist(distPackageJson.types)
59+
}
60+
61+
if (distPackageJson.module) {
62+
distPackageJson.module = removeDist(distPackageJson.module)
63+
}
64+
65+
if (distPackageJson.exports) {
66+
distPackageJson.exports = processExports(distPackageJson.exports)
67+
}
68+
69+
for (const field of FIELDS_TO_REMOVE) {
70+
delete distPackageJson[field]
71+
}
72+
73+
if (!fs.existsSync('dist')) {
74+
fs.mkdirSync('dist', { recursive: true })
75+
}
76+
77+
fs.writeFileSync(
78+
path.join('dist', 'package.json'),
79+
JSON.stringify(distPackageJson, null, 2),
80+
)
81+
82+
console.log('Copying other files')
83+
for (const fileName of FILES_TO_COPY) {
84+
if (fs.existsSync(fileName)) {
85+
fs.copyFileSync(fileName, path.join('dist', fileName))
86+
console.log(`${fileName}`)
87+
} else {
88+
console.log(`${fileName} not found, skipping`)
89+
}
90+
}
91+
92+
console.log('prepack complete')
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import {
2-
BrowserDynamicTestingModule,
3-
platformBrowserDynamicTesting,
4-
} from '@angular/platform-browser-dynamic/testing'
51
import { getTestBed } from '@angular/core/testing'
2+
import {
3+
BrowserTestingModule,
4+
platformBrowserTesting,
5+
} from '@angular/platform-browser/testing'
66

7-
getTestBed().initTestEnvironment(
8-
BrowserDynamicTestingModule,
9-
platformBrowserDynamicTesting(),
10-
)
7+
getTestBed().initTestEnvironment(BrowserTestingModule, platformBrowserTesting())

packages/angular-query-experimental/tsconfig.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"compilerOptions": {
44
"outDir": "./dist-ts",
55
"rootDir": ".",
6-
"noImplicitOverride": true,
7-
"noPropertyAccessFromIndexSignature": true,
86
"noFallthroughCasesInSwitch": true,
97
"useDefineForClassFields": false,
108
"target": "ES2022"

packages/angular-query-experimental/tsconfig.prod.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"compilerOptions": {
44
"incremental": false,
55
"composite": false,
6-
"rootDir": "../../"
6+
"rootDir": "../../",
7+
"customConditions": null
78
}
89
}

packages/angular-query-experimental/tsup.config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/angular-query-experimental/vite.config.ts

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1-
import { defineConfig } from 'vitest/config'
2-
1+
import { defineConfig, mergeConfig } from 'vitest/config'
2+
import { externalizeDeps } from 'vite-plugin-externalize-deps'
3+
import tsconfigPaths from 'vite-tsconfig-paths'
4+
import dts from 'vite-plugin-dts'
35
import packageJson from './package.json'
6+
import type { Options } from '@tanstack/config/vite'
7+
8+
function ensureImportFileExtension({
9+
content,
10+
extension,
11+
}: {
12+
content: string
13+
extension: string
14+
}) {
15+
// replace e.g. `import { foo } from './foo'` with `import { foo } from './foo.js'`
16+
content = content.replace(
17+
/(im|ex)port\s[\w{}/*\s,]+from\s['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm,
18+
`$&.${extension}`,
19+
)
20+
21+
// replace e.g. `import('./foo')` with `import('./foo.js')`
22+
content = content.replace(
23+
/import\(['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm,
24+
`$&.${extension}`,
25+
)
26+
return content
27+
}
428

5-
export default defineConfig({
29+
const config = defineConfig({
630
// fix from https://github.com/vitest-dev/vitest/issues/6992#issuecomment-2509408660
731
resolve: {
832
conditions: ['@tanstack/custom-condition'],
@@ -26,3 +50,70 @@ export default defineConfig({
2650
restoreMocks: true,
2751
},
2852
})
53+
54+
// copy from @tanstack/config/vite with changes:
55+
// - build - lib - fileName: [name.mjs]
56+
// - rollup - output - preserveModulesRoot: src
57+
export const tanstackViteConfig = (options: Options) => {
58+
const outDir = options.outDir ?? 'dist'
59+
const cjs = options.cjs ?? true
60+
61+
return defineConfig({
62+
plugins: [
63+
externalizeDeps({ include: options.externalDeps ?? [] }),
64+
tsconfigPaths({
65+
projects: options.tsconfigPath ? [options.tsconfigPath] : undefined,
66+
}),
67+
dts({
68+
outDir,
69+
entryRoot: options.srcDir,
70+
include: options.srcDir,
71+
exclude: options.exclude,
72+
tsconfigPath: options.tsconfigPath,
73+
compilerOptions: {
74+
module: 99, // ESNext
75+
declarationMap: false,
76+
},
77+
beforeWriteFile: (filePath, content) => {
78+
return {
79+
filePath,
80+
content: ensureImportFileExtension({ content, extension: 'js' }),
81+
}
82+
},
83+
afterDiagnostic: (diagnostics) => {
84+
if (diagnostics.length > 0) {
85+
console.error('Please fix the above type errors')
86+
process.exit(1)
87+
}
88+
},
89+
}),
90+
],
91+
build: {
92+
outDir,
93+
minify: false,
94+
sourcemap: true,
95+
lib: {
96+
entry: options.entry,
97+
formats: cjs ? ['es', 'cjs'] : ['es'],
98+
fileName: () => '[name].mjs',
99+
},
100+
rollupOptions: {
101+
output: {
102+
preserveModules: true,
103+
preserveModulesRoot: 'src',
104+
},
105+
},
106+
},
107+
})
108+
}
109+
110+
export default mergeConfig(
111+
config,
112+
tanstackViteConfig({
113+
cjs: false,
114+
entry: ['./src/index.ts'],
115+
exclude: ['./src/__tests__'],
116+
srcDir: './src',
117+
tsconfigPath: './tsconfig.prod.json',
118+
}),
119+
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"ignoreRules": ["cjs-resolves-to-esm", "no-resolution"]
2+
"ignoreRules": ["cjs-resolves-to-esm"]
33
}

packages/angular-query-persist-client/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"@angular/compiler": "^20.0.0",
6363
"@angular/core": "^20.0.0",
6464
"@angular/platform-browser": "^20.0.0",
65-
"@angular/platform-browser-dynamic": "^20.0.0",
6665
"@tanstack/angular-query-experimental": "workspace:*",
6766
"@tanstack/query-test-utils": "workspace:*",
6867
"@testing-library/angular": "^17.3.7",
@@ -73,6 +72,6 @@
7372
"peerDependencies": {
7473
"@angular/common": ">=16.0.0",
7574
"@angular/core": ">=16.0.0",
76-
"@tanstack/angular-query-experimental": "workspace:*"
75+
"@tanstack/angular-query-experimental": "workspace:^"
7776
}
7877
}

0 commit comments

Comments
 (0)