Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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