forked from aurelia/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.plugins.js
122 lines (109 loc) · 2.91 KB
/
rollup.plugins.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
import { dirname } from 'path';
import * as fse from 'fs-extra';
import { createFilter } from 'rollup-pluginutils';
import { minify } from 'html-minifier';
import { exec } from 'child_process';
/**
* @param {{ dir: string }} options
* @returns {import('rollup').Plugin}
*/
export function typings(options = {}) {
return {
name: 'rollup-plugin-typings-aurelia-ux',
buildEnd: function(error) {
if (error) {
return;
}
exec(`npm run build:typings ${options.dir ? ` -- --declarationDir ${options.dir}` : ''}`, (error) => {
if (error) {
console.log('Failure building typings:', error.toString());
}
});
}
}
}
/**
* @param {{ verbose: boolean, files: { from: string, to: string }[] }} options
* @returns {import('rollup').Plugin}
*/
export function copy (options = {}) {
const { verbose = false, files = [] } = options;
const name = 'rollup-plugin-copy-fork-aurelia';
return {
name: name,
buildEnd: function(error) {
if (error) {
return;
}
for (const { from, to } of files) {
const targetDir = dirname(to);
fse
.ensureDir(targetDir)
.then(() =>
fse
.copy(from, to)
.then(
() => {
if (verbose) {
success(name, from, to);
}
},
ex => {
fatal(name, from, to, ex);
}
)
);
}
}
};
}
function success (name, src, dest) {
console.log('(' + name + ") '" + src + "' -> '" + dest + "' (" + '\u2714' + ')');
}
function fatal (name, src, dest, err) {
console.error('(' + name + ") '" + src + "' -> '" + dest + "' (" + '\u2718' + ')');
console.error();
console.error(' ' + err);
process.exit(err.errno);
}
const OPTIONS_HTML_MINIFIER = {
collapseWhitespace: true,
collapseBooleanAttributes: true,
conservativeCollapse: true
};
/**
* @param {{ include: string | RegExp; exclude: string | RegExp }} options
* @returns {import('rollup').Plugin}
*/
export function html(options = {}) {
if (!options.include) {
options.include = '**/*.html'
}
const filter = createFilter(options.include, options.exclude);
return {
name: 'rollup-plugin-html-fork-aurelia',
transform(code, id) {
if (filter(id)) {
const x = {
code: `export default ${JSON.stringify(minify(code, OPTIONS_HTML_MINIFIER))};`,
// code: `export default ${JSON.stringify(code)};`,
map: { mappings: '' }
};
return x;
}
}
};
}
/**
* @param {string[]} fileNames
* @param {string[]} moduleNames
*/
export function buildCopyInstruction(fileNames, moduleNames, targetDir) {
const files = [];
moduleNames.forEach(moduleName => {
fileNames.forEach(fileName => {
files.push({ from: `src/${fileName}`, to: `${targetDir}/${moduleName}/${fileName}` });
});
});
return files;
}