From b5b585c0c80fac688947433df0a73d5d6a823397 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 20 Nov 2020 22:08:57 +0100 Subject: [PATCH] feat: automatically mock unresolved externals --- packages/nitro/src/rollup/automock.ts | 16 ++++++++++++++++ packages/nitro/src/rollup/config.ts | 4 ++++ 2 files changed, 20 insertions(+) create mode 100644 packages/nitro/src/rollup/automock.ts diff --git a/packages/nitro/src/rollup/automock.ts b/packages/nitro/src/rollup/automock.ts new file mode 100644 index 0000000000..7cec8a51f5 --- /dev/null +++ b/packages/nitro/src/rollup/automock.ts @@ -0,0 +1,16 @@ +import consola from 'consola' + +export function autoMock () { + return { + name: 'auto-mock', + resolveId (src: string) { + if (src && !src.startsWith('.') && !src.includes('?') && !src.includes('.js')) { + consola.info('Automatically mocking', src) + return { + id: require.resolve('@nuxt/un/runtime/mock/proxy') + } + } + return null + } + } +} diff --git a/packages/nitro/src/rollup/config.ts b/packages/nitro/src/rollup/config.ts index e76d91cd91..8e811154d9 100644 --- a/packages/nitro/src/rollup/config.ts +++ b/packages/nitro/src/rollup/config.ts @@ -19,6 +19,7 @@ import { resolvePath, MODULE_DIR } from '../utils' import { dynamicRequire } from './dynamic-require' import { externals } from './externals' import { timing } from './timing' +import { autoMock } from './automock' export type RollupConfig = InputOptions & { output: OutputOptions } @@ -179,6 +180,9 @@ export const getRollupConfig = (sigmaContext: SigmaContext) => { mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils) })) + // Automatically mock unresolved externals + rollupConfig.plugins.push(autoMock()) + // https://github.com/rollup/plugins/tree/master/packages/commonjs rollupConfig.plugins.push(commonjs({ extensions: extensions.filter(ext => ext !== '.json')