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