-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
115 lines (107 loc) · 2.39 KB
/
rollup.config.js
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
import { dirname, join } from 'node:path'
import { nodeExternals } from 'rollup-plugin-node-externals'
import fs, { mkdir } from 'node:fs/promises'
import copy from 'rollup-plugin-copy'
/**
* @return {import("rollup").RollupOptions[]}
*/
function main() {
const common = {
treeshake: true,
plugins: [
nodeExternals(),
inlinePlugin(),
createPackages(),
copy({
targets: [
{
src: './src/*.d.ts',
dest: ['./dist/cjs/', './dist/esm/'],
},
],
}),
],
}
return [
{
...common,
input: {
index: './src/index.js',
ast: './src/ast.js',
},
output: {
entryFileNames: '[name].mjs',
dir: './dist/esm',
format: 'esm',
},
},
{
...common,
input: {
index: './src/index.js',
ast: './src/ast.js',
},
output: {
dir: './dist/cjs',
format: 'cjs',
},
},
]
}
function inlinePlugin() {
return {
name: 'inline-plugin',
async transform(code, path) {
if (/(__inline_(\w+)[ ]?\=)/g.test(code)) {
// DO NOT REMOVE THESE, as they are used by `eval`
// to execute the inline path action
const { readFileSync } = await import('node:fs')
const __dirname = dirname(path)
const { join } = await import('node:path')
return code.replace(
/(__inline_(\w+)\s?\=\s?(.+)[;]?[\n]?)/g,
(...matchers) => {
return `__inline_${matchers[2]} = \`${escapeJS(
eval(matchers[3])
)}\``
}
)
}
},
}
}
export default main
function escapeJS(code) {
return escapeTemplateLiterals(escapeBackticks(code))
}
function escapeBackticks(code) {
return code.replace(/`/g, '\\`')
}
function escapeTemplateLiterals(code) {
return code.replace(/\$\{/g, '\\${')
}
/**
*
* @returns {import("rollup").Plugin}
*/
function createPackages() {
let meta
return {
name: 'create-final-packages',
async renderStart(info) {
const pkg = {}
if (info.format == 'cjs') {
pkg.type = 'commonjs'
}
await mkdir(info.dir, { recursive: true })
if (['esm', 'es'].includes(info.format)) {
pkg.type = 'module'
}
await fs.writeFile(
join(info.dir, 'package.json'),
JSON.stringify(pkg, null, 2),
'utf-8'
)
},
}
}