2021-10-02 11:44:30 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
import defu from 'defu'
|
2021-10-02 16:01:17 +00:00
|
|
|
import enhancedResolve from 'enhanced-resolve'
|
2021-10-02 11:44:30 +00:00
|
|
|
import { ResolveOptions } from 'webpack/types'
|
|
|
|
import { extendWebpackConfig, useNuxt } from '@nuxt/kit'
|
|
|
|
|
2021-10-02 16:01:17 +00:00
|
|
|
type UserResolveOptions = Parameters<typeof enhancedResolve.ResolverFactory['createResolver']>[0]
|
|
|
|
type ResolverOptions = Omit<UserResolveOptions, 'fileSystem'> & { fileSystem?: enhancedResolve.CachedInputFileSystem }
|
2021-10-02 11:44:30 +00:00
|
|
|
|
|
|
|
const DEFAULTS: UserResolveOptions = {
|
2021-10-02 16:01:17 +00:00
|
|
|
fileSystem: new enhancedResolve.CachedInputFileSystem(fs, 4000),
|
2021-10-04 20:25:27 +00:00
|
|
|
extensions: ['.ts', '.tsx', '.mjs', '.cjs', '.js', '.jsx', '.json', '.vue']
|
2021-10-02 11:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abstracted resolver factory which can be used in rollup, webpack, etc.
|
|
|
|
const createResolver = (resolveOptions: ResolverOptions) => {
|
|
|
|
const options = defu(resolveOptions, DEFAULTS) as UserResolveOptions
|
2021-10-02 16:01:17 +00:00
|
|
|
const resolver = enhancedResolve.ResolverFactory.createResolver(options)
|
2021-10-02 11:44:30 +00:00
|
|
|
|
|
|
|
const root = options.roots?.[0] || '.'
|
|
|
|
|
2021-10-02 16:01:17 +00:00
|
|
|
const promisifiedResolve = promisify(resolver.resolve.bind(resolver)) as (context: object, path: string, request: string, resolveContext: enhancedResolve.ResolveContext) => Promise<string | false>
|
2021-10-02 11:44:30 +00:00
|
|
|
|
|
|
|
const resolve = (id: string, importer?: string) => promisifiedResolve({}, importer || root, id, {})
|
|
|
|
|
|
|
|
return { resolve, resolver }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Webpack plugin to add first-level support for subpath exports, etc.
|
|
|
|
class EnhancedResolverPlugin {
|
|
|
|
resolver: ReturnType<typeof createResolver>
|
|
|
|
|
|
|
|
constructor (options: ResolverOptions) {
|
|
|
|
this.resolver = createResolver(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
apply (defaultResolver: any) {
|
|
|
|
const enhancedResolver = this.resolver
|
|
|
|
|
|
|
|
defaultResolver.getHook('resolve').tapPromise('EnhancedResolverPlugin', async (request) => {
|
|
|
|
const id = request.request
|
|
|
|
// Fall back to default webpack4 resolver if not a node_modules import
|
|
|
|
if (!id || !defaultResolver.isModule(id)) { return }
|
|
|
|
|
2021-10-18 10:12:01 +00:00
|
|
|
// Fall back to webpack4 resolver if resolving babel helpers
|
|
|
|
// https://github.com/nuxt/nuxt.js/blob/714249740690569eedf74aa7bca7ed31c01953d4/packages/babel-preset-app/src/index.js#L169
|
2021-10-18 12:47:26 +00:00
|
|
|
if (id.includes('@babel/')) { return }
|
2021-10-18 10:12:01 +00:00
|
|
|
|
2021-10-02 11:44:30 +00:00
|
|
|
const importer = request.context?.issuer
|
|
|
|
try {
|
|
|
|
const result = await enhancedResolver.resolve(id, importer)
|
|
|
|
// Fall back to default webpack4 resolver if we can't resolve id
|
|
|
|
if (!result) { return }
|
|
|
|
request.path = result
|
|
|
|
return request
|
|
|
|
} catch {
|
|
|
|
// Fall back to default webpack4 resolver in the event of error
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupBetterResolve () {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
extendWebpackConfig((config) => {
|
|
|
|
const isServer = config.name === 'server'
|
|
|
|
|
|
|
|
config.resolve = config.resolve || {}
|
|
|
|
config.resolve.plugins = config.resolve.plugins || []
|
|
|
|
|
|
|
|
config.resolve.plugins.push(new EnhancedResolverPlugin({
|
|
|
|
conditionNames: ['import', ...isServer ? ['node'] : []],
|
2021-10-04 20:25:27 +00:00
|
|
|
mainFields: ['module', ...isServer ? [] : ['browser'], 'main'],
|
2021-10-02 11:44:30 +00:00
|
|
|
alias: config.resolve.alias,
|
|
|
|
modules: config.resolve.modules,
|
|
|
|
plugins: config.resolve.plugins as Array<Exclude<ResolveOptions['plugins'][number], string>>,
|
|
|
|
roots: config.resolve.roots || [nuxt.options.rootDir]
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|