-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rollup.config.js
116 lines (101 loc) · 3.54 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
import { promises as fs } from 'fs';
import path from 'path';
import nodeResolve from 'rollup-plugin-node-resolve';
import includePaths from 'rollup-plugin-includepaths'
import commonjs from 'rollup-plugin-commonjs';
import copy from 'rollup-plugin-copy';
import posthtmlTemplate from './build/rollup-posthtml-template';
import babel from 'rollup-plugin-babel';
const INPUT_ROOT = 'src';
const INPUT_PATHS_ROOT = path.join(INPUT_ROOT, 'paths');
const INPUT_SHARED_ROOT = path.join(INPUT_ROOT, 'shared');
const INPUT_STATIC_ROOT = path.join(INPUT_ROOT, 'static');
const ENTRY_FILE_NAME = 'entry.js';
const TEMPLATE_FILE_NAME = 'template.html';
const GLOBAL_FILE_NAME = 'global.js';
const OUTPUT_ROOT = 'out';
const OUTPUT_STYLES = path.join(OUTPUT_ROOT, 'styles');
const OUTPUT_STYLES_SHARED = path.join(OUTPUT_STYLES, 'shared');
const OUTPUT_SCRIPTS = path.join(OUTPUT_ROOT, 'scripts');
const OUTPUT_SCRIPTS_SHARED = path.join(OUTPUT_SCRIPTS, 'shared');
const generateConfig = async () => {
let configs = [];
getGlobalConfig(configs);
await getPathsConfigs(configs);
return configs;
};
const getGlobalConfig = (configs) => {
const globalScriptPath = path.join(INPUT_SHARED_ROOT, 'scripts', GLOBAL_FILE_NAME);
const outputPath = path.join(OUTPUT_SCRIPTS_SHARED, GLOBAL_FILE_NAME);
const sharedStylesGlob = path.join(INPUT_SHARED_ROOT, 'styles/**/*.css').replace(/\\/g, '/'); // Windows path not supported by copy plugin
const staticGlob = path.join(INPUT_STATIC_ROOT, '/**/*.*').replace(/\\/g, '/'); // Windows path not supported by copy plugin
configs.push({
input: globalScriptPath,
output: {
name: 'global',
file: outputPath,
format: 'iife'
},
plugins: [
nodeResolve(),
copy({
targets: [
{ src: sharedStylesGlob, dest: OUTPUT_STYLES_SHARED },
{ src: staticGlob, dest: OUTPUT_ROOT },
],
verbose: true
})
]
})
};
const getPathsConfigs = async (configs) => {
try {
// Collect paths to process
const paths = await fs.readdir(INPUT_PATHS_ROOT);
for (const itemPath of paths) {
const itemRoot = path.join(INPUT_PATHS_ROOT, itemPath);
const itemFiles = await fs.readdir(itemRoot);
if (itemFiles.indexOf(ENTRY_FILE_NAME) < 0) {
throw Error(`Missing entry script for "${itemPath}" path`);
}
if (itemFiles.indexOf(TEMPLATE_FILE_NAME) < 0) {
throw Error(`Missing HTML template for "${itemPath}" path`);
}
const entryPath = path.join(itemRoot, ENTRY_FILE_NAME);
const templatePath = path.join(itemRoot, TEMPLATE_FILE_NAME).replace(/\\/g, '/'); // Windows path not supported by copy plugin
const bundlePath = path.join(OUTPUT_ROOT, 'scripts', `${itemPath}.js`);
const htmlPath = path.join(OUTPUT_ROOT, `${itemPath}.html`);
configs.push({
input: entryPath,
output: {
name: itemPath,
file: bundlePath,
format: 'iife'
},
plugins: [
babel({
exclude: 'node_modules/**',
plugins: [
[ '@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true } ],
'@babel/plugin-proposal-class-properties'
]
}),
includePaths({
paths: [ './' ]
}),
nodeResolve(),
commonjs({
sourceMap: false
}),
posthtmlTemplate({
src: templatePath,
dest: htmlPath
})
]
})
}
} catch (exc) {
console.error(exc);
}
};
export default generateConfig();