chmod +x strip-js-comments.mjs
npm install --save @babel/generator
npm install --save @babel/parser
./strip-js-comments.mjs assets/js/script.js
Created
August 6, 2023 12:16
-
-
Save mhitza/ddc8326e1c94094bdab762e6171e459a to your computer and use it in GitHub Desktop.
Script using babel to strip all comments from JavaScript files
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
#!/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