From 66649ad834452a5732dfd49412e72e58b3eb260e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Wed, 26 Aug 2020 12:01:30 +0200 Subject: [PATCH] chore(css): add banners to DocSearch CSS bundles --- build-css.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 build-css.js diff --git a/build-css.js b/build-css.js new file mode 100644 index 00000000..c56e1e85 --- /dev/null +++ b/build-css.js @@ -0,0 +1,90 @@ +/* eslint-disable import/no-commonjs */ + +const fs = require('fs'); +const util = require('util'); +const { execSync } = require('child_process'); + +const cssnano = require('cssnano'); +const postcss = require('postcss'); + +const pkg = require('./package.json'); + +const readFile = util.promisify(fs.readFile); + +if (process.env.NODE_ENV === 'production' && !process.env.VERSION) { + throw new Error( + `You need to specify a valid semver environment variable 'VERSION' to run the build process (received: ${JSON.stringify( + process.env.VERSION + )}).` + ); +} + +function getBundleBanner(pkg) { + const lastCommitHash = execSync('git rev-parse --short HEAD') + .toString() + .trim(); + const version = + process.env.NODE_ENV === 'production' + ? process.env.VERSION + : `${pkg.version} (UNRELEASED ${lastCommitHash})`; + const authors = '© Algolia, Inc. and contributors'; + + return `/*! ${pkg.name} ${version} | MIT License | ${authors} | ${pkg.homepage} */`; +} + +function build({ input, output, banner }) { + fs.readFile(input, (error, css) => { + if (error) { + throw error; + } + + postcss([cssnano]) + .process(css, { from: input, to: output }) + .then((result) => { + fs.writeFile(output, [banner, result.css].join('\n'), () => true); + }); + }); +} + +build({ + input: 'src/_variables.css', + output: 'dist/_variables.css', + banner: getBundleBanner({ ...pkg, name: `${pkg.name} Variables` }), +}); +build({ + input: 'src/button.css', + output: 'dist/button.css', + banner: getBundleBanner({ ...pkg, name: `${pkg.name} Button` }), +}); +build({ + input: 'src/modal.css', + output: 'dist/modal.css', + banner: getBundleBanner({ ...pkg, name: `${pkg.name} Modal` }), +}); + +async function buildStyle() { + const variablesCss = await readFile('src/_variables.css'); + const buttonCss = await readFile('src/button.css'); + const modalCss = await readFile('src/modal.css'); + + const variablesOutput = await postcss([cssnano]).process(variablesCss, { + from: undefined, + }); + const buttonOutput = await postcss([cssnano]).process(buttonCss, { + from: undefined, + }); + const modalOutput = await postcss([cssnano]).process(modalCss, { + from: undefined, + }); + + fs.writeFile( + 'dist/style.css', + [ + getBundleBanner(pkg), + [variablesOutput.css, buttonOutput.css, modalOutput.css].join(''), + ].join('\n'), + () => true + ); +} + +buildStyle(); diff --git a/package.json b/package.json index cca1c3d2..a04d4636 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "jsdelivr": "dist/style.css", "scripts": { "build": "yarn build:clean && mkdir dist && yarn build:css", - "build:css": "cp src/_variables.css dist/ && cp src/modal.css dist/ && cp src/button.css dist/ && cat src/*.css > dist/style.css", + "build:css": "node build-css.js", "build:clean": "rm -rf ./dist", "watch": "watch \"yarn build:css\" --ignoreDirectoryPattern \"/dist/\"" }