Skip to content

Commit

Permalink
wip: build setup
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 8, 2020
1 parent e44d77d commit 1c12e26
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 30 deletions.
22 changes: 11 additions & 11 deletions .github/maintenance.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Vite Maintenance Principles

## Ensure type support

Vite aims to be fully usable as a dependency in a TypeScript project (e.g. it should provide proper typings for VitePress), and also in `vite.config.ts`. This means any types that are exposed needs to be part of `dependencies` instead of `devDependencies`. For example, if a config option uses an imported type from a `@types/x` package, that type package should be included as a dependency (e.g. `@types/http-proxy`)

On the contrary, if a dependency's type isn't exposed, then its typing should be left in `devDependencies` to reduce user dependency downloads (e.g. `@types/ws`).

## Think before adding a dependency

Vite aims to be lightweight, and this includes being aware of the number of npm dependencies and their size.

Note we pre-bundle most dependencies with rollup before publishing! Therefore most non-type dependencies should be added under `devDependencies` by default.
We use rollup to pre-bundle most dependencies before publishing! Therefore most dependencies, even used in src code, should be added under `devDependencies` by default.

Avoid dependencies that:
Some exceptions are:

- Cannot be properly bundled due to native deps. If it must be included due to critical functionality, put it under `dependencies` (auto excluded during rollup build). Example: `esbuild`.
- Type packages. Example: `@types/*`.
- Deps that cannot be properly bundled due to binary files. Example: `esbuild`.
- Deps that ships its own types and its type is used in vite's own public types. Example: `rollup`.

- Simple enought that it can be substituted with a local helper (e.g. one-function packages like `p-map-series`)
Avoid deps that has large transitive dependencies that results in bloated size compared to the functionality it provides. For example, `http-proxy` itself plus `@types/http-proxy` is a little over 1MB in size, but `http-proxy-middleware` pulls in a ton of dependencies that makes it 7MB(!) when a minimal custom middleware on top of `http-proxy` only requires a couple lines of code.

- Has large transitive dependencies that results in bloated `node_modules` size compared to the functionality it provides. For example, `http-proxy` itself plus `@types/http-proxy` is a little over 1MB in size, but `http-proxy-middleware` pulls in a ton of dependencies that makes it 7MB(!) when a minimal custom middleware on top of `http-proxy` only requires a couple lines of code.
## Ensure type support

Vite aims to be fully usable as a dependency in a TypeScript project (e.g. it should provide proper typings for VitePress), and also in `vite.config.ts`. This means any types that are exposed needs to be part of `dependencies` instead of `devDependencies`. For example, if a config option uses an imported type from a `@types/x` package, that type package should be included as a dependency (e.g. `@types/http-proxy`)

On the contrary, if a dependency's type isn't exposed, then its typing should be left in `devDependencies` to reduce user dependency downloads (e.g. `@types/ws`).

## Think before adding yet another option

Expand Down
8 changes: 5 additions & 3 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
"dev": "run-p dev-client dev-node",
"dev-client": "tsc -w --incremental --p src/client",
"dev-node": "tsc -w --incremental --p src/node",
"build": "run-s build-src build-types",
"build-src": "rimraf dist && rollup -c",
"build-types": "tsc --emitDeclarationOnly --outDir dist-types -p src/node && api-extractor run && rimraf dist-types",
"prebuild": "rimraf dist",
"build": "run-s build-bundle build-types",
"build-bundle": "rollup -c",
"build-types": "tsc --emitDeclarationOnly --outDir dist-types -p src/node && api-extractor run && rimraf dist-types && node scripts/patchTypes",
"lint": "prettier --write --parser typescript \"src/**/*.ts\"",
"pretest": "yarn build-src",
"test": "jest --clearCache && jest --runInBand --forceExit",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js"
},
"//": "READ .github/maintenance.md to understand what to put under deps vs. devDeps!",
"dependencies": {
"@types/connect": "^3.4.33",
"@types/http-proxy": "^1.17.4",
Expand Down
22 changes: 19 additions & 3 deletions packages/vite/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@ import MagicString from 'magic-string'
/**
* @type { import('rollup').RollupOptions }
*/
const config = {
const clientConfig = {
input: path.resolve(__dirname, 'src/client/client.ts'),
plugins: [
typescript({
target: 'es2018',
include: ['src/client/**/*.ts']
})
],
output: {
dir: path.resolve(__dirname, 'dist/client')
}
}

/**
* @type { import('rollup').RollupOptions }
*/
const nodeConfig = {
input: {
index: path.resolve(__dirname, 'src/node/index.ts'),
cli: path.resolve(__dirname, 'src/node/cli.ts'),
Expand All @@ -26,7 +42,7 @@ const config = {
shimCac(),
typescript({
target: 'es2019',
module: 'esnext',
include: ['src/**/*.ts'],
esModuleInterop: true
}),
commonjs(),
Expand Down Expand Up @@ -173,4 +189,4 @@ function shimCac() {
}
}

export default config
export default [clientConfig, nodeConfig]
11 changes: 11 additions & 0 deletions packages/vite/scripts/patchTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs')
const path = require('path')

const importMetaDts = fs.readFileSync(
path.resolve(__dirname, '../src/client/importMeta.d.ts'),
'utf-8'
)

const indexPath = path.resolve(__dirname, '../dist/vite.d.ts')
const content = fs.readFileSync(indexPath, 'utf-8')
fs.writeFileSync(indexPath, content + `\n` + importMetaDts)
Loading

0 comments on commit 1c12e26

Please sign in to comment.