Browse Source

add dev mode

tags/3.2.0
Caven Chen 2 years ago
parent
commit
94be18c3ac
2 changed files with 76 additions and 22 deletions
  1. 74
    22
      gulpfile.js
  2. 2
    0
      package.json

+ 74
- 22
gulpfile.js View File

/** /**
@author : Caven Chen @author : Caven Chen
@date : 2023-05-06
**/ **/


'use strict' 'use strict'
import startServer from './server.js' import startServer from './server.js'
import inlineImage from 'esbuild-plugin-inline-image' import inlineImage from 'esbuild-plugin-inline-image'
import { glsl } from 'esbuild-plugin-glsl' import { glsl } from 'esbuild-plugin-glsl'
import chokidar from 'chokidar'
import shell from 'shelljs'
import chalk from 'chalk'


const obfuscatorConfig = { const obfuscatorConfig = {
compact: true, //压缩代码 compact: true, //压缩代码


const packageJson = fse.readJsonSync('./package.json') const packageJson = fse.readJsonSync('./package.json')


const plugins = [
resolve({ preferBuiltins: true }),
commonjs(),
babel({
babelHelpers: 'runtime',
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
browsers: ['> 1%', 'last 2 versions', 'ie >= 10'],
},
},
],
],
plugins: ['@babel/plugin-transform-runtime'],
}),
]

function getTime() { function getTime() {
let now = new Date() let now = new Date()
let m = now.getMonth() + 1 let m = now.getMonth() + 1
async function buildNamespace(options) { async function buildNamespace(options) {
const bundle = await rollup({ const bundle = await rollup({
input: 'libs/index.js', input: 'libs/index.js',
plugins: [
commonjs(),
resolve({ preferBuiltins: true }),
babel({
babelHelpers: 'runtime',
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
browsers: ['> 1%', 'last 2 versions', 'ie >= 10'],
},
},
],
],
plugins: ['@babel/plugin-transform-runtime'],
}),
terser(),
],
plugins: [...plugins, ...[options.dev ? null : terser()]],
onwarn: (message) => { onwarn: (message) => {
// Ignore eval warnings in third-party code we don't have control over // Ignore eval warnings in third-party code we don't have control over
if (message.code === 'EVAL' && /protobufjs/.test(message.loc.file)) { if (message.code === 'EVAL' && /protobufjs/.test(message.loc.file)) {
const exportNamespace = ` const exportNamespace = `
export const __namespace = { export const __namespace = {
Cesium: exports.Cesium, Cesium: exports.Cesium,
Supercluster: exports.Supercluster
Supercluster: exports.Supercluster,
CryptoJS: exports.CryptoJS
} }
` `


} }
} }


async function regenerate(option, content) {
await fse.remove('dist/dc.min.js')
await fse.remove('dist/dc.min.css')
await fse.outputFile(path.join('dist', 'namespace.js'), content)
await buildModules(option)
await buildCSS()
await gulp
.src(['dist/modules.js', 'dist/namespace.js'])
.pipe(concat('dc.min.js'))
.pipe(gulp.dest('dist'))
.on('end', () => {
addCopyright(option)
deleteTempFile(option)
})
}

export const build = gulp.series( export const build = gulp.series(
() => buildNamespace({ node: true }), () => buildNamespace({ node: true }),
() => buildModules({ node: true }), () => buildModules({ node: true }),
copyAssets copyAssets
) )


export const dev = gulp.series(
() => buildNamespace({ dev: true }),
copyAssets,
() => {
shell.echo(chalk.yellow('============= start dev =============='))
const watcher = chokidar.watch('src', {
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100,
},
})
fse.readFile(path.join('dist', 'namespace.js'), 'utf8').then((content) => {
watcher
.on('ready', async () => {
await regenerate({ iife: true }, content)
await startServer()
})
.on('change', async () => {
let now = new Date()
await regenerate({ iife: true }, content)
shell.echo(
chalk.green(
`regenerate lib takes ${new Date().getTime() - now.getTime()} ms`
)
)
})
})
return watcher
}
)

export const server = gulp.series(startServer) export const server = gulp.series(startServer)

+ 2
- 0
package.json View File

"type": "module", "type": "module",
"main": "dist/index.cjs", "main": "dist/index.cjs",
"scripts": { "scripts": {
"dev": "rimraf dist && gulp dev",
"build": "rimraf dist && gulp build", "build": "rimraf dist && gulp build",
"build:node": "rimraf dist && gulp buildNode", "build:node": "rimraf dist && gulp buildNode",
"build:iife": "rimraf dist && gulp buildIIFE", "build:iife": "rimraf dist && gulp buildIIFE",
"@rollup/plugin-node-resolve": "^15.0.2", "@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-terser": "^0.4.1", "@rollup/plugin-terser": "^0.4.1",
"chalk": "^5.2.0", "chalk": "^5.2.0",
"chokidar": "^3.5.3",
"esbuild": "^0.17.18", "esbuild": "^0.17.18",
"esbuild-plugin-glsl": "^1.2.1", "esbuild-plugin-glsl": "^1.2.1",
"esbuild-plugin-inline-image": "^0.0.9", "esbuild-plugin-inline-image": "^0.0.9",

Loading…
Cancel
Save