Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8596dfd
wip: bump rollup
sapphi-red Sep 30, 2023
b907e6e
wip: remove `acorn`/`acornInjectPlugins` option, remove additional op…
sapphi-red Sep 30, 2023
e6474ba
wip: replace "assertions" with "attributes"
sapphi-red Sep 30, 2023
00e0ece
wip: replace RollupWarning with RollupLog
sapphi-red Sep 30, 2023
0c488c7
wip: remove omit `moduleIds`
sapphi-red Sep 30, 2023
d9085f7
wip: call `this.addWatchFile` in vite:load-fallback plugin
sapphi-red Sep 30, 2023
4d49292
wip: remove utf8 BOM for now
sapphi-red Sep 30, 2023
f161d22
wip: use parse function from rollup
sapphi-red Sep 30, 2023
ddde37f
wip: bump rollup for plugin-legacy
sapphi-red Sep 30, 2023
49dafa0
wip: default of skipSelf is now true
sapphi-red Sep 30, 2023
176f6d5
wip: ignore rollup version mismatch type error
sapphi-red Sep 30, 2023
9f36ca3
wip: bump rollup
sapphi-red Oct 3, 2023
6b00745
wip: revert "wip: remove utf8 BOM for now"
sapphi-red Oct 3, 2023
632d3b0
wip: new hash algorithm
sapphi-red Oct 3, 2023
a3c750f
wip: new hash algorithm
sapphi-red Oct 3, 2023
ae0b4c4
wip: new hash algorithm
sapphi-red Oct 3, 2023
fa7a370
chore: merge main
sapphi-red Oct 5, 2023
e5bf794
chore: update rollup plugins
sapphi-red Oct 5, 2023
a274f82
wip: update rollup
sapphi-red Oct 5, 2023
19c2d76
wip: use parseAst function from rollup
sapphi-red Oct 5, 2023
60bb4fd
wip: remove ensureWatchedFile
sapphi-red Oct 5, 2023
1edfeff
wip: bump rollup to 4.0.0
sapphi-red Oct 5, 2023
dafc6d7
wip: use ^4.0.0 instead of 4.0.0
sapphi-red Oct 5, 2023
e2c9e8a
wip: add `addWatchFile` to assetPlugin
sapphi-red Oct 5, 2023
90f1344
wip: remove ensureWatchPlugin
sapphi-red Oct 5, 2023
924c4b0
wip: add migration guide
sapphi-red Oct 5, 2023
e56494e
wip: remove RollupParseFunc type
sapphi-red Oct 6, 2023
9ab9f61
wip: remove the tweak to workaround rollup/acorn inconsistency
sapphi-red Oct 6, 2023
522f77c
wip: update docs
sapphi-red Oct 6, 2023
c9eb9f3
wip: restore ensureWatchedFile in indexHtml.ts
sapphi-red Oct 6, 2023
342716a
chore: merge main
sapphi-red Oct 6, 2023
aae78cf
wip: new hash algorithm
sapphi-red Oct 6, 2023
c1fad36
wip: new hash algorithm
sapphi-red Oct 6, 2023
31ed8b1
chore: merge main
sapphi-red Oct 9, 2023
0728839
chore: merge main
sapphi-red Oct 11, 2023
3d081e3
chore: merge main
sapphi-red Oct 12, 2023
8915ca5
chore: bump vitepress
sapphi-red Oct 12, 2023
22d7bd9
chore: merge main
sapphi-red Oct 16, 2023
d5bd340
chore: bump rollup-plugin-license
sapphi-red Oct 16, 2023
72a4624
chore: bump rollup and magic-string
sapphi-red Oct 16, 2023
6a5d4dd
chore: merge main
sapphi-red Oct 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip: use parse function from rollup
  • Loading branch information
sapphi-red committed Sep 30, 2023
commit f161d22fbb1b066d212f756327c45303832ce31c
29 changes: 0 additions & 29 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,35 +566,6 @@ Repository: https://github.com/acornjs/acorn.git

---------------------------------------

## acorn-import-attributes
License: MIT
By: Sven Sauleau
Repository: https://github.com/xtuc/acorn-import-attributes

> MIT License
>
> Copyright (c) 2023 Sven Sauleau
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.

---------------------------------------

