Skip to content

Commit

Permalink
wip: use parseAst function from rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 5, 2023
1 parent a274f82 commit 19c2d76
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ function createNodeConfig(isProduction: boolean) {
external: [
'fsevents',
'lightningcss',
'rollup/parseAst',
...Object.keys(pkg.dependencies),
...(isProduction ? [] : Object.keys(pkg.devDependencies)),
],
Expand Down
29 changes: 3 additions & 26 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ SOFTWARE.
import fs from 'node:fs'
import { join } from 'node:path'
import { performance } from 'node:perf_hooks'
import { rollup, VERSION as rollupVersion } from 'rollup'
import { VERSION as rollupVersion } from 'rollup'
import { parseAst as rollupParseAst } from 'rollup/parseAst'
import type {
AstNode,
AsyncPluginHooks,
Expand Down Expand Up @@ -152,28 +153,6 @@ export type RollupParseFunc = (
options?: { allowReturnOutsideFunction?: boolean },
) => AstNode

export const getRollupParseFunc = async (): Promise<RollupParseFunc> => {
let rollupParse!: RollupParseFunc
await rollup({
input: 'dummy',
plugins: [
{
name: 'get-parse',
resolveId(id) {
return id
},
load(id) {
return ''
},
transform(code, id) {
rollupParse = this.parse
},
},
],
})
return rollupParse
}

export async function createPluginContainer(
config: ResolvedConfig,
moduleGraph?: ModuleGraph,
Expand Down Expand Up @@ -297,8 +276,6 @@ export async function createPluginContainer(
}
}

const rollupParseFunc = await getRollupParseFunc()

// we should create a new context for each async hook pipeline so that the
// active plugin in that pipeline can be tracked in a concurrency-safe manner.
// using a class to make creating new contexts more efficient
Expand All @@ -317,7 +294,7 @@ export async function createPluginContainer(
}

parse(code: string, opts: any) {
return rollupParseFunc(code, opts)
return rollupParseAst(code, opts)
}

async resolve(
Expand Down
11 changes: 2 additions & 9 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import type {
import { extract_names as extractNames } from 'periscopic'
import { walk as eswalk } from 'estree-walker'
import type { RawSourceMap } from '@ampproject/remapping'
import { parseAst as rollupParseAst } from 'rollup/parseAst'
import type { TransformResult } from '../server/transformRequest'
import type { RollupParseFunc } from '../server/pluginContainer'
import { getRollupParseFunc } from '../server/pluginContainer'
import { combineSourcemaps } from '../utils'
import { isJSONRequest } from '../plugins/json'

Expand Down Expand Up @@ -62,8 +61,6 @@ async function ssrTransformJSON(
}
}

let rollupParseFunc: RollupParseFunc | undefined

async function ssrTransformScript(
code: string,
inMap: SourceMap | { mappings: '' } | null,
Expand All @@ -72,13 +69,9 @@ async function ssrTransformScript(
): Promise<TransformResult | null> {
const s = new MagicString(code)

if (!rollupParseFunc) {
rollupParseFunc = await getRollupParseFunc()
}

let ast: any
try {
ast = rollupParseFunc(code)
ast = rollupParseAst(code)
} catch (err) {
if (!err.loc || !err.loc.line) throw err
const line = err.loc.line
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "node",
"moduleResolution": "Bundler",
"strict": true,
"declaration": true,
"noImplicitOverride": true,
Expand Down

0 comments on commit 19c2d76

Please sign in to comment.