Skip to content

Instantly share code, notes, and snippets.

@bfocht
Last active March 2, 2019 14:19
Show Gist options
  • Save bfocht/a0def4432d0f93dc28bc7cf64ebe8be1 to your computer and use it in GitHub Desktop.
Save bfocht/a0def4432d0f93dc28bc7cf64ebe8be1 to your computer and use it in GitHub Desktop.
post build script for create-react-app to move the js to a dist folder
const fs = require('fs');
const file = require('./build/asset-manifest.json')['main.js'];
const src = './build/';
const dest = './dist/';
if (!fs.existsSync(dest)){
fs.mkdirSync(dest);
}
if (fs.existsSync(dest+'index.js')){
fs.unlinkSync(dest+'index.js');
}
// remove source maps from production and move file to dist folder
fs.readFile(src+file, 'utf8', (err, data) => {
if (err) {
console.log('Unable to read file from manifest.');
process.exit(1);
}
let version = `/*!\n* domain-search v${process.env.npm_package_version}\n* Licensed under MIT\n* \n*/\n`;
let result = data.split('//# sourceMappingURL');
if (result[result.length - 1] !== undefined && result.length > 1) {
fs.writeFileSync(dest + 'index.js', version);
fs.appendFileSync(dest + 'index.js', result.slice(0, result.length - 1));
}
});
@abhiphirke
Copy link

Can you please also add little instructions on how can this be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment