Skip to content

Commit d70ec79

Browse files
committed
wip
1 parent f343b5f commit d70ec79

File tree

3 files changed

+88
-59
lines changed

3 files changed

+88
-59
lines changed

package.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@
1919
"dist",
2020
"esm-shims.js"
2121
],
22-
"main": "./dist/index.mjs",
22+
"main": "./dist/index.cjs",
2323
"module": "./dist/index.mjs",
24-
"types": "./dist/index.d.mts",
24+
"types": "./dist/index.d.cts",
2525
"exports": {
26-
".": "./dist/index.mjs",
27-
"./config": "./dist/config.mjs",
28-
"./plugins": "./dist/plugins.mjs",
29-
"./run": "./dist/run.mjs",
26+
".": {
27+
"require": "./dist/index.cjs",
28+
"import": "./dist/index.mjs"
29+
},
30+
"./config": {
31+
"require": "./dist/config.cjs",
32+
"import": "./dist/config.mjs"
33+
},
34+
"./plugins": {
35+
"require": "./dist/plugins.cjs",
36+
"import": "./dist/plugins.mjs"
37+
},
38+
"./run": {
39+
"require": "./dist/run.cjs",
40+
"import": "./dist/run.mjs"
41+
},
3042
"./package.json": "./package.json"
3143
},
3244
"typesVersions": {

src/features/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { logger, prettyName } from '../utils/logger'
33
import type { ResolvedOptions } from '../options'
44
import type { OutputAsset, OutputChunk, Plugin } from 'rolldown'
55

6-
const endsWithConfig = /[\\/](?:package\.json|tsdown\.config.*)$/
6+
const endsWithConfig = /[\\/]tsdown\.config.*$/
77

88
export function WatchPlugin(
99
chunks: Array<OutputChunk | OutputAsset>,

src/index.ts

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export async function buildSingle(
104104
let ab: AbortController | undefined
105105

106106
const { hooks, context } = await createHooks(config)
107-
let watcher: RolldownWatcher
108107

109108
await hooks.callHook('build:prepare', context)
110109
ab?.abort()
@@ -113,8 +112,9 @@ export async function buildSingle(
113112

114113
const isMultiFormat = formats.length > 1
115114
const chunks: TsdownChunks = {}
115+
const watchers: RolldownWatcher[] = []
116116

117-
async function buildByFormat(format: NormalizedFormat) {
117+
async function buildOptionsByFormat(format: NormalizedFormat) {
118118
const buildOptions = await getBuildOptions(
119119
config,
120120
format,
@@ -129,9 +129,13 @@ export async function buildSingle(
129129
buildOptions,
130130
})
131131

132-
const buildOptionsList = [buildOptions]
132+
const buildOptionsList: [
133+
format: NormalizedFormat,
134+
buildOptions: BuildOptions,
135+
][] = [[format, buildOptions]]
133136
if (format === 'cjs' && dts) {
134-
buildOptionsList.push(
137+
buildOptionsList.push([
138+
format,
135139
await getBuildOptions(
136140
config,
137141
format,
@@ -141,72 +145,85 @@ export async function buildSingle(
141145
isMultiFormat,
142146
true,
143147
),
144-
)
148+
])
145149
}
146150

147-
if (watch) {
148-
watcher = rolldownWatch(buildOptionsList)
149-
const changedFile: string[] = []
150-
let hasError = false
151-
watcher.on('change', (id, event) => {
152-
if (event.event === 'update') {
153-
changedFile.push(id)
154-
}
155-
})
156-
watcher.on('event', async (event) => {
157-
switch (event.code) {
158-
case 'START': {
151+
return buildOptionsList
152+
}
153+
const buildOptionsList = (
154+
await Promise.all(formats.map(buildOptionsByFormat))
155+
).flat()
156+
157+
if (watch) {
158+
const watcher = rolldownWatch(buildOptionsList.map((item) => item[1]))
159+
const changedFile: string[] = []
160+
let hasError = false
161+
watcher.on('change', (id, event) => {
162+
if (event.event === 'update') {
163+
changedFile.push(id)
164+
}
165+
})
166+
watcher.on('event', async (event) => {
167+
switch (event.code) {
168+
case 'START': {
169+
for (const format of formats) {
159170
chunks[format]!.length = 0
160-
hasError = false
161-
break
162171
}
172+
hasError = false
173+
break
174+
}
163175

164-
case 'END': {
165-
if (!hasError) {
166-
await postBuild()
167-
}
168-
break
176+
case 'END': {
177+
if (!hasError) {
178+
await postBuild()
169179
}
180+
break
181+
}
170182

171-
case 'BUNDLE_START': {
172-
if (changedFile.length > 0) {
173-
console.info('')
174-
logger.info(
175-
`Found ${bold(changedFile.join(', '))} changed, rebuilding...`,
176-
)
177-
}
178-
changedFile.length = 0
179-
break
183+
case 'BUNDLE_START': {
184+
if (changedFile.length > 0) {
185+
console.info('')
186+
logger.info(
187+
`Found ${bold(changedFile.join(', '))} changed, rebuilding...`,
188+
)
180189
}
190+
changedFile.length = 0
191+
break
192+
}
181193

182-
case 'BUNDLE_END': {
183-
await event.result.close()
184-
logger.success(`Rebuilt in ${event.duration}ms.`)
185-
break
186-
}
194+
case 'BUNDLE_END': {
195+
await event.result.close()
196+
logger.success(`Rebuilt in ${event.duration}ms.`)
197+
break
198+
}
187199

188-
case 'ERROR': {
189-
await event.result.close()
190-
logger.error(event.error)
191-
hasError = true
192-
break
193-
}
200+
case 'ERROR': {
201+
await event.result.close()
202+
logger.error(event.error)
203+
hasError = true
204+
break
194205
}
195-
})
196-
} else {
197-
const output = (await rolldownBuild(buildOptionsList)).flatMap(
198-
({ output }) => output,
199-
)
200-
chunks[format] = output
206+
}
207+
})
208+
watchers.push(watcher)
209+
} else {
210+
const outputs = (
211+
await rolldownBuild(buildOptionsList.map((item) => item[1]))
212+
).map(({ output }) => output)
213+
for (const [i, output] of outputs.entries()) {
214+
const format = buildOptionsList[i][0]
215+
chunks[format] ||= []
216+
chunks[format].push(...output)
201217
}
202218
}
203-
await Promise.all(formats.map(buildByFormat))
204219

205220
if (watch) {
206221
return {
207222
async [Symbol.asyncDispose]() {
208223
ab?.abort()
209-
await watcher.close()
224+
for (const watcher of watchers) {
225+
await watcher.close()
226+
}
210227
},
211228
}
212229
} else {

0 commit comments

Comments
 (0)