1 | import { now, saferRemove } from './utils';
|
2 | import * as Path from 'path';
|
3 | import * as fs from 'fs-extra-plus';
|
4 |
|
5 |
|
6 | const postcss = require("postcss");
|
7 | const processors = [
|
8 | require("autoprefixer"),
|
9 | require("postcss-import"),
|
10 | require("postcss-mixins"),
|
11 | require("postcss-nested")
|
12 | ];
|
13 |
|
14 |
|
15 | import * as rollup from 'rollup';
|
16 | import rollup_cjs = require('rollup-plugin-commonjs');
|
17 | import rollup_re = require('rollup-plugin-node-resolve');
|
18 | import rollup_ts = require('rollup-plugin-typescript2');
|
19 | import rollup_multi = require('rollup-plugin-multi-entry');
|
20 |
|
21 |
|
22 | import { precompile as hbsPrecompile } from 'hbsp';
|
23 |
|
24 |
|
25 | export async function tmplFiles(files: string[], distFile: string) {
|
26 |
|
27 | await fs.saferRemove([distFile]);
|
28 |
|
29 | const templateContent = [];
|
30 |
|
31 | for (let file of files) {
|
32 |
|
33 | const htmlTemplate = await fs.readFile(file, "utf8");
|
34 | const template = await hbsPrecompile(file, htmlTemplate);
|
35 | templateContent.push(template);
|
36 | }
|
37 |
|
38 | await fs.writeFile(distFile, templateContent.join("\n"), "utf8");
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 | export async function pcssFiles(entries: string[], distFile: string) {
|
44 |
|
45 | const mapFile = distFile + ".map";
|
46 | let pcssResult: any;
|
47 | try {
|
48 |
|
49 | await fs.saferRemove([distFile, mapFile]);
|
50 |
|
51 | const processor = postcss(processors);
|
52 | const pcssNodes = [];
|
53 |
|
54 |
|
55 | for (let srcFile of entries) {
|
56 |
|
57 | let pcss = await fs.readFile(srcFile, "utf8");
|
58 |
|
59 | const pcssNode = postcss.parse(pcss, {
|
60 | from: srcFile
|
61 | });
|
62 | pcssNodes.push(pcssNode);
|
63 | }
|
64 |
|
65 |
|
66 | let rootNode = null;
|
67 | for (let pcssNode of pcssNodes) {
|
68 | rootNode = (rootNode) ? rootNode.append(pcssNode) : pcssNode;
|
69 | }
|
70 | const rootNodeResult = rootNode.toResult();
|
71 |
|
72 |
|
73 | pcssResult = await processor.process(rootNodeResult, {
|
74 | from: "undefined",
|
75 | to: distFile,
|
76 | map: { inline: false }
|
77 | });
|
78 | } catch (ex) {
|
79 | console.log(`postcss ERROR - Cannot process ${distFile} because (setting css empty file) \n${ex}`);
|
80 |
|
81 | await fs.writeFile(distFile, "", "utf8");
|
82 | await fs.writeFile(mapFile, "", "utf8");
|
83 | return;
|
84 | }
|
85 |
|
86 |
|
87 | await fs.writeFile(distFile, pcssResult.css, "utf8");
|
88 | await fs.writeFile(mapFile, pcssResult.map, "utf8");
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | export interface RollupFilesOptions {
|
95 | ts?: boolean;
|
96 |
|
97 | globals?: { [importName: string]: string };
|
98 | watch: boolean;
|
99 | tsconfig?: any;
|
100 | }
|
101 | const defaultOpts: RollupFilesOptions = {
|
102 | ts: true,
|
103 | watch: false
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | export async function rollupFiles(entries: string[], distFile: string, opts: RollupFilesOptions) {
|
113 | opts = Object.assign({}, defaultOpts, opts);
|
114 |
|
115 | await saferRemove("./.rpt2_cache", false);
|
116 |
|
117 |
|
118 | const mapFile = distFile + ".map";
|
119 | try {
|
120 |
|
121 | if (!opts.watch) {
|
122 | await saferRemove(distFile, false);
|
123 | }
|
124 | await saferRemove(mapFile, false);
|
125 | } catch (ex) {
|
126 | console.log(`Can't delete dist files`, ex);
|
127 | }
|
128 |
|
129 |
|
130 | const inputOptions: any = {
|
131 | input: entries,
|
132 | plugins: [rollup_multi(), rollup_cjs(), rollup_re()]
|
133 | };
|
134 |
|
135 |
|
136 | const excludeCircularWarningByImporters = ['node_modules/d3'];
|
137 | inputOptions.onwarn = function onwarn(warning: any) {
|
138 | let skip = false;
|
139 |
|
140 | if (warning.code === 'CIRCULAR_DEPENDENCY') {
|
141 | for (let skipImporter of excludeCircularWarningByImporters) {
|
142 | if (warning.importer.startsWith(skipImporter)) {
|
143 | skip = true;
|
144 | break;
|
145 | }
|
146 | }
|
147 | }
|
148 |
|
149 | if (warning.code === 'PLUGIN_WARNING') {
|
150 | skip = true;
|
151 | }
|
152 |
|
153 | if (!skip) {
|
154 | console.log(`rollup warning - ${warning.message}`);
|
155 | }
|
156 | };
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | const name = Path.parse(distFile).name.replace(/\W+/g, "_");
|
162 | const outputOptions: any = {
|
163 | file: distFile,
|
164 | format: 'iife',
|
165 | name,
|
166 | sourcemap: true,
|
167 | sourcemapFile: mapFile
|
168 | };
|
169 |
|
170 |
|
171 | if (opts.ts || opts.tsconfig) {
|
172 | let tsOpts: any = {
|
173 | clean: true
|
174 | };
|
175 | if (opts.tsconfig) {
|
176 | tsOpts.tsconfig = opts.tsconfig;
|
177 | }
|
178 |
|
179 | inputOptions.plugins.push(rollup_ts(tsOpts));
|
180 | }
|
181 |
|
182 |
|
183 | if (opts.globals) {
|
184 |
|
185 | inputOptions.external = Object.keys(opts.globals);
|
186 | outputOptions.globals = opts.globals;
|
187 | }
|
188 |
|
189 | try {
|
190 |
|
191 | if (opts.watch) {
|
192 |
|
193 | let watchOptions = { ...inputOptions };
|
194 | watchOptions.output = outputOptions;
|
195 | watchOptions.watch = { chokidar: true };
|
196 |
|
197 | const watcher = rollup.watch(watchOptions);
|
198 | let startTime: number;
|
199 |
|
200 | watcher.on('event', function (evt) {
|
201 |
|
202 | if (evt.code === 'START') {
|
203 | startTime = now();
|
204 | } else if (evt.code === 'END') {
|
205 | console.log(`Recompile ${distFile} done: ${Math.round(now() - startTime)}ms`);
|
206 | } else if (evt.code === 'ERROR') {
|
207 | console.log(`ERROR - Rollup/Typescript error when processing: ${distFile}`);
|
208 | console.log("\t" + evt.error);
|
209 | } else if (evt.code === 'FATAL') {
|
210 | console.log(`FATAL ERROR - Rollup/Typescript fatal error when processing ${distFile}\n
|
211 | >>>>>>>> MUST RESTART WATCH SESSION <<<<<<<<\n\n`, evt.error);
|
212 | }
|
213 |
|
214 | });
|
215 |
|
216 |
|
217 | }
|
218 |
|
219 | else {
|
220 |
|
221 | const bundle = await rollup.rollup(inputOptions);
|
222 |
|
223 |
|
224 | await bundle.write(outputOptions);
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | } catch (ex) {
|
231 |
|
232 | await fs.writeFile(distFile, "", "utf8");
|
233 | throw ex;
|
234 | }
|
235 | }
|
236 |
|
237 |
|
238 |
|