-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from getrebuild/develop
merge v1.11.0
- Loading branch information
Showing
325 changed files
with
13,444 additions
and
6,061 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## About | ||
|
||
用于发布到生产环境时编译 js(x)/css 并自动在 jsp 中添加版本号。 | ||
|
||
## How use | ||
|
||
首先安装 `gulp` 及依赖包 | ||
|
||
``` | ||
npm install -g gulp gulp-cli | ||
npm install | ||
``` | ||
|
||
然后执行编译 | ||
|
||
``` | ||
gulp | ||
``` | ||
|
||
> 注意:你可能需要先执行一次 `gulp mvn`,因为 `gulp` 的编译源来自 `maven` 输出目录 `../target/rebuild/` | ||
|
||
编译成功后的文件会输出到 `build` 目录,复制此目录下编译好的文件覆盖到生产环境即可。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
const { | ||
src, | ||
dest, | ||
series, | ||
parallel | ||
} = require('gulp') | ||
const babel = require('gulp-babel') | ||
const babelCore = require('@babel/core') | ||
const cleanCSS = require('gulp-clean-css') | ||
const cleanCSS2 = require('clean-css') | ||
|
||
const fs = require('fs') | ||
const debug = require('gulp-debug') | ||
const revHash = require('rev-hash') | ||
const replace = require('gulp-replace') | ||
const filter = require('gulp-filter') | ||
|
||
const BABEL_OPTIONS = { | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
plugins: ['@babel/plugin-proposal-class-properties'], | ||
minified: true | ||
} | ||
|
||
function compileJs(cb) { | ||
return src('../target/rebuild/assets/js/**/*.js?(x)') | ||
.pipe( | ||
babel(BABEL_OPTIONS) | ||
) | ||
.pipe( | ||
debug({ | ||
title: 'Compiled : ' | ||
}) | ||
) | ||
.pipe( | ||
dest('build/assets/js') | ||
) | ||
} | ||
|
||
function compileCss(cb) { | ||
return src('../target/rebuild/assets/css/**/*.css') | ||
.pipe( | ||
cleanCSS() | ||
) | ||
.pipe( | ||
debug({ | ||
title: 'Compiled : ' | ||
}) | ||
) | ||
.pipe( | ||
dest('build/assets/css') | ||
) | ||
} | ||
|
||
const _assetsHexCached = {} | ||
|
||
function _assetsHex(file) { | ||
let hex = _assetsHexCached[file] | ||
if (!hex) { | ||
try { | ||
hex = revHash(fs.readFileSync(file.replace('${baseUrl}', 'build'))) | ||
} catch (err) { | ||
hex = revHash(fs.readFileSync(file.replace('${pageContext.request.contextPath}', 'build'))) | ||
} | ||
_assetsHexCached[file] = hex | ||
} | ||
return hex | ||
} | ||
|
||
function compileJsp(cb) { | ||
return src('../target/rebuild/**/*.jsp') | ||
.pipe( | ||
replace(/<script type="text\/babel">([\s\S]*)<\/script>/igm, (match, p) => { | ||
if (p.trim().length === 0) return '<!-- No script -->' | ||
const min = babelCore.transformSync(p, BABEL_OPTIONS).code | ||
return '<script>\n' + min + '\n</script>' | ||
}) | ||
) | ||
.pipe( | ||
replace(/ type="text\/babel"/ig, '') | ||
) | ||
.pipe( | ||
replace(/<script src="(.*)"><\/script>/ig, (m, p) => { | ||
let file = p | ||
if (file.includes('/lib/') || file.includes('/language/')) { | ||
if (file.includes('babel')) return '<!-- No Babel -->' | ||
if (file.includes('.development.js')) file = file.replace('.development.js', '.production.min.js') | ||
return '<script src="' + file + '"></script>' | ||
} else { | ||
file = file.replace('.jsx', '.js').split('?')[0] | ||
file += '?v=' + _assetsHex(file) | ||
return '<script src="' + file + '"></script>' | ||
} | ||
}) | ||
) | ||
.pipe( | ||
replace(/<style type="text\/css">([\s\S]*)<\/style>/igm, (match, p) => { | ||
if (p.trim().length === 0) return '<!-- No style -->' | ||
const min = new cleanCSS2({}).minify(p).styles | ||
return '<style type="text/css">\n' + min + '\n</style>' | ||
}) | ||
) | ||
.pipe( | ||
replace(/<link rel="stylesheet" type="text\/css" href="(.*)">/ig, (match, p) => { | ||
let file = p | ||
if (file.includes('/lib/')) { | ||
return '<link rel="stylesheet" type="text/css" href="' + file + '">' | ||
} else { | ||
file += '?v=' + _assetsHex(file.split('?')[0]) | ||
return '<link rel="stylesheet" type="text/css" href="' + file + '">' | ||
} | ||
}) | ||
) | ||
.pipe( | ||
debug({ | ||
title: 'Compiled : ' | ||
}) | ||
) | ||
.pipe( | ||
dest('build') | ||
) | ||
} | ||
|
||
function maven(cb) { | ||
const pomfile = `${__dirname}/../pom.xml` | ||
console.log('Using pom.xml : ' + pomfile) | ||
|
||
const mvn = require('child_process').spawnSync( | ||
process.platform === 'win32' ? 'mvn.cmd' : 'mvn', | ||
['clean', 'package', '-f', pomfile], { | ||
stdio: 'inherit' | ||
}) | ||
|
||
if (mvn.status !== 0) { | ||
process.stderr.write(mvn.stderr) | ||
process.exit(mvn.status) | ||
} | ||
cb() | ||
} | ||
|
||
const RELEASE_HOME = 'D:/GitHub/for-production/rebuild-standalone/REBUILD' | ||
|
||
function release(cb) { | ||
return src('../target/rebuild/**') | ||
.pipe( | ||
filter((file) => { | ||
const m = /\.jsx/.test(file.path) || /\.development\./.test(file.path) || /babel\./.test(file.path) || | ||
/rebel\.xml/.test(file.path) | ||
m && console.log('Filtered : ' + file.path) | ||
return !m | ||
}) | ||
) | ||
.pipe( | ||
dest(RELEASE_HOME) | ||
) | ||
.on('end', () => { | ||
src('build/**') | ||
.pipe( | ||
dest(RELEASE_HOME) | ||
) | ||
}) | ||
} | ||
|
||
exports.default = series(parallel(compileJs, compileCss), compileJsp) | ||
exports.p = series(maven, parallel(compileJs, compileCss), compileJsp, release) | ||
exports.mvn = maven |
Oops, something went wrong.