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