Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. banner: options.node ? '(function(){' : '',
  92. footer: options.node ? '})()' : '',
  93. })
  94. }
  95. async function buildCSS() {
  96. const bundle = await rollup({
  97. input: 'src/themes/index.js',
  98. plugins: [
  99. commonjs(),
  100. resolve({ preferBuiltins: true }),
  101. scss({
  102. outputStyle: 'compressed',
  103. fileName: 'dc.min.css',
  104. }),
  105. ],
  106. })
  107. return bundle.write({
  108. file: 'dist/dc.min.css',
  109. })
  110. }
  111. async function buildModules(options) {
  112. const dcPath = path.join('src', 'DC.js')
  113. const content = await fse.readFile(path.join('src', 'index.js'), 'utf8')
  114. await fse.ensureFile(dcPath)
  115. const exportVersion = `export const VERSION = '${packageJson.version}'`
  116. const cmdOut_content = await fse.readFile(
  117. path.join('src', 'copyright', 'cmdOut.js'),
  118. 'utf8'
  119. )
  120. const exportCmdOut = `
  121. export function __cmdOut() {
  122. ${cmdOut_content
  123. .replace('{{__VERSION__}}', packageJson.version)
  124. .replace('{{__TIME__}}', getTime())
  125. .replace(
  126. '{{__ENGINE_VERSION__}}',
  127. packageJson.devDependencies['@cesium/engine'].replace('^', '')
  128. )
  129. .replace('{{__AUTHOR__}}', packageJson.author)
  130. .replace('{{__HOME_PAGE__}}', packageJson.homepage)
  131. .replace('{{__REPOSITORY__}}', packageJson.repository)}
  132. }`
  133. const exportNamespace = `
  134. export const __namespace = {
  135. Cesium: exports.Cesium
  136. }
  137. `
  138. // Build IIFE
  139. if (options.iife) {
  140. await fse.outputFile(
  141. dcPath,
  142. `
  143. ${exportVersion}
  144. ${exportCmdOut}
  145. ${content}
  146. `,
  147. {
  148. encoding: 'utf8',
  149. }
  150. )
  151. await esbuild.build({
  152. ...buildConfig,
  153. format: 'iife',
  154. globalName: 'DC',
  155. outfile: path.join('dist', 'modules.js'),
  156. })
  157. }
  158. // Build Node、
  159. if (options.node) {
  160. await fse.outputFile(
  161. dcPath,
  162. `
  163. ${exportNamespace}
  164. ${exportVersion}
  165. ${exportCmdOut}
  166. ${content}
  167. `,
  168. {
  169. encoding: 'utf8',
  170. }
  171. )
  172. await esbuild.build({
  173. ...buildConfig,
  174. format: 'cjs',
  175. platform: 'node',
  176. define: {
  177. TransformStream: 'null',
  178. },
  179. outfile: path.join('dist', 'modules.cjs'),
  180. })
  181. }
  182. // remove DC.js
  183. await fse.remove(dcPath)
  184. }
  185. async function combineJs(options) {
  186. // combine for iife
  187. if (options.iife) {
  188. if (options.obfuscate) {
  189. await gulp
  190. .src('dist/modules.js')
  191. .pipe(javascriptObfuscator(obfuscatorConfig))
  192. .pipe(gulp.src('dist/namespace.js'))
  193. .pipe(concat('dc.min.js'))
  194. .pipe(gulp.dest('dist'))
  195. .on('end', () => {
  196. addCopyright(options)
  197. deleteTempFile(options)
  198. })
  199. } else {
  200. await gulp
  201. .src(['dist/modules.js', 'dist/namespace.js'])
  202. .pipe(concat('dc.min.js'))
  203. .pipe(gulp.dest('dist'))
  204. .on('end', () => {
  205. addCopyright(options)
  206. deleteTempFile(options)
  207. })
  208. }
  209. }
  210. // combine for node
  211. if (options.node) {
  212. if (options.obfuscate) {
  213. await gulp
  214. .src('dist/modules.cjs')
  215. .pipe(javascriptObfuscator(obfuscatorConfig))
  216. .pipe(gulp.dest('dist'))
  217. .on('end', async () => {
  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. } else {
  228. await gulp
  229. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  230. .pipe(concat('index.cjs'))
  231. .pipe(gulp.dest('dist'))
  232. .on('end', () => {
  233. addCopyright(options)
  234. deleteTempFile(options)
  235. })
  236. }
  237. }
  238. }
  239. async function copyAssets() {
  240. await fse.emptyDir('dist/resources')
  241. await gulp
  242. .src('./node_modules/@cesium/engine/Build/Workers/**', { nodir: true })
  243. .pipe(gulp.dest('dist/resources/Workers'))
  244. await gulp
  245. .src('./node_modules/@cesium/engine/Source/Assets/**', { nodir: true })
  246. .pipe(gulp.dest('dist/resources/Assets'))
  247. await gulp
  248. .src('./node_modules/@cesium/engine/Source/ThirdParty/**', { nodir: true })
  249. .pipe(gulp.dest('dist/resources/ThirdParty'))
  250. }
  251. async function addCopyright(options) {
  252. let header = await fse.readFile(
  253. path.join('src', 'copyright', 'header.js'),
  254. 'utf8'
  255. )
  256. header = header
  257. .replace('{{__VERSION__}}', packageJson.version)
  258. .replace('{{__AUTHOR__}}', packageJson.author)
  259. .replace('{{__REPOSITORY__}}', packageJson.repository)
  260. if (options.iife) {
  261. let filePath = path.join('dist', 'dc.min.js')
  262. const content = await fse.readFile(filePath, 'utf8')
  263. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  264. }
  265. if (options.node) {
  266. let filePath = path.join('dist', 'index.cjs')
  267. const content = await fse.readFile(filePath, 'utf8')
  268. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  269. }
  270. }
  271. async function deleteTempFile(options) {
  272. if (options.iife) {
  273. await gulp
  274. .src(['dist/namespace.js', 'dist/modules.js'], { read: false })
  275. .pipe(clean())
  276. }
  277. if (options.node) {
  278. await gulp
  279. .src(['dist/namespace.cjs', 'dist/modules.cjs'], { read: false })
  280. .pipe(clean())
  281. }
  282. }
  283. export const build = gulp.series(
  284. () => buildNamespace({ node: true }),
  285. () => buildModules({ node: true }),
  286. () => combineJs({ node: true }),
  287. () => buildNamespace({ iife: true }),
  288. () => buildModules({ iife: true }),
  289. () => combineJs({ iife: true }),
  290. buildCSS,
  291. copyAssets
  292. )
  293. export const buildNode = gulp.series(
  294. () => buildNamespace({ node: true }),
  295. () => buildModules({ node: true }),
  296. () => combineJs({ node: true }),
  297. buildCSS,
  298. copyAssets
  299. )
  300. export const buildIIFE = gulp.series(
  301. () => buildNamespace({ iife: true }),
  302. () => buildModules({ iife: true }),
  303. () => combineJs({ iife: true }),
  304. buildCSS,
  305. copyAssets
  306. )
  307. export const buildRelease = gulp.series(
  308. () => buildNamespace({ node: true }),
  309. () => buildModules({ node: true }),
  310. () => combineJs({ node: true, obfuscate: true }),
  311. () => buildNamespace({ iife: true }),
  312. () => buildModules({ iife: true }),
  313. () => combineJs({ iife: true, obfuscate: true }),
  314. buildCSS,
  315. copyAssets
  316. )
  317. export const server = gulp.series(startServer)