-
Notifications
You must be signed in to change notification settings - Fork 22
/
haoma-compile.config.ts
83 lines (80 loc) · 2.39 KB
/
haoma-compile.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
import fs from 'fs-extra'
import { defineBabelPlugin, getCompileConfig } from 'haoma'
import pa from 'path'
import { FirstParameter } from './src/types'
const removeIsTypePlugin = defineBabelPlugin(t => ({
visitor: {
ImportDeclaration: {
exit(path) {
if (
path.node.specifiers.length === 1 &&
t.isImportSpecifier(path.node.specifiers[0]) &&
t.isIdentifier(path.node.specifiers[0].imported) &&
path.node.specifiers[0].imported.name === 'isType'
) {
path.remove()
}
},
},
CallExpression: {
exit(path) {
if (
t.isIdentifier(path.node.callee) &&
path.node.callee.name === 'isType'
) {
path.replaceWith(t.booleanLiteral(true))
}
},
},
},
}))
const addExtension: ReturnType<
FirstParameter<typeof defineBabelPlugin>
>['visitor']['ImportDeclaration'] = {
exit(path, { filename }) {
if (path.node.source) {
const modulePath = path.node.source.value
if (modulePath.startsWith('.')) {
for (const ext of ['.ts', '/index.ts', '.js', '/index.js']) {
const modulePathWithExt = modulePath + ext
const resolvedPath = pa.join(pa.dirname(filename), modulePathWithExt)
if (fs.pathExistsSync(resolvedPath)) {
path.node.source.value = modulePathWithExt.replace(/\.ts$/, '.js')
break
}
}
}
}
},
}
const addExtensionPlugin = defineBabelPlugin(() => ({
visitor: {
ImportDeclaration: addExtension,
ExportAllDeclaration: addExtension as any,
ExportNamedDeclaration: addExtension as any,
},
}))
export default [
getCompileConfig({
name: 'esm',
inputFiles: ['src/**/*.{ts,js}', '!**/*.{test,perf}.*', '!**/__*'],
module: 'esm',
target: file => (/src\/(dev|x)\//.test(file) ? 'node' : 'browser'),
outDir: 'lib',
emitDts: true,
rollupDts: true,
rollupDtsFiles: ['**/index.d.ts'],
rollupDtsExcludeFiles: ['**/validator/**'],
rollupDtsIncludedPackages: ['type-fest', 'ts-essentials'],
plugins: [removeIsTypePlugin, addExtensionPlugin],
}),
getCompileConfig({
name: 'cjs',
inputFiles: ['src/**/*.{ts,js}', '!**/*.{test,perf}.*', '!**/__*'],
module: 'cjs',
target: file => (/src\/(dev|x)\//.test(file) ? 'node' : 'browser'),
outDir: 'lib/_cjs',
emitDts: false,
plugins: [removeIsTypePlugin],
}),
]