Skip to content

Commit

Permalink
fix: 非编辑页面切换Code方式时提示保存 TencentBlueKing#10933
Browse files Browse the repository at this point in the history
  • Loading branch information
lockiechen committed Sep 29, 2024
1 parent fb0e90e commit b48d2a0
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 128 deletions.
50 changes: 37 additions & 13 deletions src/frontend/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const svgSprite = require('gulp-svg-sprite')
const inject = require('gulp-inject')
const rename = require('gulp-rename')
const hash = require('gulp-hash')
const { globSync } = require('glob')
const replace = require('gulp-replace')
const Ora = require('ora')
const yargs = require('yargs')
Expand All @@ -15,20 +16,24 @@ const argv = yargs.alias({
dist: 'd',
env: 'e',
lsVersion: 'l',
scope: 's'
scope: 's',
effect: 'effect'
}).default({
dist: 'frontend',
env: 'master',
lsVersion: 'dev'
lsVersion: 'dev',
effect: false
}).describe({
dist: 'build output dist directory',
env: 'environment [dev, test, master, external]',
lsVersion: 'localStorage version',
head: 'head file path',
base: 'base file path'
base: 'base file path',
effect: 'only buuild effected service'
}).argv
const { dist, env, lsVersion, scope, head = 'HEAD', base = 'master' } = argv
const { dist, env, lsVersion, scope, head = 'HEAD', base = 'master', effect = false } = argv
console.log(env, head, base)
const FINAL_ASSETS_JSON_FILENAME = `${dist}/assetsBundles.js`
const svgSpriteConfig = {
mode: {
symbol: true
Expand Down Expand Up @@ -88,7 +93,7 @@ task('build', series(cb => {
const spinner = new Ora('building bk-ci frontend project').start()
const scopeStr = getScopeStr(scope)

const cmd = scopeStr ? `run-many -t public:master ${scopeStr}` : 'affected -t public:master'
const cmd = effect ? 'affected -t public:master' : `run-many -t public:master ${scopeStr}`
console.log('gulp cmd: ', cmd, cmd.split(' '))
const { spawn } = require('node:child_process')
const spawnCmd = spawn('pnpm', [
Expand Down Expand Up @@ -116,18 +121,37 @@ task('build', series(cb => {
})
}, (cb) => {
try {
const fileContent = `window.SERVICE_ASSETS = ${fs.readFileSync(`${dist}/assets_bundle.json`, 'utf8')}`
fs.writeFileSync(`${dist}/assetsBundles.js`, fileContent)
return src(`${dist}/assetsBundles.js`, {
allowEmpty: true
})
.pipe(hash())
.pipe(dest(`${dist}/`))
const entryDir = path.join(__dirname, dist, "entry's")

// 读取path.join(__dirname, dist, 'entry's', '*.json')所有Json合并成一个
const finalAssets = globSync(path.join(entryDir, '*.json')).reduce((acc, file) => {
const content = JSON.parse(fs.readFileSync(file, 'utf-8'))
acc = {
...acc,
...content
}
return acc
}, {})

console.log('final assets json!')
console.table(finalAssets)
const fileContent = `window.SERVICE_ASSETS = ${JSON.stringify(finalAssets)}`

fs.writeFileSync(FINAL_ASSETS_JSON_FILENAME, fileContent)
if (fs.existsSync(entryDir)) {
fs.rmSync(entryDir, {
recursive: true,
force: true
})
}
return src(FINAL_ASSETS_JSON_FILENAME).pipe(hash()).pipe(dest(dist))

} catch (error) {
console.log('build assetsBundles.js failed')
console.error(error)
process.exit(1)
}
cb()

}, (cb) => {
['console', 'pipeline'].map(prefix => {
const dir = path.join(dist, prefix)
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"^public:master"
],
"outputs": [
"{projectRoot}/dist"
"{projectRoot}/dist",
"{workspaceRoot}/frontend"
],
"cache": true
},
Expand Down
11 changes: 6 additions & 5 deletions src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"devDependencies": {
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^16.2.4",
"@nx/eslint": "19.5.1",
"@nx/vite": "19.5.1",
"@nx/webpack": "19.5.1",
"@typescript-eslint/parser": "^1.9.0",
"@webpack-cli/serve": "^1.7.0",
"chalk": "^4.0.0",
Expand All @@ -13,6 +16,7 @@
"del": "6.1.1",
"eslint": "^7.3.1",
"eslint-plugin-vue": "6.2.2",
"glob": "^11.0.0",
"glob-parent": "5.1.2",
"gulp": "^4.0.2",
"gulp-hash": "^4.2.2",
Expand All @@ -25,18 +29,15 @@
"husky": "^7.0.2",
"lint-staged": "^10.2.11",
"node-fetch": "^2.6.0",
"nx": "19.5.1",
"ora": "^3.4.0",
"svg-sprite-loader": "^6.0.9",
"typescript": "3.4.5",
"vue-eslint-parser": "^9.0.2",
"webpack": "~5.76.1",
"webpack-bundle-analyzer": "^4.4.2",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.3.1",
"nx": "19.5.1",
"@nx/eslint": "19.5.1",
"@nx/webpack": "19.5.1",
"@nx/vite": "19.5.1"
"webpack-dev-server": "^4.3.1"
},
"resolutions": {
"glob-parent": "5.1.2"
Expand Down
Loading

0 comments on commit b48d2a0

Please sign in to comment.