Last active
March 3, 2019 14:37
-
-
Save nweldev/a16232bc3723f54197f9a1e4da3bbe94 to your computer and use it in GitHub Desktop.
LitElement Widget - minification
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
const fs = require('fs'); | |
const glob = require('glob'); | |
const terser = require('terser'); | |
// use glob in order to list all files we want to minify | |
const files = glob.sync('./lib/**.js'); | |
files.forEach(filePath => { | |
// get the unminified script content | |
let data = fs.readFileSync(filePath, 'utf-8'); | |
/* replace import specifiers from unminified files | |
* paths to their minified versions | |
*/ | |
data = data | |
.replace( | |
/from [',"]\.\/lit-element\.js[',"]/g, | |
`from './lit-element.min.js'` | |
) | |
.replace( | |
/from [',"]\.\/lit-html\.js[',"]/g, | |
`from './lit-html.min.js'` | |
); | |
fs.writeFileSync( | |
// add the .min.js extension | |
filePath.replace(/\.js$/, '.min.js'), | |
// minify using Terser API | |
terser.minify(data, { | |
// mandatory as we are minifying ES Modules here | |
module: true, | |
compress: { | |
// compress twice for further compressed code | |
passes: 2 | |
} | |
}).code | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment