您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

gulpfile.js 6.5KB

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