Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat(vite): support build.transpile as function (#7767)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
jmorel88 and danielroe authored Jan 19, 2023
1 parent 02df51d commit baf9d95
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/schema/src/config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default defineUntypedSchema({
* ```js
transpile: [({ isLegacy }) => isLegacy && 'ky']
* ```
* @type {Array<string | RegExp | Function>}
* @type {Array<string | RegExp | ((ctx: { isClient?: boolean; isServer?: boolean; isDev: boolean }) => string | RegExp | false)>}
*/
transpile: {
$resolve: val => [].concat(val).filter(Boolean)
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { cacheDirPlugin } from './plugins/cache-dir'
import { initViteNodeServer } from './vite-node'
import { ssrStylesPlugin } from './plugins/ssr-styles'
import { writeManifest } from './manifest'
import { transpile } from './utils/transpile'

export async function buildServer (ctx: ViteBuildContext) {
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function buildServer (ctx: ViteBuildContext) {
? ['#internal/nitro', '#internal/nitro/utils', 'vue', 'vue-router']
: ['#internal/nitro', '#internal/nitro/utils'],
noExternal: [
...ctx.nuxt.options.build.transpile,
...transpile({ isServer: true, isDev: ctx.nuxt.options.dev }),
// TODO: Use externality for production (rollup) build
/\/esm\/.*\.js$/,
/\.(es|esm|esm-browser|esm-bundler).js$/,
Expand Down
28 changes: 28 additions & 0 deletions packages/vite/src/utils/transpile.ts
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion packages/vite/src/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { distDir } from './dirs'
import type { ViteBuildContext } from './vite'
import { isCSS } from './utils'
import { createIsExternal } from './utils/external'
import { transpile } from './utils/transpile'

// TODO: Remove this in favor of registerViteNodeMiddleware
// after Nitropack or h3 fixed for adding middlewares after setup
Expand Down Expand Up @@ -99,7 +100,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
inline: [
/\/(nuxt|nuxt3)\//,
/^#/,
...ctx.nuxt.options.build.transpile as string[]
...transpile({ isServer: true, isDev: ctx.nuxt.options.dev })
]
},
transformMode: {
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack/src/presets/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ export function baseTranspile (ctx: WebpackConfigContext) {

for (let pattern of options.build.transpile) {
if (typeof pattern === 'function') {
pattern = pattern(ctx)
const result = pattern(ctx)
if (result) { pattern = result }
}
if (typeof pattern === 'string') {
transpile.push(new RegExp(escapeRegExp(normalize(pattern))))
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export default defineNuxtConfig({
},
buildDir: process.env.NITRO_BUILD_DIR,
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',
css: ['~/assets/global.css'],
extends: [
Expand Down

0 comments on commit baf9d95

Please sign in to comment.