UNPKG

8.84 kBJavaScriptView Raw
1"use strict";
2var __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}));
9var __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});
14var __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};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.rollupFiles = exports.pcssFiles = exports.tmplFiles = void 0;
26const plugin_commonjs_1 = __importDefault(require("@rollup/plugin-commonjs"));
27const plugin_multi_entry_1 = __importDefault(require("@rollup/plugin-multi-entry"));
28const plugin_node_resolve_1 = __importDefault(require("@rollup/plugin-node-resolve"));
29const backlib_1 = require("backlib");
30const fs = __importStar(require("fs-extra"));
31/////// for handlebars
32const hbsp_1 = require("hbsp"); // promise style
33const Path = __importStar(require("path"));
34//////// for Postcss
35const postcss_1 = __importDefault(require("postcss"));
36/////// for JS
37const rollup = __importStar(require("rollup"));
38const rollup_plugin_typescript2_1 = __importDefault(require("rollup-plugin-typescript2")); // TODO: might want to update to @rollup/plugin-typescript (But it is a different implementation)
39const utils_1 = require("./utils");
40const processors = [
41 require("autoprefixer"),
42 require("postcss-import"),
43 require("postcss-mixins"),
44 require("postcss-nested")
45];
46// --------- For Handlebars --------- //
47async 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}
57exports.tmplFiles = tmplFiles;
58// --------- /For Handlebars --------- //
59// --------- For postCss --------- //
60async 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 // we parse all of the .pcss files
68 for (let srcFile of entries) {
69 // read the file
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 // build build the combined rootNode and its result
77 let rootNode = null;
78 for (let pcssNode of pcssNodes) {
79 rootNode = (rootNode) ? rootNode.append(pcssNode) : pcssNode;
80 }
81 const rootNodeResult = rootNode.toResult();
82 // we process the rootNodeResult
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 // we write the .css and .map files
92 await fs.writeFile(distFile, "", "utf8");
93 await fs.writeFile(mapFile, "", "utf8");
94 return;
95 }
96 // we write the .css and .map files
97 await fs.writeFile(distFile, pcssResult.css, "utf8");
98 await fs.writeFile(mapFile, pcssResult.map.toString(), "utf8");
99}
100exports.pcssFiles = pcssFiles;
101const defaultOpts = {
102 ts: true,
103 watch: false
104};
105/**
106 * @param {*} opts
107 * - ts?: boolean - (default true)
108 * -
109 * - watch: true | false (default false)
110 */
111async function rollupFiles(entries, distFile, opts) {
112 opts = Object.assign({}, defaultOpts, opts);
113 await backlib_1.saferRemove("./.rpt2_cache");
114 // delete the previous ouutput files
115 const mapFile = distFile + ".map";
116 try {
117 // Note: Do not delete the distFile if we are in watch mode, otherwise, rollup throw an uncatched promise exception
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 // set the default rollup input options
127 const inputOptions = {
128 input: entries,
129 plugins: [plugin_multi_entry_1.default(), plugin_commonjs_1.default(), plugin_node_resolve_1.default()]
130 };
131 // --------- Exclude 3rd Party circular Dependencies --------- //
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 // NOTE: 2019-01-10 for now skip the plugin warning because of ONGENERATE_HOOK_DEPRECATED on rpt2
144 if (warning.code === 'PLUGIN_WARNING') {
145 skip = true;
146 }
147 if (!skip) {
148 console.log(`rollup warning - ${warning.message}`);
149 }
150 };
151 // --------- /Exclude 3rd Party circular Dependencies --------- //
152 // set the default rollup output options
153 // make the name from file name "web/js/lib-bundle.js" : "lib_bundle"
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 // if ts, then, we add the rollup_ts plugin
163 if (opts.ts || opts.tsconfig) {
164 let tsOpts = {
165 clean: true
166 };
167 if (opts.tsconfig) {
168 tsOpts.tsconfig = opts.tsconfig;
169 }
170 // Note: if we do not have clean:true, we get some exception when watch.
171 inputOptions.plugins.push(rollup_plugin_typescript2_1.default(tsOpts));
172 }
173 // if we have some globals, we add them accordingly
174 if (opts.globals) {
175 // for input, just set the external (clone to be safe(r))
176 inputOptions.external = Object.keys(opts.globals);
177 outputOptions.globals = opts.globals;
178 }
179 try {
180 // if it is watch mode, we do the watch
181 if (opts.watch) {
182 //wathOptions = { inputOptions };
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 // console.log('rollup watch', evt.code, evt.output);
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 // otherwise, we do the full build
205 else {
206 // bundle
207 const bundle = await rollup.rollup(inputOptions);
208 // write
209 await bundle.write(outputOptions);
210 }
211 // make sure the .rpt2_cache/ folder is delete (apparently, clean:true does not work)
212 //await fs.remove("./.rpt2_cache");
213 }
214 catch (ex) {
215 // make sure we write nothing in the file, to know nothing got compiled and fail early
216 await fs.writeFile(distFile, "", "utf8");
217 throw ex;
218 }
219}
220exports.rollupFiles = rollupFiles;
221// --------- /For Rollup (JavaScript) --------- //