Skip to content

Commit

Permalink
feat(build): refactor gulpfile to use gulp-execa (#323)
Browse files Browse the repository at this point in the history
* feat(build): refactor gulpfile to use gulp-execa

* chore: fix PR comments

* chore(deps): update deps
  • Loading branch information
doprz authored Nov 11, 2024
1 parent e425daf commit db04bbb
Show file tree
Hide file tree
Showing 3 changed files with 1,984 additions and 2,027 deletions.
105 changes: 75 additions & 30 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,93 @@
// (Thanks go to https://github.com/pnd280/complexity/blob/alpha/gulpfile.js)

import cp from 'child_process';
import chalk from 'chalk';
import fs from 'fs';
import gulp from 'gulp';
import gulpZip from 'gulp-zip';
import { createRequire } from 'module';
import { dest, series, src } from 'gulp';
import { exec } from 'gulp-execa';
import zip from 'gulp-zip';
import path from 'path';

const DIST_DIR = 'dist';
const PACKAGE_DIR = 'package';
const DATABASE_DIR = path.join(DIST_DIR, 'database');

// Custom log functions
const log = message => console.log(chalk.blue(`[${new Date().toTimeString().split(' ')[0]}]`), chalk.white(message));
const logWarn = message =>
console.warn(
chalk.blue(`[${new Date().toTimeString().split(' ')[0]}]`),
chalk.yellow(' [WARN]'),
chalk.white(message)
);
const logError = message =>
console.error(
chalk.blue(`[${new Date().toTimeString().split(' ')[0]}]`),
chalk.red(' [ERROR]'),
chalk.white(message)
);

// Remove extra database folder
function removeExtraDatabaseDir(cb) {
fs.rmSync(DATABASE_DIR, { recursive: true, force: true });
log('Extra database directory removed.');

cb();
}

// Instrument with Sentry
// Make sure sentry is configured https://docs.sentry.io/platforms/javascript/sourcemaps/uploading/typescript/#2-configure-sentry-cli
function instrumentWithSentry() {
return cp.exec('sentry-cli sourcemaps inject dist/ && sentry-cli sourcemaps upload dist/');
async function instrumentWithSentry(cb) {
await exec(`sentry-cli sourcemaps inject ${DIST_DIR}`);
await exec(`sentry-cli sourcemaps upload ${DIST_DIR}`);
log('Sentry instrumentation completed.');

cb();
}

// Zip the dist folder
function zipDist() {
const require = createRequire(import.meta.url);
const manifest = require('./package.json');
const zipFileName = `${manifest.name.replaceAll(' ', '-')}-${manifest.version}.zip`;

return gulp
.src('dist/**', {
encoding: false,
})
.pipe(gulpZip(zipFileName))
.pipe(gulp.dest('package'));
}
const packageInfo = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const zipFileName = `${packageInfo.name.replace(/ /g, '-')}-${packageInfo.version}.zip`;

const zip = gulp.series(instrumentWithSentry, zipDist);
return src(`${DIST_DIR}/**`, {
base: DIST_DIR,
encoding: false, // Disable encoding to handle binary files correctly
})
.pipe(zip(zipFileName))
.pipe(dest(PACKAGE_DIR))
.on('end', () => log(`Zip file created: ${path.join(PACKAGE_DIR, zipFileName)}`));
}

// Temp fix for CSP on Chrome 130
// Manually remove them because there is no option to disable use_dynamic_url on @crxjs/vite-plugin
function forceDisableUseDynamicUrl(done) {
const require = createRequire(import.meta.url);
const manifest = require('./dist/manifest.json');
// Force disable use_dynamic_url in manifest.json
function forceDisableUseDynamicUrl(cb) {
const manifestPath = path.join(DIST_DIR, 'manifest.json');

if (!fs.existsSync(manifestPath)) {
logWarn('manifest.json not found. Skipping modification.');
return cb();
}

const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
let modified = false;

manifest.web_accessible_resources.forEach(resource => {
delete resource.use_dynamic_url;
if (resource.use_dynamic_url) {
delete resource.use_dynamic_url;
modified = true;
}
});

if (!fs.existsSync('./dist/manifest.json')) {
return done();
if (modified) {
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
log('use_dynamic_url removed from manifest.json');
} else {
log('No use_dynamic_url found in manifest.json. No changes made.');
}

fs.writeFileSync('./dist/manifest.json', JSON.stringify(manifest, null, 2));

done();
cb();
}

export { forceDisableUseDynamicUrl, zip };
// Main build task
const zipProdBuild = series(removeExtraDatabaseDir, instrumentWithSentry, zipDist);

export { forceDisableUseDynamicUrl, zipProdBuild };
80 changes: 41 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev": "vite",
"build": "tsc && vite build",
"build:watch": "NODE_ENV='development' vite build --mode development -w",
"zip": "PROD=true pnpm build && pnpm gulp zip",
"zip": "PROD=true pnpm build && pnpm gulp zipProdBuild",
"prettier": "prettier src --check",
"prettier:fix": "prettier src --write",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives",
Expand All @@ -27,12 +27,12 @@
"prepare": "husky"
},
"dependencies": {
"@headlessui/react": "^2.1.10",
"@headlessui/react": "^2.2.0",
"@hello-pangea/dnd": "^17.0.0",
"@octokit/rest": "^21.0.2",
"@sentry/react": "^8.34.0",
"@unocss/vite": "^0.63.4",
"@vitejs/plugin-react": "^4.3.2",
"@sentry/react": "^8.36.0",
"@unocss/vite": "^0.63.6",
"@vitejs/plugin-react": "^4.3.3",
"chrome-extension-toolkit": "^0.0.54",
"clsx": "^2.1.1",
"conventional-changelog": "^6.0.0",
Expand All @@ -41,14 +41,14 @@
"html-to-image": "^1.11.11",
"husky": "^9.1.6",
"kc-dabr-wasm": "^0.1.2",
"nanoid": "^5.0.7",
"nanoid": "^5.0.8",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-loading-skeleton": "^3.5.0",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"react-syntax-highlighter": "^15.6.1",
"remark-gfm": "^4.0.0",
"sass": "^1.79.5",
"sass": "^1.80.6",
"simple-git": "^3.27.0",
"sql.js": "1.11.0"
},
Expand All @@ -59,48 +59,49 @@
"@commitlint/types": "^19.5.0",
"@crxjs/vite-plugin": "2.0.0-beta.21",
"@iconify-json/bi": "^1.2.1",
"@iconify-json/iconoir": "^1.2.2",
"@iconify-json/material-symbols": "^1.2.4",
"@iconify-json/ri": "^1.2.1",
"@iconify-json/iconoir": "^1.2.3",
"@iconify-json/material-symbols": "^1.2.6",
"@iconify-json/ri": "^1.2.3",
"@semantic-release/exec": "^6.0.3",
"@sentry/types": "^8.34.0",
"@storybook/addon-designs": "^8.0.3",
"@storybook/addon-essentials": "^8.3.5",
"@storybook/addon-links": "^8.3.5",
"@storybook/blocks": "^8.3.5",
"@storybook/react": "^8.3.5",
"@storybook/react-vite": "^8.3.5",
"@storybook/test": "^8.3.5",
"@sentry/types": "^8.36.0",
"@storybook/addon-designs": "^8.0.4",
"@storybook/addon-essentials": "^8.4.1",
"@storybook/addon-links": "^8.4.1",
"@storybook/blocks": "^8.4.1",
"@storybook/react": "^8.4.1",
"@storybook/react-vite": "^8.4.1",
"@storybook/test": "^8.4.1",
"@svgr/core": "^8.1.0",
"@svgr/plugin-jsx": "^8.1.0",
"@types/chrome": "^0.0.273",
"@types/conventional-changelog": "^3.1.5",
"@types/gulp": "^4.0.17",
"@types/gulp-zip": "^4.0.4",
"@types/node": "^22.7.5",
"@types/node": "^22.8.7",
"@types/prompts": "^2.4.9",
"@types/react": "^18.3.11",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@types/react-syntax-highlighter": "^15.5.13",
"@types/semantic-release": "^20.0.6",
"@types/semver": "^7.5.8",
"@types/sql.js": "^1.4.9",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"@unocss/eslint-config": "^0.63.4",
"@unocss/postcss": "^0.63.4",
"@unocss/preset-uno": "^0.63.4",
"@unocss/preset-web-fonts": "^0.63.4",
"@unocss/reset": "^0.63.4",
"@unocss/transformer-directives": "^0.63.4",
"@unocss/transformer-variant-group": "^0.63.4",
"@unocss/eslint-config": "^0.63.6",
"@unocss/postcss": "^0.63.6",
"@unocss/preset-uno": "^0.63.6",
"@unocss/preset-web-fonts": "^0.63.6",
"@unocss/reset": "^0.63.6",
"@unocss/transformer-directives": "^0.63.6",
"@unocss/transformer-variant-group": "^0.63.6",
"@vitejs/plugin-react-swc": "^3.7.1",
"@vitest/coverage-v8": "^2.1.2",
"@vitest/ui": "^2.1.2",
"@vitest/coverage-v8": "^2.1.4",
"@vitest/ui": "^2.1.4",
"chalk": "^5.3.0",
"chromatic": "^11.12.5",
"chromatic": "^11.16.5",
"cssnano": "^7.0.6",
"cssnano-preset-advanced": "^7.0.6",
"del": "^8.0.0",
"dotenv": "^16.4.5",
"es-module-lexer": "^1.5.4",
"eslint": "^8.57.1",
Expand All @@ -111,29 +112,30 @@
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-import-essentials": "^0.2.1",
"eslint-plugin-jsdoc": "^50.3.2",
"eslint-plugin-jsdoc": "^50.4.3",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react": "^7.37.1",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-prefer-function-component": "^3.3.0",
"eslint-plugin-react-refresh": "^0.4.12",
"eslint-plugin-react-refresh": "^0.4.14",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-storybook": "^0.9.0",
"gulp": "^5.0.0",
"gulp-execa": "^7.0.1",
"gulp-zip": "^6.0.0",
"path": "^0.12.7",
"postcss": "^8.4.47",
"prettier": "^3.3.3",
"react-dev-utils": "^12.0.1",
"semantic-release": "^24.1.2",
"storybook": "^8.3.5",
"semantic-release": "^24.2.0",
"storybook": "^8.4.1",
"typescript": "^5.6.3",
"unocss": "^0.63.4",
"unocss": "^0.63.6",
"unocss-preset-primitives": "0.0.2-beta.1",
"unplugin-icons": "^0.19.3",
"vite": "^5.4.8",
"vite": "^5.4.10",
"vite-plugin-inspect": "^0.8.7",
"vitest": "^2.1.2"
"vitest": "^2.1.4"
},
"pnpm": {
"patchedDependencies": {
Expand Down
Loading

0 comments on commit db04bbb

Please sign in to comment.