build-script.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const rollup = require( 'rollup' ).rollup;
  2. const esbuild = require( 'rollup-plugin-esbuild' ).default;
  3. const babel = require( '@rollup/plugin-babel' );
  4. const resolve = require( '@rollup/plugin-node-resolve' ).nodeResolve;
  5. const path = require( 'path' );
  6. const minify = require( './plugins/minify' ).minify;
  7. const banner = require( './constants/banner' );
  8. const fs = require( 'fs' ).promises;
  9. const zlib = require( 'zlib' );
  10. const name = 'splide';
  11. async function buildScript( compress, type = 'default' ) {
  12. const file = `./dist/js/${ name }${ type !== 'default' ? `-${ type }` : '' }${ compress ? '.min' : '' }.js`;
  13. const bundle = await rollup( {
  14. input: `./src/js/build/${ type }.ts`,
  15. plugins: [
  16. resolve(),
  17. esbuild( { minify: false } ),
  18. babel.getBabelOutputPlugin( {
  19. configFile: path.resolve( __dirname, '../.babelrc' ),
  20. allowAllFormats: true,
  21. } ),
  22. compress ? minify() : false,
  23. ],
  24. } );
  25. await bundle.write( {
  26. banner,
  27. file,
  28. format : 'umd',
  29. name : type === 'default' ? 'Splide' : 'SplideRenderer',
  30. sourcemap: compress,
  31. } );
  32. if ( compress && type === 'default' ) {
  33. await fs.readFile( file ).then( content => {
  34. return new Promise( ( resolve, reject ) => {
  35. zlib.gzip( content, ( err, binary ) => {
  36. if ( err ) {
  37. return reject( err );
  38. }
  39. fs.writeFile( `${ file }.gz`, binary ).then( resolve, reject );
  40. } );
  41. } );
  42. } );
  43. }
  44. }
  45. Promise.all( [
  46. buildScript(),
  47. buildScript( true ),
  48. buildScript( true, 'renderer' ),
  49. ] ).catch( console.error );
  50. exports.buildJs = () => buildScript();
  51. exports.buildMin = () => buildScript( true );
  52. exports.buildRenderer = () => buildScript( true, 'renderer' );