mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-14 18:13:54 +00:00
f350a70775
Co-authored-by: Pooya Parsa <pooya@pi0.io>
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { useNuxt } from '@nuxt/kit'
|
|
|
|
export interface AddModuleTranspilesOptions {
|
|
additionalModules?: string[]
|
|
}
|
|
|
|
export const addModuleTranspiles = (opts: AddModuleTranspilesOptions = {}) => {
|
|
const nuxt = useNuxt()
|
|
|
|
const modules = [
|
|
...opts.additionalModules || [],
|
|
...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)
|
|
.filter(<T>(x: T | undefined): x is T => !!x)
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|