Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. @author : Caven Chen
  3. **/
  4. 'use strict'
  5. import fse from 'fs-extra'
  6. import path from 'path'
  7. import gulp from 'gulp'
  8. import esbuild from 'esbuild'
  9. import concat from 'gulp-concat'
  10. import clean from 'gulp-clean'
  11. import startServer from './server.js'
  12. import inlineImage from 'esbuild-plugin-inline-image'
  13. import { sassPlugin } from 'esbuild-sass-plugin'
  14. import { glsl } from 'esbuild-plugin-glsl'
  15. import GlobalsPlugin from 'esbuild-plugin-globals'
  16. import shell from 'shelljs'
  17. import chalk from 'chalk'
  18. const cesium_path = path.resolve('./node_modules/cesium/Build/Cesium')
  19. const packageJson = fse.readJsonSync('./package.json')
  20. const buildConfig = {
  21. entryPoints: ['src/DC.js'],
  22. bundle: true,
  23. color: true,
  24. legalComments: `inline`,
  25. logLimit: 0,
  26. target: `es2019`,
  27. minify: false,
  28. sourcemap: false,
  29. write: true,
  30. logLevel: 'info',
  31. plugins: [
  32. inlineImage({
  33. limit: -1,
  34. }),
  35. glsl(),
  36. sassPlugin(),
  37. ],
  38. external: ['cesium'],
  39. }
  40. function getTime() {
  41. let now = new Date()
  42. let m = now.getMonth() + 1
  43. m = m < 10 ? '0' + m : m
  44. let d = now.getDate()
  45. d = d < 10 ? '0' + d : d
  46. return `${now.getFullYear()}-${m}-${d}`
  47. }
  48. /**
  49. *
  50. * @param options
  51. * @returns {Promise<void>}
  52. */
  53. async function buildCSS(options) {
  54. await esbuild.build({
  55. ...buildConfig,
  56. minify: options.minify,
  57. entryPoints: ['src/themes/index.scss'],
  58. outfile: path.join('dist', 'dc.min.css'),
  59. })
  60. }
  61. /**
  62. *
  63. * @param options
  64. * @returns {Promise<void>}
  65. */
  66. async function buildModules(options) {
  67. const dcPath = path.join('src', 'DC.js')
  68. const content = await fse.readFile(path.join('src', 'index.js'), 'utf8')
  69. await fse.ensureFile(dcPath)
  70. const exportVersion = `export const VERSION = '${packageJson.version}'`
  71. const cmdOut_content = await fse.readFile(
  72. path.join('src', 'copyright', 'cmdOut.js'),
  73. 'utf8'
  74. )
  75. const cmdOutFunction = `
  76. globalThis.__cmdOut = () => {
  77. ${cmdOut_content
  78. .replace('{{__VERSION__}}', packageJson.version)
  79. .replace('{{__TIME__}}', getTime())
  80. .replace(
  81. '{{__CESIUM_VERSION__}}',
  82. packageJson.dependencies['cesium'].replace('^', '')
  83. )
  84. .replace('{{__AUTHOR__}}', packageJson.author)
  85. .replace('{{__HOME_PAGE__}}', packageJson.homepage)
  86. .replace('{{__REPOSITORY__}}', packageJson.repository)}
  87. }`
  88. await fse.outputFile(
  89. dcPath,
  90. `
  91. ${cmdOutFunction}
  92. ${content}
  93. ${exportVersion}
  94. `,
  95. {
  96. encoding: 'utf8',
  97. }
  98. )
  99. // Build IIFE
  100. if (options.iife) {
  101. await esbuild.build({
  102. ...buildConfig,
  103. format: 'iife',
  104. globalName: 'DC',
  105. plugins: [
  106. ...buildConfig.plugins,
  107. GlobalsPlugin({
  108. cesium: 'globalThis.Cesium || Cesium',
  109. }),
  110. ],
  111. minify: options.minify,
  112. outfile: path.join('dist', 'modules-iife.js'),
  113. })
  114. }
  115. // Build Node、
  116. if (options.node) {
  117. await esbuild.build({
  118. ...buildConfig,
  119. format: 'esm',
  120. platform: 'node',
  121. define: {
  122. TransformStream: 'null',
  123. },
  124. minify: options.minify,
  125. outfile: path.join('dist', 'index.js'),
  126. })
  127. }
  128. // remove DC.js
  129. await fse.remove(dcPath)
  130. }
  131. /**
  132. *
  133. * @param options
  134. * @returns {Promise<void>}
  135. */
  136. async function addCopyright(options) {
  137. let header = await fse.readFile(
  138. path.join('src', 'copyright', 'header.js'),
  139. 'utf8'
  140. )
  141. header = header
  142. .replace('{{__VERSION__}}', packageJson.version)
  143. .replace('{{__AUTHOR__}}', packageJson.author)
  144. .replace('{{__REPOSITORY__}}', packageJson.repository)
  145. if (options.iife) {
  146. let filePath = path.join('dist', 'dc.min.js')
  147. const content = await fse.readFile(filePath, 'utf8')
  148. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  149. }
  150. if (options.node) {
  151. let filePath = path.join('dist', 'index.js')
  152. const content = await fse.readFile(filePath, 'utf8')
  153. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  154. }
  155. }
  156. /**
  157. *
  158. * @returns {Promise<void>}
  159. */
  160. async function deleteTempFile() {
  161. await gulp
  162. .src([path.join('dist', 'modules-iife.js')], { read: false })
  163. .pipe(clean())
  164. }
  165. async function combineJs(options) {
  166. // combine for iife
  167. if (options.iife) {
  168. await fse.ensureFile(path.join(cesium_path, 'Cesium.js'))
  169. await gulp
  170. .src([
  171. path.join(cesium_path, 'Cesium.js'),
  172. path.join('dist', 'modules-iife.js'),
  173. ])
  174. .pipe(concat('dc.min.js'))
  175. .pipe(gulp.dest('dist'))
  176. .on('end', () => {
  177. addCopyright(options)
  178. deleteTempFile()
  179. })
  180. }
  181. // combine for node
  182. if (options.node) {
  183. await gulp
  184. .src(path.join('dist', 'index.js'))
  185. .pipe(gulp.dest('dist'))
  186. .on('end', () => {
  187. addCopyright(options)
  188. })
  189. }
  190. }
  191. async function copyAssets() {
  192. await fse.emptyDir(path.join('dist', 'resources'))
  193. for (const dir of ['Assets', 'ThirdParty', 'Workers']) {
  194. await gulp
  195. .src(path.join(cesium_path, dir, '**'), { nodir: true })
  196. .pipe(gulp.dest(path.join('dist', 'resources', dir)))
  197. }
  198. }
  199. async function regenerate(option) {
  200. await fse.remove(path.join('dist', 'dc.min.js'))
  201. await fse.remove(path.join('dist', 'dc.min.css'))
  202. await buildModules(option)
  203. await combineJs(option)
  204. await buildCSS(option)
  205. }
  206. export const server = gulp.series(startServer)
  207. export const dev = gulp.series(
  208. () => copyAssets(),
  209. () => {
  210. shell.echo(chalk.yellow('============= start dev =============='))
  211. const watcher = gulp.watch('src', {
  212. persistent: true,
  213. awaitWriteFinish: {
  214. stabilityThreshold: 1000,
  215. pollInterval: 100,
  216. },
  217. })
  218. watcher
  219. .on('ready', async () => {
  220. await regenerate({ iife: true, minify: false })
  221. await startServer()
  222. })
  223. .on('change', async () => {
  224. let now = new Date().getTime()
  225. try {
  226. await regenerate({ iife: true, minify: false })
  227. shell.echo(
  228. chalk.green(`regenerate lib takes ${new Date().getTime() - now} ms`)
  229. )
  230. } catch (e) {
  231. shell.error(e)
  232. }
  233. })
  234. return watcher
  235. }
  236. )
  237. export const buildIIFE = gulp.series(
  238. () => buildModules({ iife: true, minify: true }),
  239. () => combineJs({ iife: true }),
  240. () => buildCSS({ minify: true }),
  241. copyAssets
  242. )
  243. export const buildNode = gulp.series(
  244. () => buildModules({ node: true, minify: true }),
  245. () => combineJs({ node: true }),
  246. () => buildCSS({ minify: true }),
  247. copyAssets
  248. )
  249. export const build = gulp.series(
  250. () => buildModules({ iife: true, minify: true }),
  251. () => combineJs({ iife: true }),
  252. () => buildModules({ node: true, minify: true }),
  253. () => combineJs({ node: true }),
  254. () => buildCSS({ minify: true }),
  255. copyAssets
  256. )
  257. export const buildRelease = gulp.series(
  258. () => buildModules({ iife: true, minify: true }),
  259. () => combineJs({ iife: true }),
  260. () => buildModules({ node: true, minify: true }),
  261. () => combineJs({ node: true }),
  262. () => buildCSS({ minify: true }),
  263. copyAssets
  264. )