Skip to content

Commit e845c15

Browse files
chore: 🤖 optimize build script (#1788)
* chore: 🤖 optimize build script * chore: 🤖 remove unused start scripts * fix: 🐛 build script * chore: fix * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 16a3dfa commit e845c15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+317
-1358
lines changed

Diff for: .changeset/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"access": "public",
88
"baseBranch": "main",
99
"updateInternalDependencies": "patch",
10-
"ignore": ["@milkdown/e2e", "@milkdown/storybook"],
10+
"ignore": ["@milkdown/e2e", "@milkdown/storybook", "@milkdown/dev"],
1111
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
1212
"onlyUpdatePeerDependentsWhenOutOfRange": true
1313
}

Diff for: dev/package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@milkdown/dev",
3+
"private": true,
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"import": "./lib/index.js",
8+
"types": "./lib/index.d.ts"
9+
},
10+
"./vite": {
11+
"import": "./lib/vite.js",
12+
"types": "./lib/vite.d.ts"
13+
}
14+
},
15+
"scripts": {
16+
"build": "rimraf './lib' && tsc"
17+
},
18+
"nx": {
19+
"targets": {
20+
"build": {
21+
"outputs": [
22+
"{projectRoot}/lib"
23+
],
24+
"dependsOn": [
25+
"build"
26+
]
27+
},
28+
"tsc": {
29+
"outputs": [],
30+
"dependsOn": [
31+
"build"
32+
]
33+
}
34+
}
35+
}
36+
}

Diff for: dev/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

Diff for: dev/src/vite.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { readFileSync } from 'node:fs'
2+
import { basename, dirname, resolve } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import type { BuildOptions, UserConfig } from 'vite'
5+
import { defineConfig } from 'vite'
6+
7+
import globalPackageJson from '../../package.json' with { type: 'json' }
8+
9+
const external = [/@milkdown/]
10+
11+
const libFileName = (format: string) => `index.${format}.js`
12+
13+
function isObject(item: unknown): item is Record<string, unknown> {
14+
return Boolean(item && typeof item === 'object' && !Array.isArray(item))
15+
}
16+
17+
function mergeDeep<T>(target: T, ...sources: T[]): T {
18+
if (!sources.length) return target
19+
const source = sources.shift()
20+
21+
if (isObject(target) && isObject(source)) {
22+
for (const key in source) {
23+
if (isObject(source[key])) {
24+
if (!target[key]) Object.assign(target, { [key]: {} })
25+
mergeDeep(target[key] as T, source[key] as T)
26+
} else {
27+
Object.assign(target, { [key]: source[key] })
28+
}
29+
}
30+
}
31+
32+
return mergeDeep(target, ...sources)
33+
}
34+
35+
function viteBuild(path: string, options: BuildOptions = {}): BuildOptions {
36+
const dir = dirname(fileURLToPath(path))
37+
const packageDirName = basename(dir)
38+
39+
const packageJson = JSON.parse(
40+
readFileSync(resolve(dir, 'package.json'), { encoding: 'utf-8' })
41+
)
42+
const deps = {
43+
...packageJson.dependencies,
44+
...packageJson.devDependencies,
45+
...packageJson.peerDependencies,
46+
...globalPackageJson.devDependencies,
47+
}
48+
return mergeDeep<BuildOptions>(
49+
{
50+
sourcemap: true,
51+
emptyOutDir: false,
52+
lib: {
53+
entry: resolve(dir, 'src', 'index.ts'),
54+
name: `milkdown_${packageDirName}`,
55+
fileName: libFileName,
56+
formats: ['es'],
57+
},
58+
rollupOptions: {
59+
external: Array.from(new Set([...Object.keys(deps), ...external])),
60+
output: {
61+
dir: resolve(dir, 'lib'),
62+
},
63+
},
64+
minify: false,
65+
terserOptions: { compress: false, mangle: false },
66+
},
67+
options
68+
)
69+
}
70+
71+
/**
72+
* Config for plugins
73+
*
74+
* @param packageDirName - package directory name
75+
* @param options - custom options
76+
* @returns user config
77+
*/
78+
export function pluginViteConfig(
79+
packageDirName: string,
80+
options: UserConfig = {}
81+
) {
82+
return defineConfig({
83+
...options,
84+
build: viteBuild(packageDirName, options.build),
85+
})
86+
}

Diff for: dev/tsconfig.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "lib"
6+
},
7+
"include": ["src"]
8+
}

0 commit comments

Comments
 (0)