Skip to content

Commit dc5a7bb

Browse files
author
vakrilov
committed
feat: ship pre-parsed JSON files for all CSS
1 parent 787a751 commit dc5a7bb

File tree

1 file changed

+92
-57
lines changed

1 file changed

+92
-57
lines changed

scripts/builder.js

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const sass = require("sass");
1010
const glob = require("glob");
1111
const pjs = require("../package.json");
1212
const babel = require("@babel/core");
13+
const parse = require("@nativescript/core/css").parse;
1314

1415
// Kill The original folder, so that way it is a clean folder
1516
if (fs.existsSync("nativescript-theme-core")) {
@@ -18,39 +19,48 @@ if (fs.existsSync("nativescript-theme-core")) {
1819
fs.mkdirSync("nativescript-theme-core");
1920
fs.mkdirSync("nativescript-theme-core/css");
2021
fs.mkdirSync("nativescript-theme-core/scss");
22+
fs.mkdirSync("nativescript-theme-core/json");
2123

2224
const version = getVersion();
2325
const versionPlaceholder = "__VERSION__";
2426
console.log(`Building the Deployment files for v${version}...`);
2527

26-
// Create CSS from SCSS
27-
createCSSFromSCSS();
28+
async function createThemeFiles() {
2829

29-
// Copy the SCSS file to the build folder
30-
copySCSS();
30+
// Create CSS from SCSS
31+
await createCSSFromSCSS();
3132

32-
// Copy any Fonts
33-
//copyFonts();
33+
// Create JSON from CSS
34+
createJSONFromCSS();
3435

35-
createPackageJson();
36+
// Copy the SCSS file to the build folder
37+
copySCSS();
3638

37-
// Transform imports to commonjs
38-
const transform = babel.transform(fs.readFileSync("./src/index.js"), {
39-
plugins: ["@babel/transform-modules-commonjs"]
40-
});
39+
// Copy any Fonts
40+
//copyFonts();
4141

42-
fs.writeFile("./nativescript-theme-core/index.js", transform.code, {}, () => { });
42+
createPackageJson();
4343

44-
// Copy typings
45-
copyFile("./src/index.d.ts", "./nativescript-theme-core/index.d.ts");
44+
// Transform imports to commonjs
45+
const transform = babel.transform(fs.readFileSync("./src/index.js"), {
46+
plugins: ["@babel/transform-modules-commonjs"]
47+
});
48+
49+
fs.writeFile("./nativescript-theme-core/index.js", transform.code, {}, () => { });
50+
51+
// Copy typings
52+
copyFile("./src/index.d.ts", "./nativescript-theme-core/index.d.ts");
4653

47-
// Copy our Readme
48-
copyFile("./README.md", "./nativescript-theme-core/README.md");
49-
copyFile("./CHANGELOG.md", "./nativescript-theme-core/CHANGELOG.md");
50-
copyFile("./LICENSE", "./nativescript-theme-core/LICENSE");
54+
// Copy our Readme
55+
copyFile("./README.md", "./nativescript-theme-core/README.md");
56+
copyFile("./CHANGELOG.md", "./nativescript-theme-core/CHANGELOG.md");
57+
copyFile("./LICENSE", "./nativescript-theme-core/LICENSE");
5158

52-
console.log("Change to the 'nativescript-theme-core' folder and you can now do your `npm publish`");
53-
// TODO: We could Automatically run "npm publish"
59+
console.log("Change to the 'nativescript-theme-core' folder and you can now do your `npm publish`");
60+
// TODO: We could Automatically run "npm publish"
61+
}
62+
63+
createThemeFiles();
5464

5565
/**
5666
* Create package.json from the original one
@@ -161,7 +171,7 @@ function copySCSS() {
161171
/**
162172
* Create all the CSS from SCSS files
163173
*/
164-
function createCSSFromSCSS() {
174+
async function createCSSFromSCSS() {
165175

166176
const sassFilesPath = "./src/**/*.scss";
167177
const sassImportPaths = [
@@ -175,14 +185,35 @@ function createCSSFromSCSS() {
175185
return filename.indexOf("_") !== 0 && filename.indexOf("app.") !== 0 && filename.indexOf("customized.") !== 0 && filename.indexOf("bootstrap") !== 0 && filename.indexOf("kendo") !== 0;
176186
});
177187

188+
return Promise.all(sassFiles.map(sassFile => parseSass(sassFile, sassImportPaths)));
189+
}
178190

179-
for (let i = 0; i < sassFiles.length; i++) {
180-
// We only process open /core. files
181-
// if (sassFiles[i].indexOf("/core.") === -1) {
182-
// continue;
183-
// }
184-
parseSass(sassFiles[i], sassImportPaths);
185-
}
191+
/**
192+
* Create all the JSON from CSS files
193+
*/
194+
function createJSONFromCSS() {
195+
const cssFilesPath = "./nativescript-theme-core/css/**/*.css";
196+
const cssFiles = glob.sync(cssFilesPath)
197+
198+
const registerModules = [];
199+
registerModules.push(`require("@nativescript/core/globals/core");`)
200+
201+
cssFiles.forEach(cssFilePath =>{
202+
const cssFileContent = fs.readFileSync(cssFilePath, { encoding: "utf8" });
203+
const cssFileName = cssFilePath.substring(cssFilePath.lastIndexOf("/"));
204+
const jsonFileName = cssFileName.replace(".css", ".json");
205+
const jsonFilePath = `nativescript-theme-core/json${jsonFileName}`;
206+
207+
const ast = parse(cssFileContent, undefined);
208+
const jsonContent = JSON.stringify(ast, (k, v) => k === "position" ? undefined : v);
209+
fs.writeFileSync(jsonFilePath, jsonContent, "utf8");
210+
211+
212+
registerModules.push(`global.registerModule("@nativescript/theme/css${cssFileName}", () => { return require("@nativescript/theme/json${jsonFileName}")});`);
213+
});
214+
215+
const registerJsonJsContent = registerModules.join("\n");
216+
fs.writeFileSync("nativescript-theme-core/register-json.js", registerJsonJsContent, "utf8");
186217
}
187218

188219
/**
@@ -191,35 +222,39 @@ function createCSSFromSCSS() {
191222
* @param importPaths - Other import paths
192223
*/
193224
function parseSass(sassFile, importPaths) {
194-
const sassFileContent = fs.readFileSync(sassFile, { encoding: "utf8" });
195-
const offset = sassFile.lastIndexOf("/");
196-
const outputFile = `nativescript-theme-core/css${sassFile.substring(offset)}`;
197-
const cssFilePath = outputFile.replace(".scss", ".css");
198-
199-
// const output = sass.renderSync({
200-
sass.render({
201-
data: sassFileContent,
202-
includePaths: importPaths,
203-
outFile: cssFilePath,
204-
outputStyle: "compressed"
205-
}, (error, result) => {
206-
if (error) {
207-
console.log(error.status);
208-
console.log(error.column);
209-
console.log(error.message);
210-
console.log(error.line);
211-
} else {
212-
let css = result.css.toString();
213-
// correct version tag
214-
css = printVersion(css);
215-
// uncomment to debug builds
216-
// console.log(css);
217-
fs.writeFileSync(cssFilePath, css, "utf8");
218-
219-
// if build stats are ever desired
220-
// console.log(result.stats);
221-
}
222-
});
225+
return new Promise((resolve, reject) => {
226+
const sassFileContent = fs.readFileSync(sassFile, { encoding: "utf8" });
227+
const offset = sassFile.lastIndexOf("/");
228+
const outputFile = `nativescript-theme-core/css${sassFile.substring(offset)}`;
229+
const cssFilePath = outputFile.replace(".scss", ".css");
230+
231+
// const output = sass.renderSync({
232+
sass.render({
233+
data: sassFileContent,
234+
includePaths: importPaths,
235+
outFile: cssFilePath,
236+
outputStyle: "compressed"
237+
}, (error, result) => {
238+
if (error) {
239+
console.log(error.status);
240+
console.log(error.column);
241+
console.log(error.message);
242+
console.log(error.line);
243+
reject(error);
244+
} else {
245+
let css = result.css.toString();
246+
// correct version tag
247+
css = printVersion(css);
248+
// uncomment to debug builds
249+
// console.log(css);
250+
fs.writeFileSync(cssFilePath, css, "utf8");
251+
252+
// if build stats are ever desired
253+
// console.log(result.stats);
254+
resolve();
255+
}
256+
});
257+
})
223258
}
224259

225260
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)