2021-10-06 17:59:35 +00:00
|
|
|
import { useNuxt } from '@nuxt/kit'
|
|
|
|
|
2022-04-05 10:41:45 +00:00
|
|
|
export interface AddModuleTranspilesOptions {
|
|
|
|
additionalModules?: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export const addModuleTranspiles = (opts: AddModuleTranspilesOptions = {}) => {
|
2021-10-06 17:59:35 +00:00
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
const modules = [
|
2022-04-05 10:41:45 +00:00
|
|
|
...opts.additionalModules || [],
|
2021-10-06 17:59:35 +00:00
|
|
|
...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)
|
2022-08-12 17:47:58 +00:00
|
|
|
.filter(<T>(x: T | undefined): x is T => !!x)
|
2021-10-06 17:59:35 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|