Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

gulpfile.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. plugins: [
  46. inlineImage({
  47. limit: -1,
  48. }),
  49. glsl(),
  50. ],
  51. }
  52. const packageJson = fse.readJsonSync('./package.json')
  53. function getTime() {
  54. let now = new Date()
  55. let m = now.getMonth() + 1
  56. m = m < 10 ? '0' + m : m
  57. let d = now.getDate()
  58. d = d < 10 ? '0' + d : d
  59. return `${now.getFullYear()}-${m}-${d}`
  60. }
  61. async function buildNamespace(options) {
  62. const bundle = await rollup({
  63. input: 'src/namespace/libs.js',
  64. plugins: [
  65. commonjs(),
  66. resolve({ preferBuiltins: true }),
  67. babel({
  68. babelHelpers: 'runtime',
  69. presets: [
  70. [
  71. '@babel/preset-env',
  72. {
  73. modules: false,
  74. targets: {
  75. browsers: ['> 1%', 'last 2 versions', 'ie >= 10'],
  76. },
  77. },
  78. ],
  79. ],
  80. plugins: ['@babel/plugin-transform-runtime'],
  81. }),
  82. uglify(),
  83. ],
  84. })
  85. return bundle.write({
  86. name: 'DC.__namespace',
  87. file: options.node ? 'dist/namespace.cjs' : 'dist/namespace.js',
  88. format: options.node ? 'cjs' : 'umd',
  89. sourcemap: false,
  90. })
  91. }
  92. async function buildCSS() {
  93. const bundle = await rollup({
  94. input: 'src/themes/index.js',
  95. plugins: [
  96. commonjs(),
  97. resolve({ preferBuiltins: true }),
  98. scss({
  99. outputStyle: 'compressed',
  100. fileName: 'dc.min.css',
  101. }),
  102. ],
  103. })
  104. return bundle.write({
  105. file: 'dist/dc.min.css',
  106. })
  107. }
  108. async function buildModules(options) {
  109. const dcPath = path.join('src', 'DC.js')
  110. const content = await fse.readFile(path.join('src', 'index.js'), 'utf8')
  111. await fse.ensureFile(dcPath)
  112. const exportVersion = `export const VERSION = '${packageJson.version}'`
  113. const cmdOut_content = await fse.readFile(
  114. path.join('src', 'copyright', 'cmdOut.js'),
  115. 'utf8'
  116. )
  117. const exportCmdOut = `
  118. export function __cmdOut() {
  119. ${cmdOut_content
  120. .replace('{{__VERSION__}}', packageJson.version)
  121. .replace('{{__TIME__}}', getTime())
  122. .replace(
  123. '{{__ENGINE_VERSION__}}',
  124. packageJson.devDependencies['@cesium/engine'].replace('^', '')
  125. )
  126. .replace('{{__AUTHOR__}}', packageJson.author)
  127. .replace('{{__HOME_PAGE__}}', packageJson.homepage)
  128. .replace('{{__REPOSITORY__}}', packageJson.repository)}
  129. }`
  130. const exportNamespace = `
  131. export const __namespace = {
  132. Cesium: exports.Cesium
  133. }
  134. `
  135. // Build IIFE
  136. if (options.iife) {
  137. await fse.outputFile(
  138. dcPath,
  139. `
  140. ${content}
  141. ${exportVersion}
  142. ${exportCmdOut}
  143. `,
  144. {
  145. encoding: 'utf8',
  146. }
  147. )
  148. await esbuild.build({
  149. ...buildConfig,
  150. format: 'iife',
  151. globalName: 'DC',
  152. outfile: path.join('dist', 'modules.js'),
  153. })
  154. }
  155. // Build Node、
  156. if (options.node) {
  157. await fse.outputFile(
  158. dcPath,
  159. `
  160. ${content}
  161. ${exportNamespace}
  162. ${exportVersion}
  163. ${exportCmdOut}
  164. `,
  165. {
  166. encoding: 'utf8',
  167. }
  168. )
  169. await esbuild.build({
  170. ...buildConfig,
  171. format: 'cjs',
  172. platform: 'node',
  173. define: {
  174. TransformStream: 'null',
  175. },
  176. outfile: path.join('dist', 'modules.cjs'),
  177. })
  178. }
  179. // remove DC.js
  180. await fse.remove(dcPath)
  181. }
  182. async function combineJs(options) {
  183. // combine for iife
  184. if (options.iife) {
  185. if (options.obfuscate) {
  186. await gulp
  187. .src('dist/modules.js')
  188. .pipe(javascriptObfuscator(obfuscatorConfig))
  189. .pipe(gulp.src('dist/namespace.js'))
  190. .pipe(concat('dc.min.js'))
  191. .pipe(gulp.dest('dist'))
  192. .on('end', () => {
  193. addCopyright(options)
  194. deleteTempFile(options)
  195. })
  196. } else {
  197. await gulp
  198. .src(['dist/modules.js', 'dist/namespace.js'])
  199. .pipe(concat('dc.min.js'))
  200. .pipe(gulp.dest('dist'))
  201. .on('end', () => {
  202. addCopyright(options)
  203. deleteTempFile(options)
  204. })
  205. }
  206. }
  207. // combine for node
  208. if (options.node) {
  209. if (options.obfuscate) {
  210. await gulp
  211. .src('dist/modules.cjs')
  212. .pipe(javascriptObfuscator(obfuscatorConfig))
  213. .pipe(gulp.dest('dist'))
  214. .on('end', async () => {
  215. await gulp
  216. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  217. .pipe(concat('index.cjs'))
  218. .pipe(gulp.dest('dist'))
  219. .on('end', () => {
  220. addCopyright(options)
  221. deleteTempFile(options)
  222. })
  223. })
  224. } else {
  225. await gulp
  226. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  227. .pipe(concat('index.cjs'))
  228. .pipe(gulp.dest('dist'))
  229. .on('end', () => {
  230. addCopyright(options)
  231. deleteTempFile(options)
  232. })
  233. }
  234. }
  235. }
  236. async function copyAssets() {
  237. await fse.emptyDir('dist/resources')
  238. await gulp
  239. .src('./node_modules/@cesium/engine/Build/Workers/**', { nodir: true })
  240. .pipe(gulp.dest('dist/resources/Workers'))
  241. await gulp
  242. .src('./node_modules/@cesium/engine/Source/Assets/**', { nodir: true })
  243. .pipe(gulp.dest('dist/resources/Assets'))
  244. await gulp
  245. .src('./node_modules/@cesium/engine/Source/ThirdParty/**', { nodir: true })
  246. .pipe(gulp.dest('dist/resources/ThirdParty'))
  247. }
  248. async function addCopyright(options) {
  249. let header = await fse.readFile(
  250. path.join('src', 'copyright', 'header.js'),
  251. 'utf8'
  252. )
  253. header = header
  254. .replace('{{__VERSION__}}', packageJson.version)
  255. .replace('{{__AUTHOR__}}', packageJson.author)
  256. .replace('{{__REPOSITORY__}}', packageJson.repository)
  257. if (options.iife) {
  258. let filePath = path.join('dist', 'dc.min.js')
  259. const content = await fse.readFile(filePath, 'utf8')
  260. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  261. }
  262. if (options.node) {
  263. let filePath = path.join('dist', 'index.cjs')
  264. const content = await fse.readFile(filePath, 'utf8')
  265. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  266. }
  267. }
  268. async function deleteTempFile(options) {
  269. if (options.iife) {
  270. await gulp
  271. .src(['dist/namespace.js', 'dist/modules.js'], { read: false })
  272. .pipe(clean())
  273. }
  274. if (options.node) {
  275. await gulp
  276. .src(['dist/namespace.cjs', 'dist/modules.cjs'], { read: false })
  277. .pipe(clean())
  278. }
  279. }
  280. export const build = gulp.series(
  281. () => buildNamespace({ node: true }),
  282. () => buildModules({ node: true }),
  283. () => combineJs({ node: true }),
  284. () => buildNamespace({ iife: true }),
  285. () => buildModules({ iife: true }),
  286. () => combineJs({ iife: true }),
  287. buildCSS,
  288. copyAssets
  289. )
  290. export const buildNode = gulp.series(
  291. () => buildNamespace({ node: true }),
  292. () => buildModules({ node: true }),
  293. () => combineJs({ node: true }),
  294. buildCSS,
  295. copyAssets
  296. )
  297. export const buildIIFE = gulp.series(
  298. () => buildNamespace({ iife: true }),
  299. () => buildModules({ iife: true }),
  300. () => combineJs({ iife: true }),
  301. buildCSS,
  302. copyAssets
  303. )
  304. export const buildRelease = gulp.series(
  305. () => buildNamespace({ node: true }),
  306. () => buildModules({ node: true }),
  307. () => combineJs({ node: true, obfuscate: true }),
  308. () => buildNamespace({ iife: true }),
  309. () => buildModules({ iife: true }),
  310. () => combineJs({ iife: true, obfuscate: true }),
  311. buildCSS,
  312. copyAssets
  313. )
  314. export const server = gulp.series(startServer)