-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): refactor gulpfile to use gulp-execa (#323)
* feat(build): refactor gulpfile to use gulp-execa * chore: fix PR comments * chore(deps): update deps
- Loading branch information
Showing
3 changed files
with
1,984 additions
and
2,027 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.