index.js
function ui5CompileLessLib(oFile) {
  const sDestDir = path.dirname(oFile.path)
  const sFileName = oFile.path.split(path.sep).pop()
  const sLessFileContent = oFile.contents.toString('utf8')

  // options for less-openui5
  const oOptions = {
    lessInput: sLessFileContent,
    rootPaths: [sDestDir],
    rtl: true,
    parser: {
      filename: sFileName,
      paths: [sDestDir]
    },
    compiler: { compress: false }
  }

  // build a theme
  const oBuildThemePromise = builder
    .build(oOptions)
    .catch(oError => {
      // CSS build fails in 99% of all cases, because of a missing .less file
      // create empty LESS file and try again if build failed

      // try to parse error message to find out which missing LESS file caused the failed build
      const sMissingFileName = (oError.message.match(
        /((\.*?\/?)*\w.*\.\w+)/g
      ) || [''])[0]
      const sSourceFileName = oError.filename
      const sMissingFilePath = path.resolve(
        sDestDir,
        sSourceFileName.replace('library.source.less', ''),
        sMissingFileName
      )
      let isIssueFixed = false

      // create missing .less file (with empty content), else the library.css can't be created
      if (!fs.existsSync(sMissingFilePath)) {
        try {
          fs.writeFileSync(sMissingFilePath, '')
          isIssueFixed = true
        } catch (e) {
          isIssueFixed = false
        }
      }

      if (!isIssueFixed) {
        // if this error message raises up, the build failed due to the other 1% cases
        return Promise.reject('Compile UI5 less lib: ')
      }

      // if missing file could be created, try theme build again
      return isIssueFixed ? ui5CompileLessLib(oFile) : Promise.reject()
    })
    .then(oResult => {
      // build css content was successfull >> save result
      const aTargetFiles = [
        {
          path: `${sDestDir}/library.css`,
          content: oResult.css
        },
        {
          path: `${sDestDir}/library-RTL.css`,
          content: oResult.cssRtl
        },
        {
          path: `${sDestDir}/library-parameters.json`,
          content:
            JSON.stringify(
              oResult.variables,
              null,
              oOptions.compiler.compress ? 0 : 4
            ) || ''
        }
      ]

      const aWriteFilesPromises = aTargetFiles.map(oFile => {
        return new Promise((resolve, reject) =>
          fs.writeFile(
            oFile.path,
            oFile.content,
            oError => (oError ? reject() : resolve())
          )
        )
      })

      // return promise
      return Promise.all(aWriteFilesPromises)
    })
    .then(() => {
      // clear builder cache when finished to cleanup memory
      builder.clearCache()
      return Promise.resolve()
    })

  return oBuildThemePromise
}
function ui5CompileLessLib(oFile) {
  const sDestDir = path.dirname(oFile.path)
  const sFileName = oFile.path.split(path.sep).pop()
  const sLessFileContent = oFile.contents.toString('utf8')

  // options for less-openui5
  const oOptions = {
    lessInput: sLessFileContent,
    rootPaths: [sDestDir],
    rtl: true,
    parser: {
      filename: sFileName,
      paths: [sDestDir]
    },
    compiler: { compress: false }
  }

  // build a theme
  const oBuildThemePromise = builder
    .build(oOptions)
    .catch(oError => {
      // CSS build fails in 99% of all cases, because of a missing .less file
      // create empty LESS file and try again if build failed

      // try to parse error message to find out which missing LESS file caused the failed build
      const sMissingFileName = (oError.message.match(
        /((\.*?\/?)*\w.*\.\w+)/g
      ) || [''])[0]
      const sSourceFileName = oError.filename
      const sMissingFilePath = path.resolve(
        sDestDir,
        sSourceFileName.replace('library.source.less', ''),
        sMissingFileName
      )
      let isIssueFixed = false

      // create missing .less file (with empty content), else the library.css can't be created
      if (!fs.existsSync(sMissingFilePath)) {
        try {
          fs.writeFileSync(sMissingFilePath, '')
          isIssueFixed = true
        } catch (e) {
          isIssueFixed = false
        }
      }

      if (!isIssueFixed) {
        // if this error message raises up, the build failed due to the other 1% cases
        return Promise.reject('Compile UI5 less lib: ')
      }

      // if missing file could be created, try theme build again
      return isIssueFixed ? ui5CompileLessLib(oFile) : Promise.reject()
    })
    .then(oResult => {
      // build css content was successfull >> save result
      const aTargetFiles = [
        {
          path: `${sDestDir}/library.css`,
          content: oResult.css
        },
        {
          path: `${sDestDir}/library-RTL.css`,
          content: oResult.cssRtl
        },
        {
          path: `${sDestDir}/library-parameters.json`,
          content:
            JSON.stringify(
              oResult.variables,
              null,
              oOptions.compiler.compress ? 0 : 4
            ) || ''
        }
      ]

      const aWriteFilesPromises = aTargetFiles.map(oFile => {
        return new Promise((resolve, reject) =>
          fs.writeFile(
            oFile.path,
            oFile.content,
            oError => (oError ? reject() : resolve())
          )
        )
      })

      // return promise
      return Promise.all(aWriteFilesPromises)
    })
    .then(() => {
      // clear builder cache when finished to cleanup memory
      builder.clearCache()
      return Promise.resolve()
    })

  return oBuildThemePromise
}
function ui5CompileLessLib ( oFile ) { const sDestDir = path . dirname ( oFile . path ) const sFileName = oFile . path . split ( path . sep ) . pop ( ) const sLessFileContent = oFile . contents . toString ( 'utf8' ) // options for less-openui5 const oOptions = { lessInput : sLessFileContent , rootPaths : [ sDestDir ] , rtl : true , parser : { filename : sFileName , paths : [ sDestDir ] } , compiler : { compress : false } } // build a theme const oBuildThemePromise = builder . build ( oOptions ) . catch ( oError => { // CSS build fails in 99% of all cases, because of a missing .less file // create empty LESS file and try again if build failed // try to parse error message to find out which missing LESS file caused the failed build const sMissingFileName = ( oError . message . match ( / ((\.*?\/?)*\w.*\.\w+) / g ) || [ '' ] ) [ 0 ] const sSourceFileName = oError . filename const sMissingFilePath = path . resolve ( sDestDir , sSourceFileName . replace ( 'library.source.less' , '' ) , sMissingFileName ) let isIssueFixed = false // create missing .less file (with empty content), else the library.css can't be created if ( ! fs . existsSync ( sMissingFilePath ) ) { try { fs . writeFileSync ( sMissingFilePath , '' ) isIssueFixed = true } catch ( e ) { isIssueFixed = false } } if ( ! isIssueFixed ) { // if this error message raises up, the build failed due to the other 1% cases return Promise . reject ( 'Compile UI5 less lib: ' ) } // if missing file could be created, try theme build again return isIssueFixed ? ui5CompileLessLib ( oFile ) : Promise . reject ( ) } ) . then ( oResult => { // build css content was successfull >> save result const aTargetFiles = [ { path : ` ${ sDestDir } ` , content : oResult . css } , { path : ` ${ sDestDir } ` , content : oResult . cssRtl } , { path : ` ${ sDestDir } ` , content : JSON . stringify ( oResult . variables , null , oOptions . compiler . compress ? 0 : 4 ) || '' } ] const aWriteFilesPromises = aTargetFiles . map ( oFile => { return new Promise ( ( resolve , reject ) => fs . writeFile ( oFile . path , oFile . content , oError => ( oError ? reject ( ) : resolve ( ) ) ) ) } ) // return promise return Promise . all ( aWriteFilesPromises ) } ) . then ( ( ) => { // clear builder cache when finished to cleanup memory builder . clearCache ( ) return Promise . resolve ( ) } ) return oBuildThemePromise }
Compile library.source.less and dependencies to library.css.

@param {Vinyl} [oFile] Vinyl file object of library.source.less.
@returns {Promise} Promise.
Compile library . source . less and dependencies to library . css .