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

fix: replace @tresjs/post-processing with its subpackage exports #148

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
63 changes: 41 additions & 22 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,50 @@ export default defineNuxtModule<ModuleOptions>({
},
])

const pkg = await readPackageJSON(nuxt.options.rootDir)
const coreDeps = Object.keys({ ...pkg.dependencies, ...pkg.devDependencies }).filter(d => d.startsWith('@tresjs/'))

const mods = new Set([...options.modules, ...coreDeps])
// @tresjs/post-processing doesn't have default subpackage export,
// we need to import submodules manually here.
// The resolvePath call will not be able to resolve the entry file and will fail
// when reading the content to get the exports.
if (mods.has('@tresjs/post-processing')) {
mods.delete('@tresjs/post-processing')
mods.add('@tresjs/post-processing/three')
mods.add('@tresjs/post-processing/pmndrs')
}

nuxt.hook('prepare:types', ({ references }) => {
references.push({ types: '@tresjs/core' })
if (mods.has('@tresjs/post-processing/three')) {
references.push({ types: '@tresjs/post-processing/three' })
}
if (mods.has('@tresjs/post-processing/pmndrs')) {
references.push({ types: '@tresjs/post-processing/pmndrs' })
}
})

nuxt.options.vue.compilerOptions.isCustomElement = templateCompilerOptions.template.compilerOptions.isCustomElement

const pkg = await readPackageJSON(nuxt.options.rootDir)
const coreDeps = Object.keys({ ...pkg.dependencies, ...pkg.devDependencies }).filter(d => d.startsWith('@tresjs/'))
nuxt.options.vite.resolve = defu(nuxt.options.vite.resolve, {
dedupe: ['three'],
})

nuxt.options.vite.optimizeDeps = defu(nuxt.options.vite.optimizeDeps, {
include: ['three'],
})

const promises: Promise<void>[] = [
addComponent({
name: 'TresCanvas',
filePath: resolver.resolve('./runtime/TresCanvas.client.vue'),
}),
addComponent({
name: 'TresCanvas',
filePath: resolver.resolve('./runtime/TresCanvas.server.vue'),
}),
]

for (const mod of new Set([...options.modules, ...coreDeps])) {
if (mod === '@tresjs/core' || mod === '@tresjs/nuxt') {
Expand All @@ -79,33 +115,16 @@ export default defineNuxtModule<ModuleOptions>({
})
}
else {
addComponent({
promises.push(addComponent({
name,
filePath: mod,
export: name,
})
}))
}
}
}

nuxt.options.vite.resolve = defu(nuxt.options.vite.resolve, {
dedupe: ['three'],
})

nuxt.options.vite.optimizeDeps = defu(nuxt.options.vite.optimizeDeps, {
include: ['three'],
})

await Promise.all([
addComponent({
name: 'TresCanvas',
filePath: resolver.resolve('./runtime/TresCanvas.client.vue'),
}),
addComponent({
name: 'TresCanvas',
filePath: resolver.resolve('./runtime/TresCanvas.server.vue'),
}),
])
await Promise.all(promises)

if (options.devtools) {
setupDevToolsUI(nuxt, resolver)
Expand Down
Loading