feat(vite): support build.transpile as function (#7767)

Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
Jose Morel 2023-01-19 14:56:34 +04:00 committed by GitHub
parent 02df51dd57
commit baf9d950db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 4 deletions

View File

@ -53,7 +53,7 @@ export default defineUntypedSchema({
* ```js * ```js
transpile: [({ isLegacy }) => isLegacy && 'ky'] transpile: [({ isLegacy }) => isLegacy && 'ky']
* ``` * ```
* @type {Array<string | RegExp | Function>} * @type {Array<string | RegExp | ((ctx: { isClient?: boolean; isServer?: boolean; isDev: boolean }) => string | RegExp | false)>}
*/ */
transpile: { transpile: {
$resolve: val => [].concat(val).filter(Boolean) $resolve: val => [].concat(val).filter(Boolean)

View File

@ -9,6 +9,7 @@ import { cacheDirPlugin } from './plugins/cache-dir'
import { initViteNodeServer } from './vite-node' import { initViteNodeServer } from './vite-node'
import { ssrStylesPlugin } from './plugins/ssr-styles' import { ssrStylesPlugin } from './plugins/ssr-styles'
import { writeManifest } from './manifest' import { writeManifest } from './manifest'
import { transpile } from './utils/transpile'
export async function buildServer (ctx: ViteBuildContext) { export async function buildServer (ctx: ViteBuildContext) {
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir }) const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
@ -65,7 +66,7 @@ export async function buildServer (ctx: ViteBuildContext) {
? ['#internal/nitro', '#internal/nitro/utils', 'vue', 'vue-router'] ? ['#internal/nitro', '#internal/nitro/utils', 'vue', 'vue-router']
: ['#internal/nitro', '#internal/nitro/utils'], : ['#internal/nitro', '#internal/nitro/utils'],
noExternal: [ noExternal: [
...ctx.nuxt.options.build.transpile, ...transpile({ isServer: true, isDev: ctx.nuxt.options.dev }),
// TODO: Use externality for production (rollup) build // TODO: Use externality for production (rollup) build
/\/esm\/.*\.js$/, /\/esm\/.*\.js$/,
/\.(es|esm|esm-browser|esm-bundler).js$/, /\.(es|esm|esm-browser|esm-bundler).js$/,

View File

@ -0,0 +1,28 @@
import { useNuxt } from '@nuxt/kit'
import escapeRegExp from 'escape-string-regexp'
import { normalize } from 'pathe'
interface Envs {
isDev: boolean
isClient?: boolean
isServer?: boolean
}
export function transpile (envs: Envs): Array<string | RegExp> {
const nuxt = useNuxt()
const transpile = []
for (let pattern of nuxt.options.build.transpile) {
if (typeof pattern === 'function') {
const result = pattern(envs)
if (result) { pattern = result }
}
if (typeof pattern === 'string') {
transpile.push(new RegExp(escapeRegExp(normalize(pattern))))
} else if (pattern instanceof RegExp) {
transpile.push(pattern)
}
}
return transpile
}

View File

@ -11,6 +11,7 @@ import { distDir } from './dirs'
import type { ViteBuildContext } from './vite' import type { ViteBuildContext } from './vite'
import { isCSS } from './utils' import { isCSS } from './utils'
import { createIsExternal } from './utils/external' import { createIsExternal } from './utils/external'
import { transpile } from './utils/transpile'
// TODO: Remove this in favor of registerViteNodeMiddleware // TODO: Remove this in favor of registerViteNodeMiddleware
// after Nitropack or h3 fixed for adding middlewares after setup // after Nitropack or h3 fixed for adding middlewares after setup
@ -99,7 +100,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
inline: [ inline: [
/\/(nuxt|nuxt3)\//, /\/(nuxt|nuxt3)\//,
/^#/, /^#/,
...ctx.nuxt.options.build.transpile as string[] ...transpile({ isServer: true, isDev: ctx.nuxt.options.dev })
] ]
}, },
transformMode: { transformMode: {

View File

@ -161,7 +161,8 @@ export function baseTranspile (ctx: WebpackConfigContext) {
for (let pattern of options.build.transpile) { for (let pattern of options.build.transpile) {
if (typeof pattern === 'function') { if (typeof pattern === 'function') {
pattern = pattern(ctx) const result = pattern(ctx)
if (result) { pattern = result }
} }
if (typeof pattern === 'string') { if (typeof pattern === 'string') {
transpile.push(new RegExp(escapeRegExp(normalize(pattern)))) transpile.push(new RegExp(escapeRegExp(normalize(pattern))))

View File

@ -22,6 +22,14 @@ export default defineNuxtConfig({
}, },
buildDir: process.env.NITRO_BUILD_DIR, buildDir: process.env.NITRO_BUILD_DIR,
builder: process.env.TEST_WITH_WEBPACK ? 'webpack' : 'vite', builder: process.env.TEST_WITH_WEBPACK ? 'webpack' : 'vite',
build: {
transpile: [
(ctx) => {
if (typeof ctx.isDev !== 'boolean') { throw new TypeError('context not passed') }
return false
}
]
},
theme: './extends/bar', theme: './extends/bar',
css: ['~/assets/global.css'], css: ['~/assets/global.css'],
extends: [ extends: [