build-css.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const sass = require( 'sass' );
  2. const fs = require( 'fs' ).promises;
  3. const path = require( 'path' );
  4. const postcss = require( 'postcss' );
  5. const cssnano = require( 'cssnano' );
  6. const autoprefixer = require( 'autoprefixer' );
  7. const name = 'splide';
  8. const files = [
  9. './src/css/core/index.scss',
  10. './src/css/themes/default/index.scss',
  11. './src/css/themes/sea-green/index.scss',
  12. './src/css/themes/skyblue/index.scss',
  13. ];
  14. function buildCss( file ) {
  15. const result = sass.renderSync( { file, outputStyle: 'compressed' } );
  16. const outFile = rename( file );
  17. return postcss( [
  18. cssnano( { reduceIdents: false } ),
  19. autoprefixer(),
  20. ] )
  21. .process( result.css, { from: undefined } )
  22. .then( result => {
  23. result.warnings().forEach( warn => {
  24. console.warn( warn.toString() );
  25. } );
  26. return fs.writeFile( outFile, result.css ).then( () => result );
  27. } )
  28. .then( result => {
  29. if ( outFile.includes( 'splide-default' ) ) {
  30. const dir = path.dirname( outFile ).split( '/' ).slice( 0, -1 ).join( '/' );
  31. return fs.writeFile( `${ dir }/${ name }.min.css`, result.css ).then( () => result );
  32. }
  33. } );
  34. }
  35. function rename( file ) {
  36. file = file.replace( './src/', '' );
  37. const fragments = path.dirname( file ).split( '/' );
  38. const dirname = fragments.slice( 0, -1 ).join( '/' );
  39. return `./dist/${ dirname }/${ name }-${ fragments[ fragments.length - 1 ] }.min.css`;
  40. }
  41. Promise.all( files.map( buildCss ) ).catch( e => console.error( e ) );
  42. exports.buildCss = () => {
  43. files.forEach( buildCss );
  44. };