Skip to content

Commit

Permalink
feat: support for regex for ssr.noExternal (#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinando-ferreira authored Jun 25, 2021
1 parent dd5931d commit 330c94c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ SSR options may be adjusted in minor releases.

### ssr.noExternal

- **Type:** `string[]`
- **Type:** `string | RegExp | (string | RegExp)[]`

Prevent listed dependencies from being externalized for SSR.

Expand Down
2 changes: 2 additions & 0 deletions packages/playground/ssr-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"scripts": {
"dev": "node server",
"build": "yarn build:client && yarn build:server",
"build:noExternal": "yarn build:client && yarn build:server:noExternal",
"build:client": "vite build --ssrManifest --outDir dist/client",
"build:server": "vite build --ssr src/entry-server.js --outDir dist/server",
"build:server:noExternal": "vite build --config vite.config.noexternal.js --ssr src/entry-server.js --outDir dist/server",
"generate": "vite build --ssrManifest --outDir dist/static && yarn build:server && node prerender",
"serve": "cross-env NODE_ENV=production node server",
"debug": "node --inspect-brk server"
Expand Down
22 changes: 22 additions & 0 deletions packages/playground/ssr-vue/vite.config.noexternal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const config = require('./vite.config.js')
/**
* @type {import('vite').UserConfig}
*/
module.exports = Object.assign(config, {
ssr: {
noExternal: /./
},
resolve: {
// necessary because vue.ssrUtils is only exported on cjs modules
alias: [
{
find: '@vue/runtime-dom',
replacement: '@vue/runtime-dom/dist/runtime-dom.cjs.js'
},
{
find: '@vue/runtime-core',
replacement: '@vue/runtime-core/dist/runtime-core.cjs.js'
}
]
}
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export type SSRTarget = 'node' | 'webworker'

export interface SSROptions {
external?: string[]
noExternal?: string[]
noExternal?: string | RegExp | (string | RegExp)[]
/**
* Define the target for the ssr build. The browser field in package.json
* is ignored for node but used if webworker is the target
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import { tryNodeResolve, InternalResolveOptions } from '../plugins/resolve'
import { isDefined, lookupFile, resolveFrom, unique } from '../utils'
import { ResolvedConfig } from '..'

import { createFilter } from '@rollup/pluginutils'
/**
* Heuristics for determining whether a dependency should be externalized for
* server-side rendering.
Expand Down Expand Up @@ -108,7 +108,10 @@ export function resolveSSRExternal(
}
let externals = [...ssrExternals]
if (config.ssr?.noExternal) {
externals = externals.filter((id) => !config.ssr!.noExternal!.includes(id))
const filter = createFilter(undefined, config.ssr.noExternal, {
resolve: false
})
externals = externals.filter((id) => filter(id))
}
return externals.filter((id) => id !== 'vite')
}
Expand Down

0 comments on commit 330c94c

Please sign in to comment.