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.6KB

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