fix(bridge): automatically transpile all modules (and sanitize transpiles) (#677)

This commit is contained in:
Daniel Roe 2021-10-06 19:59:35 +02:00 committed by GitHub
parent 4b3814a14f
commit 0aa6fbf5ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 4 deletions

View File

@ -9,9 +9,6 @@ export function setupAppBridge (_options: any) {
nuxt.options.alias['#app'] = resolve(distDir, 'runtime/index.mjs') nuxt.options.alias['#app'] = resolve(distDir, 'runtime/index.mjs')
nuxt.options.alias['#build'] = nuxt.options.buildDir nuxt.options.alias['#build'] = nuxt.options.buildDir
// Transpile runtime/
nuxt.options.build.transpile.push(resolve(distDir, 'runtime'))
// Alias vue to a vue3-compat version of vue2 // Alias vue to a vue3-compat version of vue2
nuxt.options.alias['#vue'] = nuxt.options.alias.vue || resolveModule('vue/dist/vue.runtime.esm.js', { paths: nuxt.options.modulesDir }) nuxt.options.alias['#vue'] = nuxt.options.alias.vue || resolveModule('vue/dist/vue.runtime.esm.js', { paths: nuxt.options.modulesDir })
nuxt.options.alias['@vue/shared'] = 'vue' nuxt.options.alias['@vue/shared'] = 'vue'

View File

@ -8,6 +8,7 @@ import { setupBetterResolve } from './resolve'
import { setupGlobalImports } from './global-imports' import { setupGlobalImports } from './global-imports'
import { setupTypescript } from './typescript' import { setupTypescript } from './typescript'
import { setupMeta } from './meta' import { setupMeta } from './meta'
import { setupTranspile } from './transpile'
export default defineNuxtModule({ export default defineNuxtModule({
name: 'nuxt-bridge', name: 'nuxt-bridge',
@ -17,6 +18,7 @@ export default defineNuxtModule({
vite: false, vite: false,
app: {}, app: {},
capi: {}, capi: {},
transpile: true,
globalImports: true, globalImports: true,
constraints: true, constraints: true,
meta: null, meta: null,
@ -55,6 +57,9 @@ export default defineNuxtModule({
if (opts.resolve) { if (opts.resolve) {
setupBetterResolve() setupBetterResolve()
} }
if (opts.transpile) {
setupTranspile()
}
if (opts.constraints) { if (opts.constraints) {
nuxt.hook('modules:done', (moduleContainer: any) => { nuxt.hook('modules:done', (moduleContainer: any) => {
for (const [name, m] of Object.entries(moduleContainer.requiredModules || {})) { for (const [name, m] of Object.entries(moduleContainer.requiredModules || {})) {

View File

@ -0,0 +1,10 @@
import { useNuxt } from '@nuxt/kit'
import { addModuleTranspiles } from '../../nuxt3/src/core/modules'
export const setupTranspile = () => {
const nuxt = useNuxt()
nuxt.hook('modules:done', () => {
addModuleTranspiles()
})
}

View File

@ -6,6 +6,7 @@ export interface BridgeConfig {
app: boolean | {} app: boolean | {}
capi: boolean | {} capi: boolean | {}
globalImports: boolean globalImports: boolean
transpile: boolean
constraints: boolean constraints: boolean
postcss8: boolean postcss8: boolean
swc: boolean swc: boolean

View File

@ -0,0 +1,29 @@
import { useNuxt } from '@nuxt/kit'
export const addModuleTranspiles = () => {
const nuxt = useNuxt()
const modules = [
...nuxt.options.buildModules,
...nuxt.options.modules,
...nuxt.options._modules
]
.map(m => typeof m === 'string' ? m : Array.isArray(m) ? m[0] : m.src)
.filter(m => typeof m === 'string')
.map(m => m.split('node_modules/').pop())
// Try to sanitize modules to better match imports
nuxt.options.build.transpile =
nuxt.options.build.transpile.map(m => typeof m === 'string' ? m.split('node_modules/').pop() : m)
function isTranspilePresent (mod: string) {
return nuxt.options.build.transpile.some(t => !(t instanceof Function) && (t instanceof RegExp ? t.test(mod) : new RegExp(t).test(mod)))
}
// Automatically add used modules to the transpile
for (const module of modules) {
if (!isTranspilePresent(module)) {
nuxt.options.build.transpile.push(module)
}
}
}

View File

@ -8,6 +8,7 @@ import globalImportsModule from '../global-imports/module'
import { distDir, pkgDir } from '../dirs' import { distDir, pkgDir } from '../dirs'
import { version } from '../../package.json' import { version } from '../../package.json'
import { initNitro } from './nitro' import { initNitro } from './nitro'
import { addModuleTranspiles } from './modules'
export function createNuxt (options: NuxtOptions): Nuxt { export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = createHooks<NuxtHooks>() const hooks = createHooks<NuxtHooks>()
@ -52,6 +53,8 @@ async function initNuxt (nuxt: Nuxt) {
await nuxt.callHook('modules:done', { nuxt } as ModuleContainer) await nuxt.callHook('modules:done', { nuxt } as ModuleContainer)
await addModuleTranspiles()
await nuxt.callHook('ready', nuxt) await nuxt.callHook('ready', nuxt)
} }

View File

@ -14,7 +14,7 @@ export default defineNuxtModule({
const runtimeDir = nuxt.options.alias['#meta'] || resolve(distDir, 'meta/runtime') const runtimeDir = nuxt.options.alias['#meta'] || resolve(distDir, 'meta/runtime')
// Transpile @nuxt/meta and @vueuse/head // Transpile @nuxt/meta and @vueuse/head
nuxt.options.build.transpile.push(runtimeDir, '@vueuse/head') nuxt.options.build.transpile.push('nuxt3', '@vueuse/head')
// Add #meta alias // Add #meta alias
nuxt.options.alias['#meta'] = runtimeDir nuxt.options.alias['#meta'] = runtimeDir