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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. @author : Caven Chen
  3. @date : 2023-05-06
  4. **/
  5. 'use strict'
  6. import fse from 'fs-extra'
  7. import path from 'path'
  8. import gulp from 'gulp'
  9. import esbuild from 'esbuild'
  10. import concat from 'gulp-concat'
  11. import { rollup } from 'rollup'
  12. import clean from 'gulp-clean'
  13. import commonjs from '@rollup/plugin-commonjs'
  14. import resolve from '@rollup/plugin-node-resolve'
  15. import scss from 'rollup-plugin-scss'
  16. import javascriptObfuscator from 'gulp-javascript-obfuscator'
  17. import { babel } from '@rollup/plugin-babel'
  18. import startServer from './server.js'
  19. import { uglify } from 'rollup-plugin-uglify'
  20. const obfuscatorConfig = {
  21. compact: true, //压缩代码
  22. identifierNamesGenerator: 'hexadecimal', //标识符的混淆方式 hexadecimal(十六进制) mangled(短标识符)
  23. renameGlobals: false, //是否启用全局变量和函数名称的混淆
  24. rotateStringArray: true, //通过固定和随机(在代码混淆时生成)的位置移动数组。这使得将删除的字符串的顺序与其原始位置相匹配变得更加困难。如果原始源代码不小,建议使用此选项,因为辅助函数可以引起注意。
  25. selfDefending: true, //混淆后的代码,不能使用代码美化,同时需要配置 compact:true;
  26. stringArray: true, //删除字符串文字并将它们放在一个特殊的数组中
  27. stringArrayEncoding: ['base64'],
  28. stringArrayThreshold: 0.75,
  29. transformObjectKeys: false,
  30. unicodeEscapeSequence: false, //允许启用/禁用字符串转换为unicode转义序列。Unicode转义序列大大增加了代码大小,并且可以轻松地将字符串恢复为原始视图。建议仅对小型源代码启用此选项。
  31. }
  32. const buildConfig = {
  33. entryPoints: ['src/DC.js'],
  34. bundle: true,
  35. color: true,
  36. legalComments: `inline`,
  37. logLimit: 0,
  38. target: `es2020`,
  39. minify: false,
  40. sourcemap: false,
  41. write: true,
  42. logLevel: 'info',
  43. }
  44. const packageJson = fse.readJsonSync('./package.json')
  45. function getTime() {
  46. let now = new Date()
  47. let m = now.getMonth() + 1
  48. m = m < 10 ? '0' + m : m
  49. let d = now.getDate()
  50. d = d < 10 ? '0' + d : d
  51. return `${now.getFullYear()}-${m}-${d}`
  52. }
  53. async function buildNamespace(options) {
  54. const bundle = await rollup({
  55. input: 'src/namespace.js',
  56. plugins: [
  57. commonjs(),
  58. resolve({ preferBuiltins: true }),
  59. babel({
  60. babelHelpers: 'runtime',
  61. presets: [
  62. [
  63. '@babel/preset-env',
  64. {
  65. modules: false,
  66. targets: {
  67. browsers: ['> 1%', 'last 2 versions', 'ie >= 10'],
  68. },
  69. },
  70. ],
  71. ],
  72. plugins: ['@babel/plugin-transform-runtime'],
  73. }),
  74. uglify(),
  75. ],
  76. })
  77. return bundle.write({
  78. name: 'DC.__namespace',
  79. file: options.node ? 'dist/namespace.cjs' : 'dist/namespace.js',
  80. format: options.node ? 'cjs' : 'umd',
  81. sourcemap: false,
  82. })
  83. }
  84. async function buildCSS() {
  85. const bundle = await rollup({
  86. input: 'src/themes/index.js',
  87. plugins: [
  88. commonjs(),
  89. resolve({ preferBuiltins: true }),
  90. scss({
  91. outputStyle: 'compressed',
  92. fileName: 'dc.min.css',
  93. }),
  94. ],
  95. })
  96. return bundle.write({
  97. file: 'dist/dc.min.css',
  98. })
  99. }
  100. async function buildModules(options) {
  101. const dcPath = path.join('src', 'DC.js')
  102. const content = await fse.readFile(path.join('src', 'index.js'), 'utf8')
  103. await fse.ensureFile(dcPath)
  104. const exportVersion = `export const VERSION = '${packageJson.version}'`
  105. const cmdOut_content = await fse.readFile(
  106. path.join('src', 'copyright', 'cmdOut.js'),
  107. 'utf8'
  108. )
  109. const exportCmdOut = `
  110. export function __cmdOut() {
  111. ${cmdOut_content
  112. .replace('{{__VERSION__}}', packageJson.version)
  113. .replace('{{__TIME__}}', getTime())
  114. .replace(
  115. '{{__ENGINE_VERSION__}}',
  116. packageJson.devDependencies['@cesium/engine'].replace('^', '')
  117. )
  118. .replace('{{__AUTHOR__}}', packageJson.author)
  119. .replace('{{__HOME_PAGE__}}', packageJson.homepage)
  120. .replace('{{__REPOSITORY__}}', packageJson.repository)}
  121. }`
  122. const exportNamespace = `
  123. export const __namespace = {
  124. Cesium: exports.Cesium,
  125. turf: exports.turf
  126. }
  127. `
  128. // Build IIFE
  129. if (options.iife) {
  130. await fse.outputFile(
  131. dcPath,
  132. `
  133. ${content}
  134. ${exportVersion}
  135. ${exportCmdOut}
  136. `,
  137. {
  138. encoding: 'utf8',
  139. }
  140. )
  141. await esbuild.build({
  142. ...buildConfig,
  143. format: 'iife',
  144. globalName: 'DC',
  145. outfile: path.join('dist', 'modules.js'),
  146. })
  147. }
  148. // Build Node、
  149. if (options.node) {
  150. await fse.outputFile(
  151. dcPath,
  152. `
  153. ${content}
  154. ${exportNamespace}
  155. ${exportVersion}
  156. ${exportCmdOut}
  157. `,
  158. {
  159. encoding: 'utf8',
  160. }
  161. )
  162. await esbuild.build({
  163. ...buildConfig,
  164. format: 'cjs',
  165. platform: 'node',
  166. define: {
  167. TransformStream: 'null',
  168. },
  169. outfile: path.join('dist', 'modules.cjs'),
  170. })
  171. }
  172. // remove DC.js
  173. await fse.remove(dcPath)
  174. }
  175. async function combineJs(options) {
  176. // combine for iife
  177. if (options.iife) {
  178. if (options.obfuscate) {
  179. await gulp
  180. .src('dist/modules.js')
  181. .pipe(javascriptObfuscator(obfuscatorConfig))
  182. .pipe(gulp.src('dist/namespace.js'))
  183. .pipe(concat('dc.min.js'))
  184. .pipe(gulp.dest('dist'))
  185. .on('end', () => {
  186. addCopyright(options)
  187. deleteTempFile(options)
  188. })
  189. } else {
  190. await gulp
  191. .src(['dist/modules.js', 'dist/namespace.js'])
  192. .pipe(concat('dc.min.js'))
  193. .pipe(gulp.dest('dist'))
  194. .on('end', () => {
  195. addCopyright(options)
  196. deleteTempFile(options)
  197. })
  198. }
  199. }
  200. // combine for node
  201. if (options.node) {
  202. if (options.obfuscate) {
  203. await gulp
  204. .src('dist/modules.cjs')
  205. .pipe(javascriptObfuscator(obfuscatorConfig))
  206. .pipe(gulp.dest('dist'))
  207. .on('end', async () => {
  208. await gulp
  209. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  210. .pipe(concat('index.cjs'))
  211. .pipe(gulp.dest('dist'))
  212. .on('end', () => {
  213. addCopyright(options)
  214. deleteTempFile(options)
  215. })
  216. })
  217. } else {
  218. await gulp
  219. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  220. .pipe(concat('index.cjs'))
  221. .pipe(gulp.dest('dist'))
  222. .on('end', () => {
  223. addCopyright(options)
  224. deleteTempFile(options)
  225. })
  226. }
  227. }
  228. }
  229. async function copyAssets() {
  230. await fse.emptyDir('dist/resources')
  231. await gulp
  232. .src('./node_modules/@cesium/engine/Build/Workers/**', { nodir: true })
  233. .pipe(gulp.dest('dist/resources/Workers'))
  234. await gulp
  235. .src('./node_modules/@cesium/engine/Source/Assets/**', { nodir: true })
  236. .pipe(gulp.dest('dist/resources/Assets'))
  237. await gulp
  238. .src('./node_modules/@cesium/engine/Source/ThirdParty/**', { nodir: true })
  239. .pipe(gulp.dest('dist/resources/ThirdParty'))
  240. }
  241. async function addCopyright(options) {
  242. let header = await fse.readFile(
  243. path.join('src', 'copyright', 'header.js'),
  244. 'utf8'
  245. )
  246. header = header
  247. .replace('{{__VERSION__}}', packageJson.version)
  248. .replace('{{__AUTHOR__}}', packageJson.author)
  249. .replace('{{__REPOSITORY__}}', packageJson.repository)
  250. if (options.iife) {
  251. let filePath = path.join('dist', 'dc.min.js')
  252. const content = await fse.readFile(filePath, 'utf8')
  253. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  254. }
  255. if (options.node) {
  256. let filePath = path.join('dist', 'index.cjs')
  257. const content = await fse.readFile(filePath, 'utf8')
  258. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  259. }
  260. }
  261. async function deleteTempFile(options) {
  262. if (options.iife) {
  263. await gulp
  264. .src(['dist/namespace.js', 'dist/modules.js'], { read: false })
  265. .pipe(clean())
  266. }
  267. if (options.node) {
  268. await gulp
  269. .src(['dist/namespace.cjs', 'dist/modules.cjs'], { read: false })
  270. .pipe(clean())
  271. }
  272. }
  273. export const build = gulp.series(
  274. () => buildNamespace({ node: true }),
  275. () => buildModules({ node: true }),
  276. () => combineJs({ node: true }),
  277. () => buildNamespace({ iife: true }),
  278. () => buildModules({ iife: true }),
  279. () => combineJs({ iife: true }),
  280. buildCSS,
  281. copyAssets
  282. )
  283. export const buildNode = gulp.series(
  284. () => buildNamespace({ node: true }),
  285. () => buildModules({ node: true }),
  286. () => combineJs({ node: true }),
  287. buildCSS,
  288. copyAssets
  289. )
  290. export const buildIIFE = gulp.series(
  291. () => buildNamespace({ iife: true }),
  292. () => buildModules({ iife: true }),
  293. () => combineJs({ iife: true }),
  294. buildCSS,
  295. copyAssets
  296. )
  297. export const buildRelease = gulp.series(
  298. () => buildNamespace({ node: true }),
  299. () => buildModules({ node: true }),
  300. () => combineJs({ node: true, obfuscate: true }),
  301. () => buildNamespace({ iife: true }),
  302. () => buildModules({ iife: true }),
  303. () => combineJs({ iife: true, obfuscate: true }),
  304. buildCSS,
  305. copyAssets
  306. )
  307. export const server = gulp.series(startServer)