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',
'serialize-javascript': 'un/mock/proxy',
// Vue 3
'estree-walker': 'un/mock/proxy',
'@babel/parser': 'un/mock/proxy',
'@vue/compiler-core': 'un/mock/proxy',
'@vue/compiler-dom': 'un/mock/proxy',
@ -214,13 +215,16 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
// prod
nitroContext._nuxt.srcDir,
nitroContext._nuxt.rootDir,
nitroContext._nuxt.buildDir
nitroContext._nuxt.buildDir,
'vue',
'@vue/'
]) || []),
nitroContext._nuxt.serverDir,
...nitroContext.middleware.map(m => m.handle)
],
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 {
const resolvedExternals = {}
const resolvedExternals = new Set<string>()
return {
name: 'node-externals',
resolveId (id) {
// Internals
if (id.startsWith('\x00') || id.includes('?')) {
if (!id || id.startsWith('\x00') || id.includes('?')) {
return null
}
// Normalize from node_modules
const _id = id.split('node_modules/').pop()
// 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
}
// Bundle ts
if (id.endsWith('.ts')) {
if (_id.endsWith('.ts')) {
return null
}
for (const dir of opts.moduleDirectories) {
if (id.startsWith(dir)) {
id = id.substr(dir.length + 1)
break
}
// Try to resolve for nft
if (opts.trace !== false) {
let _resolvedId = _id
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 {
id,
external: 'absolute'
id: _id,
external: true
}
},
async buildEnd () {
if (opts.trace !== false) {
const { fileList } = await nodeFileTrace(Object.values(resolvedExternals), opts.traceOptions)
await Promise.all(fileList.map(async (file) => {
if (!file.startsWith('node_modules')) {
const tracedFiles = await nodeFileTrace(Array.from(resolvedExternals), opts.traceOptions)
.then(r => r.fileList.map(f => resolve(opts.traceOptions.base, f)))
await Promise.all(tracedFiles.map(async (file) => {
if (!file.includes('node_modules')) {
return
}
// TODO: Minify package.json
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 copyFile(src, dst)
}))

View File

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