forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
80 lines (75 loc) · 2.11 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
'use strict';
const commonjs = require('@rollup/plugin-commonjs');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { terser } = require('rollup-plugin-terser');
const typescript = require('rollup-plugin-typescript2');
const handlebars = require('handlebars');
const path = require('path');
const fs = require('fs');
const pkg = require('./package.json');
const external = Object.keys(pkg.dependencies);
/**
* 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 (path.extname(file) === '.hbs') {
return path.resolve(path.dirname(importer), file);
}
return null;
},
load: (file) => {
if (path.extname(file) === '.hbs') {
const template = fs.readFileSync(file, 'utf8').toString().trim();
const templateSpec = handlebars.precompile(template, {
strict: true,
noEscape: true,
preventIndent: true,
knownHelpersOnly: true,
knownHelpers: {
equals: true,
notEquals: true,
containsSpaces: true,
union: true,
intersection: true,
enumerator: true,
escapeQuotes: true,
},
});
return `export default ${templateSpec};`;
}
return null;
}
});
const getPlugins = () => {
const plugins = [
handlebarsPlugin(),
typescript(),
nodeResolve(),
commonjs(),
]
if (process.env.NODE_ENV === 'development') {
return plugins;
}
return [...plugins, terser()];
};
module.exports = {
input: './src/index.ts',
output: {
file: './dist/index.js',
format: 'cjs',
},
external: [
'fs',
'os',
'util',
'path',
'http',
'https',
'handlebars/runtime',
...external,
],
plugins: getPlugins(),
};