Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗 Remove almost all gulp streaming from the compilation pipeline #32903

Merged
merged 6 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 64 additions & 69 deletions build-system/tasks/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,21 @@
*/

const debounce = require('debounce');
const file = require('gulp-file');
const fs = require('fs-extra');
const gulp = require('gulp');
const {
endBuildStep,
mkdirSync,
toPromise,
watchDebounceDelay,
} = require('../helpers');
const globby = require('globby');
const path = require('path');
const {buildExtensions} = require('../extension-helpers');
const {endBuildStep, watchDebounceDelay} = require('../helpers');
const {jsifyCssAsync} = require('./jsify-css');
const {maybeUpdatePackages} = require('../update-packages');
const {watch} = require('gulp');
const {watch} = require('chokidar');

/**
* Entry point for 'gulp css'
* @return {!Promise}
rsimha marked this conversation as resolved.
Show resolved Hide resolved
*/
async function css() {
maybeUpdatePackages();
return compileCss();
await compileCss();
}

const cssEntryPoints = [
Expand Down Expand Up @@ -84,80 +78,81 @@ const cssEntryPoints = [
},
];

/**
* Copies the css from the build folder to the dist folder
*/
async function copyCss() {
const startTime = Date.now();
await fs.ensureDir('dist/v0');
for (const {outCss} of cssEntryPoints) {
await fs.copy(`build/css/${outCss}`, `dist/${outCss}`);
}
rsimha marked this conversation as resolved.
Show resolved Hide resolved
const cssFiles = globby.sync('build/css/amp-*.css');
await Promise.all(
cssFiles.map((cssFile) => {
return fs.copy(cssFile, `dist/v0/${path.basename(cssFile)}`);
})
);
endBuildStep('Copied', 'build/css/*.css to dist/v0/*.css', startTime);
}

/**
* Writes CSS to build folder
*
* @param {string} css
* @param {string} jsFilename
* @param {string} cssFilename
* @param {boolean} append append CSS to existing file
*/
async function writeCss(css, jsFilename, cssFilename, append) {
await fs.ensureDir('build/css');
const jsContent = 'export const cssText = ' + JSON.stringify(css);
await fs.writeFile(`build/${jsFilename}`, jsContent);
if (append) {
await fs.appendFile(`build/css/${cssFilename}`, css);
} else {
await fs.writeFile(`build/css/${cssFilename}`, css);
}
}

/**
* @param {string} path
* @param {string} outJs
* @param {string} outCss
* @param {boolean} append
*/
async function writeCssEntryPoint(path, outJs, outCss, append) {
const css = await jsifyCssAsync(`css/${path}`);
await writeCss(css, outJs, outCss, append);
}

/**
* Compile all the css and drop in the build folder
*
* @param {Object=} options
* @return {!Promise}
*/
function compileCss(options = {}) {
async function compileCss(options = {}) {
if (options.watch) {
const watchFunc = () => {
compileCss();
};
watch('css/**/*.css').on('change', debounce(watchFunc, watchDebounceDelay));
}

/**
* Writes CSS to build folder
*
* @param {string} css
* @param {string} jsFilename
* @param {string} cssFilename
* @param {boolean} append append CSS to existing file
* @return {Promise}
*/
function writeCss(css, jsFilename, cssFilename, append) {
return toPromise(
file(jsFilename, 'export const cssText = ' + JSON.stringify(css), {
src: true,
})
.pipe(gulp.dest('build'))
.on('end', function () {
mkdirSync('build');
mkdirSync('build/css');
if (append) {
fs.appendFileSync(`build/css/${cssFilename}`, css);
} else {
fs.writeFileSync(`build/css/${cssFilename}`, css);
}
})
);
}

/**
* @param {string} path
* @param {string} outJs
* @param {string} outCss
* @param {boolean} append
* @return {!Promise}
*/
function writeCssEntryPoint(path, outJs, outCss, append) {
return jsifyCssAsync(`css/${path}`).then((css) =>
writeCss(css, outJs, outCss, append)
watch('css/**/*.css').on(
'change',
debounce(compileCss, watchDebounceDelay)
);
}

const startTime = Date.now();

let promise = Promise.resolve();

cssEntryPoints.forEach(({path, outJs, outCss, append}) => {
promise = promise.then(() =>
writeCssEntryPoint(path, outJs, outCss, append)
);
});

return promise
.then(() => buildExtensions({compileOnlyCss: true}))
.then(() => {
endBuildStep('Recompiled all CSS files into', 'build/', startTime);
});
// Must be in order because some iterations write while others append.
for (const {path, outJs, outCss, append} of cssEntryPoints) {
await writeCssEntryPoint(path, outJs, outCss, append);
}
await buildExtensions({compileOnlyCss: true});
endBuildStep('Recompiled all CSS files into', 'build/', startTime);
}

module.exports = {
css,
compileCss,
copyCss,
cssEntryPoints,
};

Expand Down