chore(css): add banners to DocSearch CSS bundles
This commit is contained in:
parent
6ff89319b8
commit
66649ad834
2 changed files with 91 additions and 1 deletions
90
build-css.js
Normal file
90
build-css.js
Normal file
|
|
@ -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();
|
||||
|
|
@ -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/\""
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue