/
rollup.ts
76 lines (70 loc) · 2.08 KB
/
rollup.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
import { type InputPluginOption, type Plugin, rollup } from 'rollup'
import {
default as ViteVueJsx,
type Options as VueJsxOptions,
} from '@vitejs/plugin-vue-jsx'
export { default as RollupEsbuildPlugin } from 'rollup-plugin-esbuild'
export { default as RollupVue } from 'unplugin-vue/rollup'
export { default as RollupVue2 } from '@vitejs/plugin-vue2'
export const RollupVueJsx = ViteVueJsx as (options?: VueJsxOptions) => Plugin
export { default as RollupJson } from '@rollup/plugin-json'
export const RollupToStringPlugin = (): Plugin => {
return {
name: 'to-string',
transform: (code) => `export default \`${code.replace(/`/g, '\\`')}\``,
}
}
export const RollupRemoveVueFilePathPlugin = (): Plugin => {
const REGEX = [
/\[["']__file["'],\s*["'](.*?)["']]/g,
/__component__\.options\.__file.*/,
]
return {
name: 'remove-vue-filepath',
transform(code: string) {
const transformed = code
.replace(REGEX[0], '__FILE__')
.replace(REGEX[1], '__FILE__')
if (code !== transformed) return transformed
},
}
}
export const RollupEscapeNullCharacterPlugin = (): Plugin => {
return {
name: 'escape-null-character',
generateBundle(options, bundle) {
for (const filename of Object.keys(bundle)) {
const b = bundle[filename]
if (b.type !== 'chunk') continue
if (b.code.includes('\0')) {
b.code = b.code.replace(/\0/g, '[NULL]')
}
}
},
}
}
export async function rollupBuild(file: string, plugins: InputPluginOption) {
const bundle = await rollup({
input: [file],
external: [
'vue',
'@vueuse/core',
'/plugin-vue/export-helper',
'\0plugin-vue2:normalizer',
/^\/vue-macros\/.*/,
],
plugins,
treeshake: false,
onwarn(warning, defaultHandler) {
if (warning.code === 'UNUSED_EXTERNAL_IMPORT') return
defaultHandler(warning)
},
})
const output = await bundle.generate({
format: 'esm',
sourcemap: false,
})
return output.output
.map((file) => (file.type === 'chunk' ? file.code : file.fileName))
.join('\n')
}