feat(nitro): specify packages to copy to .output/server/node_modules (#2382)

This commit is contained in:
Daniel Roe 2021-12-20 11:03:22 +00:00 committed by GitHub
parent a1a2d2f92b
commit c8e281e7b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -22,7 +22,7 @@ import { resolvePath } from '../utils'
import { pkgDir } from '../dirs'
import { dynamicRequire } from './plugins/dynamic-require'
import { externals } from './plugins/externals'
import { externals, NodeExternalsOptions } from './plugins/externals'
import { timing } from './plugins/timing'
// import { autoMock } from './plugins/automock'
import { staticAssets, dirnames } from './plugins/static'
@ -249,7 +249,7 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
// Externals Plugin
if (nitroContext.externals) {
rollupConfig.plugins.push(externals(defu(nitroContext.externals as any, {
rollupConfig.plugins.push(externals(defu(nitroContext.externals as NodeExternalsOptions, {
outDir: nitroContext.output.serverDir,
moduleDirectories,
external: [
@ -266,7 +266,7 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
nitroContext._nuxt.srcDir,
nitroContext._nuxt.rootDir,
nitroContext._nuxt.serverDir,
...nitroContext.middleware.map(m => m.handle),
...nitroContext.middleware.map(m => m.handle).filter(i => typeof i === 'string') as string[],
...(nitroContext._nuxt.dev ? [] : ['vue', '@vue/', '@nuxt/'])
],
traceOptions: {

View File

@ -10,6 +10,8 @@ export interface NodeExternalsOptions {
trace?: boolean
traceOptions?: NodeFileTraceOptions
moduleDirectories?: string[]
/** additional packages to include in `.output/server/node_modules` */
traceInclude?: string[]
}
export function externals (opts: NodeExternalsOptions): Plugin {
@ -67,6 +69,12 @@ export function externals (opts: NodeExternalsOptions): Plugin {
},
async buildEnd () {
if (opts.trace !== false) {
for (const pkgName of opts.traceInclude || []) {
const path = await this.resolve(pkgName)
if (path?.id) {
trackedExternals.add(path.id)
}
}
const tracedFiles = await nodeFileTrace(Array.from(trackedExternals), opts.traceOptions)
.then(r => Array.from(r.fileList).map(f => resolve(opts.traceOptions.base, f)))
.then(r => r.filter(file => file.includes('node_modules')))