Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better path resolution #925

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/internal-context/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
const { meta = { disableHMR: moduleOptions.disableHMR } } = twCtx.tryUse() ?? {}
const trackObjChanges = createObjProxy(configUpdatedHook, meta)

const { buildDir } = nuxt.options

const resolveConfigs = <T extends Partial<TWConfig> | string | undefined>(configs: T | T[], nuxt = useNuxt()) =>
((Array.isArray(configs) ? configs : [configs])
.filter(Boolean)
Expand All @@ -38,7 +40,7 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
return { config } as { config: NonNullable<T> }
}

const configFile = await (config.startsWith(nuxt.options.buildDir) ? config : findPath(config, { extensions: ['.js', '.cjs', '.mjs', '.ts'] }))
const configFile = await (config.startsWith(buildDir) ? config : findPath(config, { extensions: ['.js', '.cjs', '.mjs', '.ts'] }))
return configFile
? loadConfig({ configFile }).then(async (resolvedConfig) => {
const { configFile: resolvedConfigFile = configFile } = resolvedConfig
Expand Down Expand Up @@ -169,7 +171,7 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux

const layerConfigs = resolvedConfigsCtx.use().map((c) => {
if (c?.configFile) {
const configImport = `require(${JSON.stringify(/[/\\]node_modules[/\\]/.test(c.configFile) ? c.configFile : './' + relative(nuxt.options.buildDir, c.configFile))})`
const configImport = `require(${JSON.stringify(/[/\\]node_modules[/\\]/.test(c.configFile) ? c.configFile : './' + relative(buildDir, c.configFile))})`
return configUpdatedHook[c.configFile] ? `(() => {const cfg=configMerger(undefined, ${configImport});${configUpdatedHook[c.configFile]};return cfg;})()` : configImport
}

Expand All @@ -178,7 +180,7 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux

return [
`// generated by the @nuxtjs/tailwindcss <https://github.com/nuxt-modules/tailwindcss> module at ${(new Date()).toLocaleString()}`,
`const configMerger = require(${JSON.stringify(options.mergerPath)});\n`,
`const configMerger = require(${JSON.stringify(relative(buildDir, options.mergerPath))});\n`,
'const config = [',
layerConfigs.join(',\n'),
`].reduce((acc, curr) => configMerger(acc, curr), {});\n`,
Expand Down
5 changes: 3 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ export default defineNuxtModule<ModuleOptions>({
}

const ctx = await createInternalContext(moduleOptions, nuxt)
const resolver = createResolver(import.meta.url)

if (moduleOptions.editorSupport) {
const editorSupportConfig = resolvers.resolveEditorSupportConfig(moduleOptions.editorSupport)

if ((editorSupportConfig.autocompleteUtil) && !isNuxtMajorVersion(2, nuxt)) {
addImports({
name: 'autocompleteUtil',
from: createResolver(import.meta.url).resolve('./runtime/utils'),
from: resolver.resolve('./runtime/utils'),
as: 'tw',
...(typeof editorSupportConfig.autocompleteUtil === 'object' ? editorSupportConfig.autocompleteUtil : {}),
})
Expand Down Expand Up @@ -88,7 +89,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.hook('modules:done', async () => {
const _config = await ctx.loadConfigs()

const twConfig = ctx.generateConfig({ mergerPath: createResolver(import.meta.url).resolve('./runtime/merger.js') })
const twConfig = ctx.generateConfig({ mergerPath: resolver.resolve('./runtime/merger.js') })
ctx.registerHooks()

nuxt2ViewerConfig = twConfig.dst || _config
Expand Down
Loading