| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 | /**
 @author : Caven Chen
 **/
'use strict'
import fse from 'fs-extra'
import path from 'path'
import gulp from 'gulp'
import esbuild from 'esbuild'
import concat from 'gulp-concat'
import clean from 'gulp-clean'
import startServer from './server.js'
import inlineImage from 'esbuild-plugin-inline-image'
import { sassPlugin } from 'esbuild-sass-plugin'
import { glsl } from 'esbuild-plugin-glsl'
import GlobalsPlugin from 'esbuild-plugin-globals'
import shell from 'shelljs'
import chalk from 'chalk'
const dc_common_path = './node_modules/@dvgis/dc-common'
const packageJson = fse.readJsonSync('./package.json')
const c_packageJson = fse.readJsonSync(
  path.join(dc_common_path, 'package.json')
)
const buildConfig = {
  entryPoints: ['src/DC.js'],
  bundle: true,
  color: true,
  legalComments: `inline`,
  logLimit: 0,
  target: `es2019`,
  minify: false,
  sourcemap: false,
  write: true,
  logLevel: 'info',
  plugins: [
    inlineImage({
      limit: -1,
    }),
    glsl(),
    sassPlugin(),
  ],
  external: ['@dvgis/dc-common'],
}
function getTime() {
  let now = new Date()
  let m = now.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = now.getDate()
  d = d < 10 ? '0' + d : d
  return `${now.getFullYear()}-${m}-${d}`
}
async function buildCSS(options) {
  await esbuild.build({
    ...buildConfig,
    minify: options.minify,
    entryPoints: ['src/themes/index.scss'],
    outfile: path.join('dist', 'dc.min.css'),
  })
}
async function buildModules(options) {
  const dcPath = path.join('src', 'DC.js')
  const content = await fse.readFile(path.join('src', 'index.js'), 'utf8')
  await fse.ensureFile(dcPath)
  const exportVersion = `export const VERSION = '${packageJson.version}'`
  const cmdOut_content = await fse.readFile(
    path.join('src', 'copyright', 'cmdOut.js'),
    'utf8'
  )
  const cmdOutFunction = `
        function __cmdOut() {
          ${cmdOut_content
            .replace('{{__VERSION__}}', packageJson.version)
            .replace('{{__TIME__}}', getTime())
            .replace(
              '{{__ENGINE_VERSION__}}',
              c_packageJson.dependencies['@cesium/engine'].replace('^', '')
            )
            .replace('{{__AUTHOR__}}', packageJson.author)
            .replace('{{__HOME_PAGE__}}', packageJson.homepage)
            .replace('{{__REPOSITORY__}}', packageJson.repository)}
    }`
  await fse.outputFile(
    dcPath,
    `
              ${content}
              ${cmdOutFunction}
              ${exportVersion}
            `,
    {
      encoding: 'utf8',
    }
  )
  // Build IIFE
  if (options.iife) {
    await esbuild.build({
      ...buildConfig,
      format: 'iife',
      globalName: 'DC',
      plugins: [
        ...buildConfig.plugins,
        GlobalsPlugin({
          '@dvgis/dc-common': 'DC_Common',
        }),
      ],
      minify: options.minify,
      outfile: path.join('dist', 'modules-iife.js'),
    })
  }
  // Build Node、
  if (options.node) {
    await esbuild.build({
      ...buildConfig,
      format: 'esm',
      platform: 'node',
      define: {
        TransformStream: 'null',
      },
      minify: options.minify,
      outfile: path.join('dist', 'index.js'),
    })
  }
  // remove DC.js
  await fse.remove(dcPath)
}
async function combineJs(options) {
  // combine for iife
  if (options.iife) {
    await gulp
      .src([
        path.join(dc_common_path, 'dist', 'dc.common.min.js'),
        'dist/modules-iife.js',
      ])
      .pipe(concat('dc.min.js'))
      .pipe(gulp.dest('dist'))
      .on('end', () => {
        addCopyright(options)
        deleteTempFile()
      })
  }
  // combine for node
  if (options.node) {
    await gulp
      .src('dist/index.js')
      .pipe(gulp.dest('dist'))
      .on('end', () => {
        addCopyright(options)
      })
  }
}
async function copyAssets() {
  await fse.emptyDir('dist/resources')
  await gulp
    .src(dc_common_path + '/dist/resources/**', { nodir: true })
    .pipe(gulp.dest('dist/resources'))
}
async function addCopyright(options) {
  let header = await fse.readFile(
    path.join('src', 'copyright', 'header.js'),
    'utf8'
  )
  header = header
    .replace('{{__VERSION__}}', packageJson.version)
    .replace('{{__AUTHOR__}}', packageJson.author)
    .replace('{{__REPOSITORY__}}', packageJson.repository)
  if (options.iife) {
    let filePath = path.join('dist', 'dc.min.js')
    const content = await fse.readFile(filePath, 'utf8')
    await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  }
  if (options.node) {
    let filePath = path.join('dist', 'index.js')
    const content = await fse.readFile(filePath, 'utf8')
    await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  }
}
async function deleteTempFile() {
  await gulp.src(['dist/modules-iife.js'], { read: false }).pipe(clean())
}
async function regenerate(option) {
  await fse.remove('dist/dc.min.js')
  await fse.remove('dist/dc.min.css')
  await buildModules(option)
  await combineJs(option)
  await buildCSS(option)
}
export const server = gulp.series(startServer)
export const dev = gulp.series(
  () => copyAssets(),
  () => {
    shell.echo(chalk.yellow('============= start dev =============='))
    const watcher = gulp.watch('src', {
      persistent: true,
      awaitWriteFinish: {
        stabilityThreshold: 1000,
        pollInterval: 100,
      },
    })
    watcher
      .on('ready', async () => {
        await regenerate({ iife: true, minify: false })
        await startServer()
      })
      .on('change', async () => {
        let now = new Date().getTime()
        try {
          await regenerate({ iife: true, minify: false })
          shell.echo(
            chalk.green(`regenerate lib takes ${new Date().getTime() - now} ms`)
          )
        } catch (e) {
          shell.error(e)
        }
      })
    return watcher
  }
)
export const buildIIFE = gulp.series(
  () => buildModules({ iife: true }),
  () => combineJs({ iife: true }),
  () => buildCSS({ minify: true }),
  copyAssets
)
export const buildNode = gulp.series(
  () => buildModules({ node: true, minify: true }),
  () => combineJs({ node: true }),
  () => buildCSS({ minify: true }),
  copyAssets
)
export const build = gulp.series(
  () => buildModules({ iife: true, minify: true }),
  () => combineJs({ iife: true }),
  () => buildModules({ node: true, minify: true }),
  () => combineJs({ node: true }),
  () => buildCSS({ minify: true }),
  copyAssets
)
export const buildRelease = gulp.series(
  () => buildModules({ iife: true, minify: true }),
  () => combineJs({ iife: true }),
  () => buildModules({ node: true, minify: true }),
  () => combineJs({ node: true }),
  () => buildCSS({ minify: true }),
  copyAssets
)
 |