-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
131 lines (119 loc) · 4.06 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'dotenv/config';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import html from '@rollup/plugin-html';
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
import ejs from 'rollup-plugin-ejs';
import { minify } from 'html-minifier';
import { extname } from 'path';
import postcss from 'rollup-plugin-postcss';
import postcssImport from 'postcss-import';
import pkg from './package.json';
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;
const replaceOptions = {
preventAssignment: true,
values: Object.entries(process.env).reduce(
(o, [key, value]) => {
o[`process.env.${key}`] = JSON.stringify(value);
return o;
},
{
'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development'),
'process.env.APP_CONFIG.receipts.htmlFile': JSON.stringify(pkg.config.receipts.htmlFile)
}
)
};
export default [
{
input: 'src/app.js',
output: {
dir: 'deploy/lib',
format: 'cjs', // immediately-invoked function expression — suitable for <script> tags
sourcemap: false
},
plugins: [
replace(replaceOptions),
resolve(), // tells Rollup how to find date-fns in node_modules
commonjs(), // converts date-fns to ES modules
production && terser({ mangle: { toplevel: false } })
]
},
{
input: 'src/receipts-app/index.js',
output: {
dir: 'deploy',
format: 'iife',
sourcemap: false
},
plugins: [
replace(replaceOptions),
resolve(), // tells Rollup how to find date-fns in node_modules
commonjs(), // converts date-fns to ES modules
postcss({
// extract: true,
minimize: !!production,
plugins: [postcssImport()]
}),
{
name: 'ejs-min',
transform: function transform(code, tplFilePath) {
if (extname(tplFilePath) === '.html') {
code = minify(code, { collapseWhitespace: true, removeComments: true, trimCustomFragments: false });
return {
code,
map: { mappings: '' }
};
}
}
},
ejs({
include: '**/*.html'
}),
(function () {
const htmlPlugin = html({
fileName: pkg.config.receipts.htmlFile,
attributes: { html: { lang: 'uk' } },
title: pkg.name,
template: async ({ attributes, files, meta, title }) => {
const makeHtmlAttributes = (attributes) => {
if (!attributes) {
return '';
}
const keys = Object.keys(attributes);
return keys.reduce((result, key) => `${result} ${key}="${attributes[key]}"`, '');
};
const scripts = (files.js || [])
.map(({ code, source }) => (code || source) && `<script>${code || source}</script>`)
.filter((html) => !!html)
.join('');
const css = (files.css || [])
.map(({ code, source }) => (code || source) && `<style>${code || source}</style>`)
.filter((html) => !!html)
.join('');
const metas = meta
.map((input) => {
const attrs = makeHtmlAttributes(input);
return `<meta${attrs}>`;
})
.join('');
return `<!doctype html><html${makeHtmlAttributes(
attributes.html
)}><head>${metas}<title>${title}</title>${css}</head><body>${scripts}</body></html>`;
}
});
const _generateBundle = htmlPlugin.generateBundle;
htmlPlugin.generateBundle = async function (output, bundle) {
await _generateBundle.call(this, output, bundle);
Object.keys(bundle)
.filter((fileName) => fileName !== pkg.config.receipts.htmlFile)
.forEach((fileName) => delete bundle[fileName]);
};
return htmlPlugin;
})(),
production && terser()
]
}
];