-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodule.ts
137 lines (119 loc) · 3.67 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { readFile } from 'node:fs/promises'
import { addComponent, addImports, addVitePlugin, createResolver, defineNuxtModule, resolvePath } from '@nuxt/kit'
import * as core from '@tresjs/core'
import { templateCompilerOptions } from '@tresjs/core'
import { defu } from 'defu'
import { findExportNames } from 'mlly'
import { readPackageJSON } from 'pkg-types'
import glsl from 'vite-plugin-glsl'
import { setupDevToolsUI } from './devtools'
export interface ModuleOptions {
modules: string[]
devtools: boolean
glsl: boolean
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@tresjs/nuxt',
configKey: 'tres',
},
defaults: {
modules: [],
devtools: true,
glsl: false,
},
async setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
nuxt.options.build.transpile.push(/@tresjs/)
for (const name in core) {
if (name.match(/^use/)) {
addImports({
from: '@tresjs/core',
name,
})
}
}
addImports([
{
from: '@tresjs/core',
name: 'extend',
as: 'extendTres',
},
{
from: '@tresjs/core',
type: true,
name: 'TresObject',
},
])
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
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') {
continue
}
const entry = await resolvePath(mod)
if (entry === mod) {
continue
}
const imports = findExportNames(await readFile(entry, 'utf8'))
for (const name of imports) {
if (name.match(/^[a-z]/)) {
addImports({
from: mod,
name,
})
}
else {
promises.push(addComponent({
name,
filePath: mod,
export: name,
}))
}
}
}
await Promise.all(promises)
if (options.devtools) {
setupDevToolsUI(nuxt, resolver)
}
if (options.glsl) {
addVitePlugin(glsl())
}
},
})