minify.js 592 B

1234567891011121314151617181920212223242526272829303132333435
  1. const uglify = require( 'uglify-js' );
  2. const DEFAULTS = {
  3. minify: {
  4. sourceMap: true,
  5. output: {
  6. comments: /^!/,
  7. },
  8. toplevel: true,
  9. mangle: {
  10. properties: {
  11. regex: /^_/,
  12. },
  13. },
  14. },
  15. };
  16. function minify( pluginOptions = {} ) {
  17. pluginOptions = { ...DEFAULTS, ...pluginOptions };
  18. return {
  19. name: 'minify',
  20. renderChunk( code ) {
  21. const result = uglify.minify( code, pluginOptions.minify );
  22. if ( result.error ) {
  23. throw new Error( result.error );
  24. }
  25. return result;
  26. },
  27. };
  28. }
  29. exports.minify = minify;