Skip to content

Commit

Permalink
Fast build command without WebPack (#1008)
Browse files Browse the repository at this point in the history
* Fast build command without WebPack

* Use `build:pages` instead

Co-authored-by: Queen Vinyl Da.i'gyu-Kazotetsu <vinyldarkscratch@gmail.com>

Co-authored-by: Queen Vinyl Da.i'gyu-Kazotetsu <vinyldarkscratch@gmail.com>
  • Loading branch information
NiedziolkaMichal and queengooborg authored Jan 19, 2023
1 parent 6af5eff commit e421916
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ function getConfig() {
sync: true,
});

return cosmiConfig.load(configFile).config;
const config = cosmiConfig.load(configFile).config;
const progressArguments = getProgressArguments();
return {
...config,
...progressArguments,
};
}

function getProgressArguments() {
const argv = process.argv;
return {
doWebPack: !argv.includes("--skip-webpack"),
};
}

export default getConfig;
40 changes: 36 additions & 4 deletions lib/mdn-bob.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import getConfig from "./config.js";
import * as pageBuilder from "./pageBuilder.js";
import * as utils from "./utils.js";

/**
* Making sure base output directory is created and empty
*
* In case Web Pack process is skipped, its output files are not deleted
*/
function cleanBaseDir(config) {
if (config.doWebPack) {
fse.emptyDirSync(config.baseDir);
} else {
fse.ensureDirSync(config.baseDir);

const baseFileNames = fse.readdirSync(config.baseDir);
const baseFilePaths = baseFileNames.map((n) => config.baseDir + n);
const notWebPackPaths = baseFilePaths.filter(
(p) => !pathCreatedByWebPack(config, p)
);
notWebPackPaths.forEach((filePath) => fse.removeSync(filePath));
}
}

function pathCreatedByWebPack(config, path) {
return (
utils.isSamePath(config.destCssDir, path) ||
utils.isSamePath(config.destJsDir, path)
);
}

function doWebpack() {
return new Promise((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
Expand Down Expand Up @@ -39,14 +66,19 @@ async function init() {
const config = getConfig();

console.info(`MDN-BOB: Cleaning or creating ${config.baseDir}....`);
// empty or create `config.baseDir`
await fse.emptyDir(config.baseDir);
cleanBaseDir(config);

console.info("MDN-BOB: Copying static assets....");
utils.copyStaticAssets();

console.info("MDN-BOB: Running Webpack...");
await doWebpack();
if (config.doWebPack) {
console.info("MDN-BOB: Running Webpack...");
await doWebpack();
} else {
console.warn(
"MDN-BOB: Skipped compilation of base JS & CSS shared by all examples"
);
}

console.info("MDN-BOB: Building pages....");
console.log(await pageBuilder.buildPages());
Expand Down
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ export function copyStaticAssets() {
// copy fonts
copyDirectory(config.examplesFontsRoot, config.fontsMediaDest);
}

/**
* Compares both arguments after trimming and removing end slash
*/
export function isSamePath(p1, p2) {
const removeEndSlash = (path) => {
if (path.endsWith("/")) {
return path.substring(0, path.length - 1);
}
return path;
};

const normalize = (path) => removeEndSlash(path.trim());

return normalize(p1) == normalize(p2);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
],
"scripts": {
"build": "node ./lib/mdn-bob.js",
"build:pages": "node ./lib/mdn-bob.js --skip-webpack",
"start": "npm-run-all build start-server",
"start-server": "http-server -p 4444 ./docs",
"format": "prettier --ignore-unknown --check \"**/*\"",
Expand Down

0 comments on commit e421916

Please sign in to comment.