Skip to content

Commit

Permalink
feat(ghp-postbuild.js): update regular expressions to handle single a…
Browse files Browse the repository at this point in the history
…nd double quote asset urls
  • Loading branch information
ymekuria committed Aug 17, 2023
1 parent e4ef5c7 commit 01d0684
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions examples/zkapps/04-zkapp-browser-ui/ui/ghp-postbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,23 @@ const path = require('path');

// This script modifies the built CSS files and prepends the repo-name to the asset URLs.
// to be compatible with github pages deployment.
const cssDir = path.join(__dirname, '/.next/static/css');
// Add your repository name here.
const cssDir = path.join(__dirname, '/out/_next/static/css');
// Update your repository name here if it is different from the project name.
let repoURL = '04-zkapp-browser-ui';
fs.readdir(cssDir, (err, files) => {
if (err) {
console.error(err);
process.exit(1);
}
const files = fs.readdirSync(cssDir);

files.forEach((file) => {
if (path.extname(file) === '.css') {
const filePath = path.join(cssDir, file);

const data = fs.readFileSync(filePath, 'utf8');

files.forEach(file => {
if (path.extname(file) === '.css') {
const filePath = path.join(cssDir, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
process.exit(1);
}

const result = data.replace(/url\(\//g, `url(/${repoURL}/`);
const singleQuoteRegex = new RegExp(`url\\(\\s*'\\/(?!${repoURL})`, 'g');
const doubleQuoteRegex = new RegExp(`url\\(\\s*"\\/(?!${repoURL})`, 'g');

fs.writeFile(filePath, result, 'utf8', err => {
if (err) {
console.error(err);
process.exit(1);
}
});
});
}
});
});
let result = data.replace(singleQuoteRegex, `url('/${repoURL}/`);
result = result.replace(doubleQuoteRegex, `url("/${repoURL}/`);

fs.writeFileSync(filePath, result, 'utf8');
}
});

0 comments on commit 01d0684

Please sign in to comment.