Skip to content

Instantly share code, notes, and snippets.

@mhitza
Created August 6, 2023 12:16
Show Gist options
  • Save mhitza/ddc8326e1c94094bdab762e6171e459a to your computer and use it in GitHub Desktop.
Save mhitza/ddc8326e1c94094bdab762e6171e459a to your computer and use it in GitHub Desktop.
Script using babel to strip all comments from JavaScript files
chmod +x strip-js-comments.mjs
npm install --save @babel/generator
npm install --save @babel/parser
./strip-js-comments.mjs assets/js/script.js
#!/usr/bin/env node
import { parse } from "@babel/parser";
import { CodeGenerator } from "@babel/generator";
import { readFileSync, writeFileSync } from 'fs';
const code = readFileSync(process.argv[2]).toString();
const ast = parse(code);
const generator = new CodeGenerator(ast, {comments: false}, code);
writeFileSync(process.argv[2], generator.generate().code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment