Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: plugin hooks ssr param to object #5253

Merged
merged 7 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
12 changes: 9 additions & 3 deletions docs/guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ If you have configured aliases that redirects one package to another, you may wa

## SSR-specific Plugin Logic

Some frameworks such as Vue or Svelte compiles components into different formats based on client vs. SSR. To support conditional transforms, Vite passes an additional `ssr` argument to the following plugin hooks:
Some frameworks such as Vue or Svelte compiles components into different formats based on client vs. SSR. To support conditional transforms, Vite passes an additional `ssr` property in the `options` object of the following plugin hooks:

- `resolveId`
- `load`
Expand All @@ -239,15 +239,21 @@ Some frameworks such as Vue or Svelte compiles components into different formats
export function mySSRPlugin() {
return {
name: 'my-ssr',
transform(code, id, ssr) {
if (ssr) {
transform(code, id, options) {
if (options?.ssr) {
// perform ssr-specific transform...
}
}
}
}
```

The options object in `load` and `transform` is optional, rollup is not currently using this object but may extend these hooks with additional metadata in the future.

::: note
Before Vite 2.7, this was informed to plugin hooks with a positional `ssr` param instead of using the `options` object. All major frameworks and plugins are updated but you may find outdated posts using the previous API.
:::

## SSR Target

The default target for the SSR build is a node environment, but you can also run the server in a Web Worker. Packages entry resolution is different for each platform. You can configure the target to be Web Worker using the `ssr.target` set to `'webworker'`.
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
)
)
},
async transform(code, id, ssr) {
async transform(code, id, options) {
const ssr = typeof options === 'boolean' ? options : options?.ssr === true
if (/\.[tj]sx?$/.test(id)) {
const plugins = [...userPlugins]

Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-vue-jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ function vueJsxPlugin(options = {}) {
}
},

transform(code, id, ssr) {
transform(code, id, opt) {
const ssr = typeof opt === 'boolean' ? opt : (opt && opt.ssr) === true
const {
include,
exclude,
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
sourceMap: true
}

// Temporal handling for 2.7 breaking change
const isSSR = (opt: { ssr?: boolean } | boolean | undefined) =>
opt === undefined ? !!options.ssr :
(typeof opt === 'boolean' ? opt : opt?.ssr === true)

return {
name: 'vite:vue',

Expand Down Expand Up @@ -150,7 +155,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
}
},

load(id, ssr = !!options.ssr) {
load(id, opt) {
const ssr = isSSR(opt)
if (id === EXPORT_HELPER_ID) {
return helperCode
}
Expand Down Expand Up @@ -182,7 +188,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
}
},

transform(code, id, ssr = !!options.ssr) {
transform(code, id, opt) {
const ssr = isSSR(opt)
const { filename, query } = parseVueRequest(id)
if (query.raw) {
return
Expand Down
7 changes: 3 additions & 4 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,17 @@ export interface Plugin extends RollupPlugin {
this: PluginContext,
source: string,
importer: string | undefined,
options: { custom?: CustomPluginOptions },
ssr?: boolean
options: { custom?: CustomPluginOptions, ssr?: boolean }
): Promise<ResolveIdResult> | ResolveIdResult
load?(
this: PluginContext,
id: string,
ssr?: boolean
options?: { ssr?: boolean }
): Promise<LoadResult> | LoadResult
transform?(
this: TransformPluginContext,
code: string,
id: string,
ssr?: boolean
options?: { ssr?: boolean }
): Promise<TransformResult> | TransformResult
}
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { multilineCommentsRE, singlelineCommentsRE } from '../utils'
export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
return {
name: 'vite:asset-import-meta-url',
async transform(code, id, ssr) {
async transform(code, id, options) {
if (code.includes('new URL') && code.includes(`import.meta.url`)) {
const importMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\)/g
Expand All @@ -30,7 +30,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
while ((match = importMetaUrlRE.exec(noCommentsCode))) {
const { 0: exp, 1: rawUrl, index } = match

if (ssr) {
if (options?.ssr) {
this.error(
`\`new URL(url, import.meta.url)\` is not supported in SSR.`,
index
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
hasEmitted = false
},

async transform(css, id, ssr) {
async transform(css, id, options) {
if (!isCSSRequest(id) || commonjsProxyRE.test(id)) {
return
}
Expand All @@ -286,7 +286,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
return css
} else {
// server only
if (ssr) {
if (options?.ssr) {
return modulesCode || `export default ${JSON.stringify(css)}`
}
if (inlined) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export function definePlugin(config: ResolvedConfig): Plugin {

return {
name: 'vite:define',
transform(code, id, ssr) {
transform(code, id, options) {
const ssr = options?.ssr === true
if (!ssr && !isBuild) {
// for dev we inject actual global defines in the vite client to
// avoid the transform cost.
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
server = _server
},

async transform(source, importer, ssr) {
async transform(source, importer, options) {
const ssr = options?.ssr === true
const prettyImporter = prettifyUrl(importer, root)

if (canSkip(importer)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/preAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function preAliasPlugin(): Plugin {
configureServer(_server) {
server = _server
},
resolveId(id, importer, _, ssr) {
if (!ssr && bareImportRE.test(id)) {
resolveId(id, importer, options) {
if (!options?.ssr && bareImportRE.test(id)) {
return tryOptimizedResolve(id, server, importer)
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
server = _server
},

resolveId(id, importer, resolveOpts, ssr) {
resolveId(id, importer, resolveOpts) {
const ssr = resolveOpts?.ssr === true
if (id.startsWith(browserExternalId)) {
return id
}
Expand Down
7 changes: 3 additions & 4 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ export async function createPluginContainer(
ctx as any,
rawId,
importer,
{},
ssr
{ ssr }
)
if (!result) continue

Expand Down Expand Up @@ -487,7 +486,7 @@ export async function createPluginContainer(
for (const plugin of plugins) {
if (!plugin.load) continue
ctx._activePlugin = plugin
const result = await plugin.load.call(ctx as any, id, ssr)
const result = await plugin.load.call(ctx as any, id, { ssr })
if (result != null) {
return result
}
Expand All @@ -506,7 +505,7 @@ export async function createPluginContainer(
const start = isDebug ? performance.now() : 0
let result: TransformResult | string | undefined
try {
result = await plugin.transform.call(ctx as any, code, id, ssr)
result = await plugin.transform.call(ctx as any, code, id, { ssr })
} catch (e) {
ctx.error(e)
}
Expand Down