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

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