Skip to content

Commit 2e1287f

Browse files
authored
chore: use rollup types exposed from Vite (#583)
1 parent 2600cc3 commit 2e1287f

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

packages/plugin-vue/src/main.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import path from 'node:path'
22
import fs from 'node:fs'
33
import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
4-
import type { PluginContext, TransformPluginContext } from 'rollup'
54
import type { RawSourceMap } from 'source-map-js'
65
import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping'
76
import { TraceMap, eachMapping } from '@jridgewell/trace-mapping'
87
import type { EncodedSourceMap as GenEncodedSourceMap } from '@jridgewell/gen-mapping'
98
import { addMapping, fromMap, toEncodedMap } from '@jridgewell/gen-mapping'
9+
import type { Rollup } from 'vite'
1010
import { normalizePath, transformWithEsbuild } from 'vite'
1111
import {
1212
createDescriptor,
@@ -31,7 +31,7 @@ export async function transformMain(
3131
code: string,
3232
filename: string,
3333
options: ResolvedOptions,
34-
pluginContext: TransformPluginContext,
34+
pluginContext: Rollup.TransformPluginContext,
3535
ssr: boolean,
3636
customElement: boolean,
3737
) {
@@ -290,7 +290,7 @@ export async function transformMain(
290290
async function genTemplateCode(
291291
descriptor: SFCDescriptor,
292292
options: ResolvedOptions,
293-
pluginContext: PluginContext,
293+
pluginContext: Rollup.PluginContext,
294294
ssr: boolean,
295295
customElement: boolean,
296296
) {
@@ -339,7 +339,7 @@ async function genTemplateCode(
339339
async function genScriptCode(
340340
descriptor: SFCDescriptor,
341341
options: ResolvedOptions,
342-
pluginContext: PluginContext,
342+
pluginContext: Rollup.PluginContext,
343343
ssr: boolean,
344344
customElement: boolean,
345345
): Promise<{
@@ -397,7 +397,7 @@ async function genScriptCode(
397397

398398
async function genStyleCode(
399399
descriptor: SFCDescriptor,
400-
pluginContext: PluginContext,
400+
pluginContext: Rollup.PluginContext,
401401
customElement: boolean,
402402
attachedProps: [string, string][],
403403
) {
@@ -487,7 +487,7 @@ function genCSSModulesCode(
487487

488488
async function genCustomBlockCode(
489489
descriptor: SFCDescriptor,
490-
pluginContext: PluginContext,
490+
pluginContext: Rollup.PluginContext,
491491
) {
492492
let code = ''
493493
for (let index = 0; index < descriptor.customBlocks.length; index++) {
@@ -514,7 +514,7 @@ async function genCustomBlockCode(
514514
async function linkSrcToDescriptor(
515515
src: string,
516516
descriptor: SFCDescriptor,
517-
pluginContext: PluginContext,
517+
pluginContext: Rollup.PluginContext,
518518
scoped?: boolean,
519519
) {
520520
const srcFile =

packages/plugin-vue/src/style.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ExistingRawSourceMap, TransformPluginContext } from 'rollup'
21
import type { RawSourceMap } from 'source-map-js'
2+
import type { Rollup } from 'vite'
33
import { formatPostcssSourceMap } from 'vite'
44
import type { ExtendedSFCDescriptor } from './utils/descriptorCache'
55
import type { ResolvedOptions } from './index'
@@ -10,7 +10,7 @@ export async function transformStyle(
1010
descriptor: ExtendedSFCDescriptor,
1111
index: number,
1212
options: ResolvedOptions,
13-
pluginContext: TransformPluginContext,
13+
pluginContext: Rollup.TransformPluginContext,
1414
filename: string,
1515
) {
1616
const block = descriptor.styles[index]
@@ -54,7 +54,10 @@ export async function transformStyle(
5454
? await formatPostcssSourceMap(
5555
// version property of result.map is declared as string
5656
// but actually it is a number
57-
result.map as Omit<RawSourceMap, 'version'> as ExistingRawSourceMap,
57+
result.map as Omit<
58+
RawSourceMap,
59+
'version'
60+
> as Rollup.ExistingRawSourceMap,
5861
filename,
5962
)
6063
: ({ mappings: '' } as any)

packages/plugin-vue/src/template.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
SFCTemplateCompileOptions,
77
SFCTemplateCompileResults,
88
} from 'vue/compiler-sfc'
9-
import type { PluginContext, TransformPluginContext } from 'rollup'
9+
import type { Rollup } from 'vite'
1010
import { getResolvedScript, resolveScript } from './script'
1111
import { createRollupError } from './utils/error'
1212
import type { ResolvedOptions } from './index'
@@ -15,7 +15,7 @@ export async function transformTemplateAsModule(
1515
code: string,
1616
descriptor: SFCDescriptor,
1717
options: ResolvedOptions,
18-
pluginContext: TransformPluginContext,
18+
pluginContext: Rollup.TransformPluginContext,
1919
ssr: boolean,
2020
customElement: boolean,
2121
): Promise<{
@@ -56,7 +56,7 @@ export function transformTemplateInMain(
5656
code: string,
5757
descriptor: SFCDescriptor,
5858
options: ResolvedOptions,
59-
pluginContext: PluginContext,
59+
pluginContext: Rollup.PluginContext,
6060
ssr: boolean,
6161
customElement: boolean,
6262
): SFCTemplateCompileResults {
@@ -82,7 +82,7 @@ export function compile(
8282
code: string,
8383
descriptor: SFCDescriptor,
8484
options: ResolvedOptions,
85-
pluginContext: PluginContext,
85+
pluginContext: Rollup.PluginContext,
8686
ssr: boolean,
8787
customElement: boolean,
8888
) {

packages/plugin-vue/src/utils/error.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { CompilerError } from 'vue/compiler-sfc'
2-
import type { RollupError } from 'rollup'
2+
import type { Rollup } from 'vite'
33

44
export function createRollupError(
55
id: string,
66
error: CompilerError | SyntaxError,
7-
): RollupError {
7+
): Rollup.RollupError {
88
const { message, name, stack } = error
9-
const rollupError: RollupError = {
9+
const rollupError: Rollup.RollupError = {
1010
id,
1111
plugin: 'vue',
1212
message,

playground/vitestSetup.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
Logger,
99
PluginOption,
1010
ResolvedConfig,
11+
Rollup,
1112
UserConfig,
1213
ViteDevServer,
1314
} from 'vite'
@@ -19,7 +20,6 @@ import {
1920
preview,
2021
} from 'vite'
2122
import type { Browser, Page } from 'playwright-chromium'
22-
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
2323
import type { File } from 'vitest'
2424
import { beforeAll } from 'vitest'
2525

@@ -72,7 +72,7 @@ export let resolvedConfig: ResolvedConfig = undefined!
7272
export let page: Page = undefined!
7373
export let browser: Browser = undefined!
7474
export let viteTestUrl: string = ''
75-
export let watcher: RollupWatcher | undefined = undefined
75+
export let watcher: Rollup.RollupWatcher | undefined = undefined
7676

7777
declare module 'vite' {
7878
interface InlineConfig {
@@ -269,7 +269,7 @@ export async function startDefaultServe(): Promise<void> {
269269
const isWatch = !!resolvedConfig!.build.watch
270270
// in build watch,call startStaticServer after the build is complete
271271
if (isWatch) {
272-
watcher = rollupOutput as RollupWatcher
272+
watcher = rollupOutput as Rollup.RollupWatcher
273273
await notifyRebuildComplete(watcher)
274274
}
275275
// @ts-ignore
@@ -290,10 +290,10 @@ export async function startDefaultServe(): Promise<void> {
290290
* Send the rebuild complete message in build watch
291291
*/
292292
export async function notifyRebuildComplete(
293-
watcher: RollupWatcher,
294-
): Promise<RollupWatcher> {
293+
watcher: Rollup.RollupWatcher,
294+
): Promise<Rollup.RollupWatcher> {
295295
let resolveFn: undefined | (() => void)
296-
const callback = (event: RollupWatcherEvent): void => {
296+
const callback = (event: Rollup.RollupWatcherEvent): void => {
297297
if (event.code === 'END') {
298298
resolveFn?.()
299299
}
@@ -306,7 +306,7 @@ export async function notifyRebuildComplete(
306306
}
307307

308308
function createInMemoryLogger(logs: string[]): Logger {
309-
const loggedErrors = new WeakSet<Error | RollupError>()
309+
const loggedErrors = new WeakSet<Error | Rollup.RollupError>()
310310
const warnedMessages = new Set<string>()
311311

312312
const logger: Logger = {

playground/vue-lib/__tests__/vue-lib.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
2+
import type { Rollup } from 'vite'
23
import { build } from 'vite'
34
import { describe, expect, test } from 'vitest'
4-
import type { OutputChunk, RollupOutput } from 'rollup'
55

66
describe('vue component library', () => {
77
test('should output tree shakeable css module code', async () => {
@@ -14,10 +14,10 @@ describe('vue component library', () => {
1414
const { output } = (await build({
1515
logLevel: 'silent',
1616
configFile: path.resolve(__dirname, '../vite.config.consumer.ts'),
17-
})) as RollupOutput
17+
})) as Rollup.RollupOutput
1818
const { code } = output.find(
1919
(e) => e.type === 'chunk' && e.isEntry,
20-
) as OutputChunk
20+
) as Rollup.OutputChunk
2121
// Unused css module should be treeshaked
2222
expect(code).toContain('styleA') // styleA is used by CompA
2323
expect(code).not.toContain('styleB') // styleB is not used

0 commit comments

Comments
 (0)