Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: use rollup-plugin-dts #14571

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"devDependencies": {
"@babel/types": "^7.23.0",
"@microsoft/api-extractor": "^7.38.0",
"@rollup/plugin-typescript": "^11.1.5",
"@types/babel__core": "^7.20.2",
"@types/babel__preset-env": "^7.9.3",
Expand Down
56 changes: 0 additions & 56 deletions packages/vite/api-extractor.json

This file was deleted.

12 changes: 4 additions & 8 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@
"dev": "rimraf dist && pnpm run build-bundle -w",
"build": "rimraf dist && run-s build-bundle build-types",
"build-bundle": "rollup --config rollup.config.ts --configPlugin typescript",
"build-types": "run-s build-types-temp build-types-pre-patch build-types-roll build-types-post-patch build-types-check",
"build-types": "run-s build-types-temp build-types-roll",
"build-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"build-types-pre-patch": "tsx scripts/prePatchTypes.ts",
"build-types-roll": "tsx scripts/api-extractor.ts run && rimraf temp",
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
"build-types-check": "tsx scripts/checkBuiltTypes.ts && tsc --project tsconfig.check.json",
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
"build-types-roll": "rollup --config rollup.dts.config.ts --configPlugin typescript && rimraf temp",
"typecheck": "tsc --noEmit",
"lint": "eslint --cache --ext .ts src/**",
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
Expand All @@ -78,15 +75,13 @@
"dependencies": {
"esbuild": "^0.19.3",
"postcss": "^8.4.31",
"rollup": "^3.29.2"
"rollup": "^3.29.4"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
},
"devDependencies": {
"@ampproject/remapping": "^2.2.1",
"@babel/parser": "^7.23.0",
"@babel/types": "^7.23.0",
"@jridgewell/trace-mapping": "^0.3.19",
"@rollup/plugin-alias": "^5.0.1",
"@rollup/plugin-commonjs": "^25.0.5",
Expand Down Expand Up @@ -134,6 +129,7 @@
"postcss-load-config": "^4.0.1",
"postcss-modules": "^6.0.0",
"resolve.exports": "^2.0.2",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-license": "^3.1.0",
"sirv": "^2.0.3",
"source-map-support": "^0.5.21",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function createNodeConfig(isProduction: boolean) {
plugins: createNodePlugins(
isProduction,
!isProduction,
// in production we use api-extractor for dts generation
// in production we use rollup.dts.config.ts for dts generation
// in development we need to rely on the rollup ts plugin
isProduction ? false : './dist/node',
),
Expand Down
84 changes: 84 additions & 0 deletions packages/vite/rollup.dts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { type Plugin, defineConfig } from 'rollup'
import dts from 'rollup-plugin-dts'

const depTypesDir = new URL('./src/types/', import.meta.url)
const pkg = JSON.parse(
readFileSync(new URL('./package.json', import.meta.url)).toString(),
)

export default defineConfig({
input: './temp/node/index.d.ts',
output: {
file: './dist/node/index.d.ts',
format: 'es',
},
external: [
/^node:*/,
...Object.keys(pkg.dependencies),
// lightningcss types are bundled
...Object.keys(pkg.devDependencies).filter((d) => d !== 'lightningcss'),
],
plugins: [patchTypes(), dts({ respectExternal: true })],
})

// Taken from https://stackoverflow.com/a/36328890
const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g
const singlelineCommentsRE = /\/\/[^/].*/g
const licenseCommentsRE = /MIT License|MIT license|BSD license/
const consecutiveNewlinesRE = /\n{2,}/g

/**
* Patch the types files before passing to dts plugin
* 1. Resolve `dep-types/*` and `types/*` imports
* 2. Validate unallowed dependency imports
* 3. Clean unnecessary comments
*/
function patchTypes(): Plugin {
return {
name: 'patch-types',
resolveId(id) {
// Dep types should be bundled
if (id.startsWith('dep-types/')) {
const fileUrl = new URL(
`./${id.slice('dep-types/'.length)}.d.ts`,
depTypesDir,
)
return fileURLToPath(fileUrl)
}
// Ambient types are unbundled and externalized
if (id.startsWith('types/')) {
return {
id: '../../' + (id.endsWith('.js') ? id : id + '.js'),
external: true,
}
}
},
renderChunk(code, chunk) {
const deps = new Set(Object.keys(pkg.dependencies))
// Validate that chunk imports do not import dev deps
for (const id of chunk.imports) {
if (
!id.startsWith('./') &&
!id.startsWith('../') &&
!id.startsWith('node:') &&
!deps.has(id)
) {
// If validation failed, only warn and set exit code 1 so that files
// are written to disk for inspection, but the build will fail
this.warn(`${chunk.fileName} imports "${id}" which is not allowed`)
process.exitCode = 1
}
}

// Clean unnecessary comments
return code
.replace(singlelineCommentsRE, '')
.replace(multilineCommentsRE, (m) => {
return licenseCommentsRE.test(m) ? '' : m
})
.replace(consecutiveNewlinesRE, '\n\n')
},
}
}
25 changes: 0 additions & 25 deletions packages/vite/scripts/api-extractor.ts

This file was deleted.

101 changes: 0 additions & 101 deletions packages/vite/scripts/checkBuiltTypes.ts

This file was deleted.

34 changes: 0 additions & 34 deletions packages/vite/scripts/postPatchTypes.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/vite/scripts/prePatchTypes.ts

This file was deleted.

Loading