forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
76 lines (72 loc) · 2.16 KB
/
rollup.config.mjs
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 commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { readFileSync } from 'fs';
import handlebars from 'handlebars';
import { dirname, extname, resolve } from 'path';
import { terser } from 'rollup-plugin-terser';
const { precompile } = handlebars;
/**
* Custom plugin to parse handlebar imports and precompile
* the template on the fly. This reduces runtime by about
* half on large projects.
*/
const handlebarsPlugin = () => ({
resolveId: (file, importer) => {
if (extname(file) === '.hbs') {
return resolve(dirname(importer), file);
}
return null;
},
load: file => {
if (extname(file) === '.hbs') {
const template = readFileSync(file, 'utf8').toString().trim();
const templateSpec = precompile(template, {
strict: true,
noEscape: true,
preventIndent: true,
knownHelpersOnly: true,
knownHelpers: {
ifdef: true,
equals: true,
notEquals: true,
containsSpaces: true,
union: true,
intersection: true,
enumerator: true,
escapeComment: true,
escapeDescription: true,
camelCase: true,
},
});
return `export default ${templateSpec};`;
}
return null;
},
});
const getPlugins = () => {
const plugins = [
nodeResolve(),
commonjs({
sourceMap: false,
}),
handlebarsPlugin(),
typescript({
module: 'esnext',
}),
];
if (process.env.NODE_ENV === 'development') {
return plugins;
}
return [...plugins, terser()];
};
export default {
input: './src/index.ts',
output: {
exports: 'named',
file: './dist/index.js',
format: 'cjs',
},
external: ['camelcase', 'commander', 'fs-extra', 'handlebars', 'json-schema-ref-parser'],
plugins: getPlugins(),
};