You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gulpfile.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. minify: options.minify,
  106. outfile: path.join('dist', 'modules-iife.js'),
  107. })
  108. }
  109. // Build Node、
  110. if (options.node) {
  111. await esbuild.build({
  112. ...buildConfig,
  113. format: 'esm',
  114. platform: 'node',
  115. define: {
  116. TransformStream: 'null',
  117. },
  118. minify: options.minify,
  119. outfile: path.join('dist', 'index.js'),
  120. })
  121. }
  122. // remove DC.js
  123. await fse.remove(dcPath)
  124. }
  125. async function combineJs(options) {
  126. // combine for iife
  127. if (options.iife) {
  128. await gulp
  129. .src([
  130. path.join(dc_common_path, 'dist', 'dc.common.min.js'),
  131. 'dist/modules-iife.js',
  132. ])
  133. .pipe(concat('dc.min.js'))
  134. .pipe(gulp.dest('dist'))
  135. .on('end', () => {
  136. addCopyright(options)
  137. deleteTempFile()
  138. })
  139. }
  140. // combine for node
  141. if (options.node && options.obfuscate) {
  142. await gulp
  143. .src('dist/index.js')
  144. .pipe(gulp.dest('dist'))
  145. .on('end', () => {
  146. addCopyright(options)
  147. })
  148. }
  149. }
  150. async function copyAssets() {
  151. await fse.emptyDir('dist/resources')
  152. await gulp
  153. .src(dc_common_path + '/dist/resources/**', { nodir: true })
  154. .pipe(gulp.dest('dist/resources'))
  155. }
  156. async function addCopyright(options) {
  157. let header = await fse.readFile(
  158. path.join('src', 'copyright', 'header.js'),
  159. 'utf8'
  160. )
  161. header = header
  162. .replace('{{__VERSION__}}', packageJson.version)
  163. .replace('{{__AUTHOR__}}', packageJson.author)
  164. .replace('{{__REPOSITORY__}}', packageJson.repository)
  165. if (options.iife) {
  166. let filePath = path.join('dist', 'dc.min.js')
  167. const content = await fse.readFile(filePath, 'utf8')
  168. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  169. }
  170. if (options.node) {
  171. let filePath = path.join('dist', 'index.js')
  172. const content = await fse.readFile(filePath, 'utf8')
  173. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  174. }
  175. }
  176. async function deleteTempFile() {
  177. await gulp.src(['dist/modules-iife.js'], { read: false }).pipe(clean())
  178. }
  179. async function regenerate(option) {
  180. await fse.remove('dist/dc.min.js')
  181. await fse.remove('dist/dc.min.css')
  182. await buildModules(option)
  183. await combineJs(option)
  184. await buildCSS(option)
  185. }
  186. export const server = gulp.series(startServer)
  187. export const dev = gulp.series(
  188. () => copyAssets(),
  189. () => {
  190. shell.echo(chalk.yellow('============= start dev =============='))
  191. const watcher = gulp.watch('src', {
  192. persistent: true,
  193. awaitWriteFinish: {
  194. stabilityThreshold: 1000,
  195. pollInterval: 100,
  196. },
  197. })
  198. watcher
  199. .on('ready', async () => {
  200. await regenerate({ iife: true, minify: false })
  201. await startServer()
  202. })
  203. .on('change', async () => {
  204. let now = new Date().getTime()
  205. try {
  206. await regenerate({ iife: true, minify: false })
  207. shell.echo(
  208. chalk.green(`regenerate lib takes ${new Date().getTime() - now} ms`)
  209. )
  210. } catch (e) {
  211. shell.error(e)
  212. }
  213. })
  214. return watcher
  215. }
  216. )
  217. export const buildIIFE = gulp.series(
  218. () => buildModules({ iife: true }),
  219. () => combineJs({ iife: true }),
  220. () => buildCSS({ minify: true }),
  221. copyAssets
  222. )
  223. export const buildNode = gulp.series(
  224. () => buildModules({ node: true, minify: true }),
  225. () => combineJs({ node: true }),
  226. () => buildCSS({ minify: true }),
  227. copyAssets
  228. )
  229. export const build = gulp.series(
  230. () => buildModules({ iife: true, minify: true }),
  231. () => combineJs({ iife: true }),
  232. () => buildModules({ node: true, minify: true }),
  233. () => combineJs({ node: true }),
  234. () => buildCSS({ minify: true }),
  235. copyAssets
  236. )
  237. export const buildRelease = gulp.series(
  238. () => buildModules({ iife: true, minify: true }),
  239. () => combineJs({ iife: true }),
  240. () => buildModules({ node: true, minify: true }),
  241. () => combineJs({ node: true }),
  242. () => buildCSS({ minify: true }),
  243. copyAssets
  244. )