## acorn-walk
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Expand Down
1 change: 0 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
"@types/escape-html": "^1.0.2",
"@types/pnpapi": "^0.0.3",
"acorn": "^8.10.0",
"acorn-import-attributes": "^1.9.2",
"acorn-walk": "^8.2.0",
"cac": "^6.7.14",
"chokidar": "^3.5.3",
Expand Down
44 changes: 32 additions & 12 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ SOFTWARE.
import fs from 'node:fs'
import { join } from 'node:path'
import { performance } from 'node:perf_hooks'
import { VERSION as rollupVersion } from 'rollup'
import { rollup, VERSION as rollupVersion } from 'rollup'
import type {
AstNode,
AsyncPluginHooks,
CustomPluginOptions,
EmittedFile,
Expand All @@ -56,11 +57,8 @@ import type {
SourceMap,
TransformResult,
} from 'rollup'
import * as acorn from 'acorn'
import type { RawSourceMap } from '@ampproject/remapping'
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
// @ts-expect-error untyped
import { importAttributes } from 'acorn-import-attributes'
import MagicString from 'magic-string'
import type { FSWatcher } from 'chokidar'
import colors from 'picocolors'
Expand Down Expand Up @@ -149,7 +147,32 @@ type PluginContext = Omit<
'cache'
>

export const parser = acorn.Parser.extend(importAttributes)
export type RollupParseFunc = (
input: string,
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,
Expand Down Expand Up @@ -274,6 +297,8 @@ 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 @@ -291,13 +316,8 @@ export async function createPluginContainer(
this._activePlugin = initialPlugin || null
}

parse(code: string, opts: any = {}) {
return parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true,
...opts,
})
parse(code: string, opts: any) {
return rollupParseFunc(code, opts)
}

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

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

let rollupParseFunc: RollupParseFunc | undefined

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

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

let ast: any
try {
ast = parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true,
allowHashBang: true,
})
ast = rollupParseFunc(code)
} catch (err) {
if (!err.loc || !err.loc.line) throw err
const line = err.loc.line
Expand Down Expand Up @@ -377,7 +379,7 @@ function walk(
}

;(eswalk as any)(root, {
enter(node: Node, parent: Node | null) {
enter(node: Node, parent: Node | null, prop: string) {
if (node.type === 'ImportDeclaration') {
return this.skip()
}
Expand Down Expand Up @@ -405,7 +407,7 @@ function walk(
if (node.type === 'Identifier') {
if (
!isInScope(node.name, parentStack) &&
isRefIdentifier(node, parent!, parentStack)
isRefIdentifier(node, parent!, parentStack, prop)
) {
// record the identifier, for DFS -> BFS
identifiers.push([node, parentStack.slice(0)])
Expand All @@ -427,7 +429,7 @@ function walk(
return
}
;(eswalk as any)(p.type === 'AssignmentPattern' ? p.left : p, {
enter(child: Node, parent: Node) {
enter(child: Node, parent: Node, prop: string) {
// skip params default value of destructure
if (
parent?.type === 'AssignmentPattern' &&
Expand All @@ -437,7 +439,7 @@ function walk(
}
if (child.type !== 'Identifier') return
// do not record as scope variable if is a destructuring keyword
if (isStaticPropertyKey(child, parent)) return
if (isStaticPropertyKey(child, parent, prop)) return
// do not record if this is a default value
// assignment of a destructuring variable
if (
Expand Down Expand Up @@ -489,7 +491,12 @@ function walk(
})
}

function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {
function isRefIdentifier(
id: Identifier,
parent: _Node,
parentStack: _Node[],
prop: string,
) {
// declaration id
if (
parent.type === 'CatchClause' ||
Expand Down Expand Up @@ -517,7 +524,7 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {
}

// property key
if (isStaticPropertyKey(id, parent)) {
if (isStaticPropertyKey(id, parent, prop)) {
return false
}

Expand Down Expand Up @@ -558,8 +565,8 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {
const isStaticProperty = (node: _Node): node is Property =>
node && node.type === 'Property' && !node.computed

const isStaticPropertyKey = (node: _Node, parent: _Node) =>
isStaticProperty(parent) && parent.key === node
const isStaticPropertyKey = (node: _Node, parent: _Node, prop: string) =>
isStaticProperty(parent) && prop === 'key' && parent.key === node

const functionNodeTypeRE = /Function(?:Expression|Declaration)$|Method$/
function isFunction(node: _Node): node is FunctionNode {
Expand Down
11 changes: 0 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.