feat: automatically mock unresolved externals

This commit is contained in:
Pooya Parsa 2020-11-20 22:08:57 +01:00
parent 5c8ac226f1
commit b5b585c0c8
2 changed files with 20 additions and 0 deletions

View File

@ -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
}
}
}

View File

@ -19,6 +19,7 @@ import { resolvePath, MODULE_DIR } from '../utils'
import { dynamicRequire } from './dynamic-require' import { dynamicRequire } from './dynamic-require'
import { externals } from './externals' import { externals } from './externals'
import { timing } from './timing' import { timing } from './timing'
import { autoMock } from './automock'
export type RollupConfig = InputOptions & { output: OutputOptions } export type RollupConfig = InputOptions & { output: OutputOptions }
@ -179,6 +180,9 @@ export const getRollupConfig = (sigmaContext: SigmaContext) => {
mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils) 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 // https://github.com/rollup/plugins/tree/master/packages/commonjs
rollupConfig.plugins.push(commonjs({ rollupConfig.plugins.push(commonjs({
extensions: extensions.filter(ext => ext !== '.json') extensions: extensions.filter(ext => ext !== '.json')