選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

gulpfile.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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: `es2019`,
  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. Supercluster: exports.Supercluster
  147. }
  148. `
  149. // Build IIFE
  150. if (options.iife) {
  151. await fse.outputFile(
  152. dcPath,
  153. `
  154. ${exportVersion}
  155. ${exportCmdOut}
  156. ${content}
  157. `,
  158. {
  159. encoding: 'utf8',
  160. }
  161. )
  162. await esbuild.build({
  163. ...buildConfig,
  164. format: 'iife',
  165. globalName: 'DC',
  166. outfile: path.join('dist', 'modules.js'),
  167. })
  168. }
  169. // Build Node、
  170. if (options.node) {
  171. await fse.outputFile(
  172. dcPath,
  173. `
  174. ${exportNamespace}
  175. ${exportVersion}
  176. ${exportCmdOut}
  177. ${content}
  178. `,
  179. {
  180. encoding: 'utf8',
  181. }
  182. )
  183. await esbuild.build({
  184. ...buildConfig,
  185. format: 'cjs',
  186. platform: 'node',
  187. define: {
  188. TransformStream: 'null',
  189. },
  190. outfile: path.join('dist', 'modules.cjs'),
  191. })
  192. }
  193. // remove DC.js
  194. await fse.remove(dcPath)
  195. }
  196. async function combineJs(options) {
  197. // combine for iife
  198. if (options.iife) {
  199. if (options.obfuscate) {
  200. await gulp
  201. .src('dist/modules.js')
  202. .pipe(javascriptObfuscator(obfuscatorConfig))
  203. .pipe(gulp.src('dist/namespace.js'))
  204. .pipe(concat('dc.min.js'))
  205. .pipe(gulp.dest('dist'))
  206. .on('end', () => {
  207. addCopyright(options)
  208. deleteTempFile(options)
  209. })
  210. } else {
  211. await gulp
  212. .src(['dist/modules.js', 'dist/namespace.js'])
  213. .pipe(concat('dc.min.js'))
  214. .pipe(gulp.dest('dist'))
  215. .on('end', () => {
  216. addCopyright(options)
  217. deleteTempFile(options)
  218. })
  219. }
  220. }
  221. // combine for node
  222. if (options.node) {
  223. if (options.obfuscate) {
  224. await gulp
  225. .src('dist/modules.cjs')
  226. .pipe(javascriptObfuscator(obfuscatorConfig))
  227. .pipe(gulp.dest('dist'))
  228. .on('end', async () => {
  229. await gulp
  230. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  231. .pipe(concat('index.cjs'))
  232. .pipe(gulp.dest('dist'))
  233. .on('end', () => {
  234. addCopyright(options)
  235. deleteTempFile(options)
  236. })
  237. })
  238. } else {
  239. await gulp
  240. .src(['dist/namespace.cjs', 'dist/modules.cjs'])
  241. .pipe(concat('index.cjs'))
  242. .pipe(gulp.dest('dist'))
  243. .on('end', () => {
  244. addCopyright(options)
  245. deleteTempFile(options)
  246. })
  247. }
  248. }
  249. }
  250. async function copyAssets() {
  251. await fse.emptyDir('dist/resources')
  252. await gulp
  253. .src('./node_modules/@cesium/engine/Build/Workers/**', { nodir: true })
  254. .pipe(gulp.dest('dist/resources/Workers'))
  255. await gulp
  256. .src('./node_modules/@cesium/engine/Source/Assets/**', { nodir: true })
  257. .pipe(gulp.dest('dist/resources/Assets'))
  258. await gulp
  259. .src('./node_modules/@cesium/engine/Source/ThirdParty/**', { nodir: true })
  260. .pipe(gulp.dest('dist/resources/ThirdParty'))
  261. }
  262. async function addCopyright(options) {
  263. let header = await fse.readFile(
  264. path.join('src', 'copyright', 'header.js'),
  265. 'utf8'
  266. )
  267. header = header
  268. .replace('{{__VERSION__}}', packageJson.version)
  269. .replace('{{__AUTHOR__}}', packageJson.author)
  270. .replace('{{__REPOSITORY__}}', packageJson.repository)
  271. if (options.iife) {
  272. let filePath = path.join('dist', 'dc.min.js')
  273. const content = await fse.readFile(filePath, 'utf8')
  274. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  275. }
  276. if (options.node) {
  277. let filePath = path.join('dist', 'index.cjs')
  278. const content = await fse.readFile(filePath, 'utf8')
  279. await fse.outputFile(filePath, `${header}${content}`, { encoding: 'utf8' })
  280. }
  281. }
  282. async function deleteTempFile(options) {
  283. if (options.iife) {
  284. await gulp
  285. .src(['dist/namespace.js', 'dist/modules.js'], { read: false })
  286. .pipe(clean())
  287. }
  288. if (options.node) {
  289. await gulp
  290. .src(['dist/namespace.cjs', 'dist/modules.cjs'], { read: false })
  291. .pipe(clean())
  292. }
  293. }
  294. export const build = gulp.series(
  295. () => buildNamespace({ node: true }),
  296. () => buildModules({ node: true }),
  297. () => combineJs({ node: true }),
  298. () => buildNamespace({ iife: true }),
  299. () => buildModules({ iife: true }),
  300. () => combineJs({ iife: true }),
  301. buildCSS,
  302. copyAssets
  303. )
  304. export const buildNode = gulp.series(
  305. () => buildNamespace({ node: true }),
  306. () => buildModules({ node: true }),
  307. () => combineJs({ node: true }),
  308. buildCSS,
  309. copyAssets
  310. )
  311. export const buildIIFE = gulp.series(
  312. () => buildNamespace({ iife: true }),
  313. () => buildModules({ iife: true }),
  314. () => combineJs({ iife: true }),
  315. buildCSS,
  316. copyAssets
  317. )
  318. export const buildRelease = gulp.series(
  319. () => buildNamespace({ node: true }),
  320. () => buildModules({ node: true }),
  321. () => combineJs({ node: true, obfuscate: true }),
  322. () => buildNamespace({ iife: true }),
  323. () => buildModules({ iife: true }),
  324. () => combineJs({ iife: true, obfuscate: true }),
  325. buildCSS,
  326. copyAssets
  327. )
  328. export const server = gulp.series(startServer)