From 4e1865358c1597cb68cc96bef2b30e2811fcd899 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 19 Apr 2021 22:41:02 +0200 Subject: [PATCH] fix: issues with externals outside of rootDir --- packages/nitro/src/rollup/config.ts | 8 +++- .../nitro/src/rollup/plugins/externals.ts | 40 ++++++++++--------- packages/webpack/src/configs/server.ts | 4 +- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/nitro/src/rollup/config.ts b/packages/nitro/src/rollup/config.ts index 0fb5585a30..4de9315b3d 100644 --- a/packages/nitro/src/rollup/config.ts +++ b/packages/nitro/src/rollup/config.ts @@ -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 } }))) } diff --git a/packages/nitro/src/rollup/plugins/externals.ts b/packages/nitro/src/rollup/plugins/externals.ts index c80dabf3eb..abc72f880d 100644 --- a/packages/nitro/src/rollup/plugins/externals.ts +++ b/packages/nitro/src/rollup/plugins/externals.ts @@ -12,51 +12,53 @@ export interface NodeExternalsOptions { } export function externals (opts: NodeExternalsOptions): Plugin { - const resolvedExternals = {} + const resolvedExternals = new Set() + 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) })) diff --git a/packages/webpack/src/configs/server.ts b/packages/webpack/src/configs/server.ts index 78cc108160..7a98788b22 100644 --- a/packages/webpack/src/configs/server.ts +++ b/packages/webpack/src/configs/server.ts @@ -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) }) }