fix: issues with externals outside of rootDir

This commit is contained in:
Pooya Parsa 2021-04-19 22:41:02 +02:00
parent bef9f82a8d
commit 4e1865358c
3 changed files with 30 additions and 22 deletions

View File

@ -47,6 +47,7 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
'lodash.template': 'un/mock/proxy', 'lodash.template': 'un/mock/proxy',
'serialize-javascript': 'un/mock/proxy', 'serialize-javascript': 'un/mock/proxy',
// Vue 3 // Vue 3
'estree-walker': 'un/mock/proxy',
'@babel/parser': 'un/mock/proxy', '@babel/parser': 'un/mock/proxy',
'@vue/compiler-core': 'un/mock/proxy', '@vue/compiler-core': 'un/mock/proxy',
'@vue/compiler-dom': 'un/mock/proxy', '@vue/compiler-dom': 'un/mock/proxy',
@ -214,13 +215,16 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
// prod // prod
nitroContext._nuxt.srcDir, nitroContext._nuxt.srcDir,
nitroContext._nuxt.rootDir, nitroContext._nuxt.rootDir,
nitroContext._nuxt.buildDir nitroContext._nuxt.buildDir,
'vue',
'@vue/'
]) || []), ]) || []),
nitroContext._nuxt.serverDir, nitroContext._nuxt.serverDir,
...nitroContext.middleware.map(m => m.handle) ...nitroContext.middleware.map(m => m.handle)
], ],
traceOptions: { traceOptions: {
base: nitroContext._nuxt.rootDir base: '/',
processCwd: nitroContext._nuxt.rootDir
} }
}))) })))
} }

View File

@ -12,51 +12,53 @@ export interface NodeExternalsOptions {
} }
export function externals (opts: NodeExternalsOptions): Plugin { export function externals (opts: NodeExternalsOptions): Plugin {
const resolvedExternals = {} const resolvedExternals = new Set<string>()
return { return {
name: 'node-externals', name: 'node-externals',
resolveId (id) { resolveId (id) {
// Internals // Internals
if (id.startsWith('\x00') || id.includes('?')) { if (!id || id.startsWith('\x00') || id.includes('?')) {
return null return null
} }
// Normalize from node_modules
const _id = id.split('node_modules/').pop()
// Resolve relative paths and exceptions // Resolve relative paths and exceptions
if (id.startsWith('.') || opts.ignore.find(i => id.startsWith(i))) { if (_id.startsWith('.') || opts.ignore.find(i => _id.startsWith(i))) {
return null return null
} }
// Bundle ts // Bundle ts
if (id.endsWith('.ts')) { if (_id.endsWith('.ts')) {
return null return null
} }
for (const dir of opts.moduleDirectories) { // Try to resolve for nft
if (id.startsWith(dir)) { if (opts.trace !== false) {
id = id.substr(dir.length + 1) let _resolvedId = _id
break try { _resolvedId = require.resolve(_resolvedId, { paths: opts.moduleDirectories }) } catch (_err) {}
resolvedExternals.add(_resolvedId)
} }
}
try {
resolvedExternals[id] = require.resolve(id, { paths: opts.moduleDirectories })
} catch (_err) { }
return { return {
id, id: _id,
external: 'absolute' external: true
} }
}, },
async buildEnd () { async buildEnd () {
if (opts.trace !== false) { if (opts.trace !== false) {
const { fileList } = await nodeFileTrace(Object.values(resolvedExternals), opts.traceOptions) const tracedFiles = await nodeFileTrace(Array.from(resolvedExternals), opts.traceOptions)
await Promise.all(fileList.map(async (file) => { .then(r => r.fileList.map(f => resolve(opts.traceOptions.base, f)))
if (!file.startsWith('node_modules')) {
await Promise.all(tracedFiles.map(async (file) => {
if (!file.includes('node_modules')) {
return return
} }
// TODO: Minify package.json // TODO: Minify package.json
const src = resolve(opts.traceOptions.base, file) const src = resolve(opts.traceOptions.base, file)
const dst = resolve(opts.outDir, file) const dst = resolve(opts.outDir, 'node_modules', file.split('node_modules/').pop())
await mkdirp(dirname(dst)) await mkdirp(dirname(dst))
await copyFile(src, dst) await copyFile(src, dst)
})) }))

View File

@ -40,6 +40,9 @@ function serverStandalone (ctx: WebpackConfigContext) {
'vuex5', 'vuex5',
'!', '!',
'-!', '-!',
'~',
'@',
'#',
...ctx.options.build.transpile ...ctx.options.build.transpile
] ]
@ -52,7 +55,6 @@ function serverStandalone (ctx: WebpackConfigContext) {
) { ) {
return cb(null, false) return cb(null, false)
} }
// console.log('External:', request)
return cb(null, true) return cb(null, true)
}) })
} }