-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.config.ts
86 lines (78 loc) · 2.54 KB
/
build.config.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
import type { NuxtModule } from '@nuxt/schema'
import { existsSync, promises as fsp } from 'node:fs'
import { defineBuildConfig } from "unbuild"
import { pathToFileURL } from 'node:url'
import { resolve } from 'pathe'
import mri from 'mri'
const args = mri(process.argv.slice(2))
export default defineBuildConfig({
failOnWarn: false,
declaration: true,
stub: args.stub,
entries: [
'src/module',
// @ts-ignore
{ input: 'src/types/', outDir: 'dist/types', ext: 'd.ts' },
{ input: 'src/runtime/', outDir: 'dist/runtime', ext: 'mjs' },
],
rollup: {
emitCJS: false,
cjsBridge: true,
},
externals: [
'@nuxt/schema',
'@nuxt/schema-edge',
'@nuxt/kit',
'@nuxt/kit-edge',
'nuxt',
'nuxt-edge',
'nuxt3',
'vue',
'vue-demi',
'@nuxtjs-alt/proxy'
],
hooks: {
async 'rollup:done' (ctx) {
// Generate CommonJS stup
await writeCJSStub(ctx.options.outDir)
// Load module meta
const moduleEntryPath = resolve(ctx.options.outDir, 'module.mjs')
const moduleFn: NuxtModule<any> = await import(
pathToFileURL(moduleEntryPath).toString()
).then(r => r.default || r).catch((err) => {
console.error(err)
console.error('Cannot load module. Please check dist:', moduleEntryPath)
return null
})
if (!moduleFn) {
return
}
const moduleMeta = await moduleFn.getMeta!()
// Enhance meta using package.json
if (ctx.pkg) {
if (!moduleMeta.name) {
moduleMeta.name = ctx.pkg.name
}
if (!moduleMeta.version) {
moduleMeta.version = ctx.pkg.version
}
}
// Write meta
const metaFile = resolve(ctx.options.outDir, 'module.json')
await fsp.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), 'utf8')
}
}
});
async function writeCJSStub (distDir: string) {
const cjsStubFile = resolve(distDir, 'module.cjs')
if (existsSync(cjsStubFile)) {
return
}
const cjsStub =
`module.exports = function(...args) {
return import('./module.mjs').then(m => m.default.call(this, ...args))
}
const _meta = module.exports.meta = require('./module.json')
module.exports.getMeta = () => Promise.resolve(_meta)`
await fsp.writeFile(cjsStubFile, cjsStub, 'utf8')
}