UNPKG

7.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const utils_1 = require("./utils");
4const Path = require("path");
5const fs = require("fs-extra-plus");
6//////// for Postcss
7const postcss = require("postcss");
8const processors = [
9 require("autoprefixer"),
10 require("postcss-import"),
11 require("postcss-mixins"),
12 require("postcss-nested")
13];
14/////// for JS
15const rollup = require("rollup");
16const rollup_cjs = require("rollup-plugin-commonjs");
17const rollup_re = require("rollup-plugin-node-resolve");
18const rollup_ts = require("rollup-plugin-typescript2");
19const rollup_multi = require("rollup-plugin-multi-entry");
20/////// for handlebars
21const hbsp_1 = require("hbsp"); // promise style
22// --------- For Handlebars --------- //
23async 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}
33exports.tmplFiles = tmplFiles;
34// --------- /For Handlebars --------- //
35// --------- For postCss --------- //
36async 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 // we parse all of the .pcss files
43 for (let srcFile of entries) {
44 // read the file
45 let pcss = await fs.readFile(srcFile, "utf8");
46 var pcssNode = postcss.parse(pcss, {
47 from: srcFile
48 });
49 pcssNodes.push(pcssNode);
50 }
51 // build build the combined rootNode and its result
52 var rootNode = null;
53 for (let pcssNode of pcssNodes) {
54 rootNode = (rootNode) ? rootNode.append(pcssNode) : pcssNode;
55 }
56 var rootNodeResult = rootNode.toResult();
57 // we process the rootNodeResult
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 // we write the .css and .map files
67 await fs.writeFile(distFile, "", "utf8");
68 await fs.writeFile(mapFile, "", "utf8");
69 return;
70 }
71 // we write the .css and .map files
72 await fs.writeFile(distFile, pcssResult.css, "utf8");
73 await fs.writeFile(mapFile, pcssResult.map, "utf8");
74}
75exports.pcssFiles = pcssFiles;
76var defaultOpts = {
77 ts: true,
78 watch: false
79};
80/**
81 * @param {*} opts
82 * - ts?: boolean - (default true)
83 * -
84 * - watch: true | false (default false)
85 */
86async function rollupFiles(entries, distFile, opts) {
87 opts = Object.assign({}, defaultOpts, opts);
88 await utils_1.saferRemove("./.rpt2_cache", false);
89 // delete the previous ouutput files
90 var mapFile = distFile + ".map";
91 try {
92 // Note: Do not delete the distFile if we are in watch mode, otherwise, rollup throw an uncatched promise exception
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 // set the default rollup input options
102 const inputOptions = {
103 input: entries,
104 plugins: [rollup_multi(), rollup_cjs(), rollup_re()]
105 };
106 // --------- Exclude 3rd Party circular Dependencies --------- //
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 // NOTE: 2019-01-10 for now skip the plugin warning because of ONGENERATE_HOOK_DEPRECATED on rpt2
119 if (warning.code === 'PLUGIN_WARNING') {
120 skip = true;
121 }
122 if (!skip) {
123 console.log(`rollup warning - ${warning.message}`);
124 }
125 };
126 // --------- /Exclude 3rd Party circular Dependencies --------- //
127 // set the default rollup output options
128 // make the name from file name "web/js/lib-bundle.js" : "lib_bundle"
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 // if ts, then, we add the rollup_ts plugin
138 if (opts.ts || opts.tsconfig) {
139 let tsOpts = {
140 clean: true
141 };
142 if (opts.tsconfig) {
143 tsOpts.tsconfig = opts.tsconfig;
144 }
145 // Note: if we do not have clean:true, we get some exception when watch.
146 inputOptions.plugins.push(rollup_ts(tsOpts));
147 }
148 // if we have some globals, we add them accordingly
149 if (opts.globals) {
150 // for input, just set the external (clone to be safe(r))
151 inputOptions.external = Object.keys(opts.globals);
152 outputOptions.globals = opts.globals;
153 }
154 try {
155 // if it is watch mode, we do the watch
156 if (opts.watch) {
157 //wathOptions = { inputOptions };
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 // console.log('rollup watch', evt.code, evt.output);
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 // otherwise, we do the full build
182 else {
183 // bundle
184 const bundle = await rollup.rollup(inputOptions);
185 // write
186 await bundle.write(outputOptions);
187 }
188 // make sure the .rpt2_cache/ folder is delete (apparently, clean:true does not work)
189 //await fs.remove("./.rpt2_cache");
190 }
191 catch (ex) {
192 // make sure we write nothing in the file, to know nothing got compiled and fail early
193 await fs.writeFile(distFile, "", "utf8");
194 throw ex;
195 }
196}
197exports.rollupFiles = rollupFiles;
198// --------- /For Rollup (JavaScript) --------- //