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

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