@@ -4,92 +4,64 @@ import { globby } from 'globby';
44import * as url from 'url' ;
55import chalk from 'chalk' ;
66import elapsed from 'elapsed-time-logger' ;
7- import less from './utils/less.js' ;
87import autoprefixer from './utils/autoprefixer.js' ;
9- import minifyCSS from './utils/clean -css.js' ;
8+ import minifyCSS from './utils/minify -css.js' ;
109import { banner } from './utils/banner.js' ;
1110import config from './build-config.js' ;
1211import { outputDir } from './utils/output-dir.js' ;
1312import isProd from './utils/isProd.js' ;
1413import { getSplittedCSS , proceedReplacements } from './utils/get-element-styles.js' ;
14+ import unwrapCss from './utils/unwrap-css.js' ;
1515
1616const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
1717
18- const readSwiperFile = async ( filePath ) => {
19- const fileContent = await fs . readFile ( filePath , 'utf-8' ) ;
20- if ( filePath . includes ( 'swiper.less' ) ) {
21- const coreContent = fs . readFileSync ( path . resolve ( __dirname , '../src/core/core.less' ) , 'utf-8' ) ;
22- return fileContent
23- . replace ( '//IMPORT_MODULES' , '' )
24- . replace ( `@import url('./less/mixins.less');` , '' )
25- . replace ( `@import url('./core/core.less');` , coreContent ) ;
26- }
27- if ( filePath . includes ( 'swiper-vars.less' ) ) {
28- return fileContent ;
29- }
30- if ( filePath . includes ( 'navigation.less' ) || filePath . includes ( 'pagination.less' ) ) {
31- return [ "@import url('../../swiper-vars.less');" , fileContent ] . join ( '\n\n' ) ;
32- }
33- if ( filePath . includes ( 'swiper.scss' ) ) {
34- const coreContent = await fs . readFile (
35- path . resolve ( __dirname , '../src/core/core.scss' ) ,
36- 'utf-8' ,
18+ const buildCSS = async ( { isBundle, modules, minified } ) => {
19+ const coreContent = await fs . readFile ( path . resolve ( __dirname , '../src/swiper.css' ) , 'utf8' ) ;
20+ const modulesContent = [ ] ;
21+ for ( const mod of modules ) {
22+ modulesContent . push (
23+ // eslint-disable-next-line no-await-in-loop
24+ await fs . readFile ( path . resolve ( __dirname , `../src/modules/${ mod } /${ mod } .css` ) , 'utf8' ) ,
3725 ) ;
38- return fileContent
39- . replace ( `@import './core/core';` , coreContent )
40- . replace ( '//IMPORT_MODULES' , '' ) ;
4126 }
42- return fileContent ;
43- } ;
44- const buildCSS = async ( { isBundle, modules, minified } ) => {
45- let lessContent = await fs . readFile ( path . resolve ( __dirname , '../src/swiper.less' ) , 'utf8' ) ;
46- lessContent = lessContent . replace (
47- '//IMPORT_MODULES' ,
48- ! isBundle
49- ? ''
50- : modules . map ( ( mod ) => `@import url('./modules/${ mod } /${ mod } .less');` ) . join ( '\n' ) ,
51- ) ;
5227
53- const cssContent = await autoprefixer (
54- await less ( lessContent , path . resolve ( __dirname , '../src' ) ) ,
55- ) . catch ( ( err ) => {
56- throw err ;
57- } ) ;
28+ const swiperCSS = await autoprefixer ( coreContent ) ;
29+ const swiperBundleCSS = await autoprefixer ( [ coreContent , ...modulesContent ] . join ( '\n' ) ) ;
30+
5831 const fileName = isBundle ? 'swiper-bundle' : 'swiper' ;
5932 // Write file
6033 await fs . ensureDir ( `./${ outputDir } ` ) ;
6134
62- await fs . writeFile ( `./${ outputDir } /${ fileName } .css` , `${ banner ( ) } \n${ cssContent } ` ) ;
35+ await fs . writeFile (
36+ `./${ outputDir } /${ fileName } .css` ,
37+ `${ banner ( ) } \n${ isBundle ? swiperBundleCSS : swiperCSS } ` ,
38+ ) ;
6339
6440 if ( minified ) {
65- const minifiedContent = await minifyCSS ( cssContent ) ;
41+ const minifiedContent = await minifyCSS ( isBundle ? swiperBundleCSS : swiperCSS ) ;
6642 await fs . writeFile ( `./${ outputDir } /${ fileName } .min.css` , `${ banner ( ) } \n${ minifiedContent } ` ) ;
6743 }
6844} ;
6945export default async function buildStyles ( ) {
7046 elapsed . start ( 'styles' ) ;
7147 // eslint-disable-next-line import/no-named-as-default-member
7248 const modules = config . modules . filter ( ( name ) => {
73- const lessFilePath = `./src/modules/${ name } /${ name } .less ` ;
74- return fs . existsSync ( lessFilePath ) ;
49+ const cssFilePath = `./src/modules/${ name } /${ name } .css ` ;
50+ return fs . existsSync ( cssFilePath ) ;
7551 } ) ;
7652 buildCSS ( { isBundle : true , modules, minified : isProd } ) ;
7753 buildCSS ( { isBundle : false , modules, minified : isProd } ) ;
7854 if ( isProd ) {
79- // Copy less & scss
80- const files = await globby (
81- [ '**/**.scss' , '**/**.less' , '!**/mixins.less' , '!**/icons/**' , '!**/core/**' ] ,
82- {
83- cwd : path . resolve ( __dirname , '../src' ) ,
84- } ,
85- ) ;
55+ // Copy css
56+ const files = await globby ( [ '**/**.css' ] , {
57+ cwd : path . resolve ( __dirname , '../src' ) ,
58+ } ) ;
8659 await Promise . all (
8760 files . map ( async ( file ) => {
8861 let distFilePath = path . resolve ( __dirname , `../${ outputDir } ` , file ) ;
8962 const srcFilePath = path . resolve ( __dirname , '../src' , file ) ;
90- let distFileContent = await readSwiperFile ( srcFilePath ) ;
91- distFileContent = distFileContent . replace ( '../../swiper-vars' , '../swiper-vars' ) ;
92- if ( file === 'swiper.scss' || file === 'swiper.less' ) {
63+ let distFileContent = fs . readFileSync ( srcFilePath , 'utf-8' ) ;
64+ if ( file === 'swiper.css' ) {
9365 distFileContent = `${ banner ( ) } \n${ distFileContent } ` ;
9466 }
9567 if ( distFilePath . includes ( '/modules/' ) || distFilePath . includes ( '\\modules\\' ) ) {
@@ -101,20 +73,17 @@ export default async function buildStyles() {
10173 await fs . writeFile ( distFilePath , distFileContent ) ;
10274 } ) ,
10375 ) ;
104- const modulesLessFiles = await globby ( [ '**/**.less ' ] , {
76+ const modulesCSSFiles = await globby ( [ '**/**.css ' ] , {
10577 cwd : path . resolve ( __dirname , '../dist/modules' ) ,
10678 absolute : true ,
10779 } ) ;
10880 await Promise . all (
109- modulesLessFiles . map ( async ( filePath ) => {
110- const fileContent = await fs . readFile ( filePath , 'utf-8' ) ;
111- const content = fileContent . replace ( '@themeColor' , config . themeColor ) ;
112- const lessContent = await less ( content , path . dirname ( filePath ) ) . catch ( ( err ) => {
113- throw new Error ( `${ filePath } : ${ err } ` ) ;
114- } ) ;
115- const resultCSS = await autoprefixer ( lessContent ) ;
116- const resultCSSElement = proceedReplacements ( getSplittedCSS ( resultCSS ) . container ) ;
117- const resultFilePath = filePath . replace ( / \. l e s s $ / , '' ) ;
81+ modulesCSSFiles . map ( async ( filePath ) => {
82+ const cssContent = await fs . readFile ( filePath , 'utf-8' ) ;
83+ const resultCSS = await autoprefixer ( cssContent ) ;
84+ const unwrappedCSS = await unwrapCss ( resultCSS ) ;
85+ const resultCSSElement = proceedReplacements ( getSplittedCSS ( unwrappedCSS ) . container ) ;
86+ const resultFilePath = filePath . replace ( / \. c s s $ / , '' ) ;
11887 const minifiedCSS = await minifyCSS ( resultCSS ) ;
11988 const minifiedCSSElement = await minifyCSS ( resultCSSElement ) ;
12089 await fs . writeFile ( `${ resultFilePath } .css` , resultCSS ) ;
0 commit comments