mirror of
https://github.com/getrebuild/rebuild.git
synced 2025-09-30 02:28:49 +08:00
@rbv js/css compile
This commit is contained in:
parent
c10ee66eb7
commit
28d898da9f
3 changed files with 79 additions and 89 deletions
|
@ -1,9 +1,11 @@
|
|||
const {
|
||||
src,
|
||||
dest,
|
||||
series,
|
||||
parallel
|
||||
} = require('gulp')
|
||||
/*
|
||||
Copyright (c) REBUILD <https://getrebuild.com/> and/or its owners. All rights reserved.
|
||||
|
||||
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
|
||||
See LICENSE and COMMERCIAL in the project root for license information.
|
||||
*/
|
||||
|
||||
const { src, dest, series, parallel } = require('gulp')
|
||||
const babel = require('gulp-babel')
|
||||
const babelCore = require('@babel/core')
|
||||
const cleanCSS = require('gulp-clean-css')
|
||||
|
@ -18,49 +20,42 @@ const filter = require('gulp-filter')
|
|||
const BABEL_OPTIONS = {
|
||||
presets: ['@babel/preset-env', '@babel/preset-react'],
|
||||
plugins: ['@babel/plugin-proposal-class-properties'],
|
||||
minified: true
|
||||
minified: true,
|
||||
}
|
||||
|
||||
const WEB_ROOT = '../src/main/resources/web'
|
||||
const RBV_ROOT = '../@rbv/main/resources/web'
|
||||
const OUT_ROOT = '../target/classes/web'
|
||||
|
||||
function compileJs(cb) {
|
||||
return src(`${WEB_ROOT}/assets/js/**/*.js`)
|
||||
.pipe(
|
||||
babel(BABEL_OPTIONS)
|
||||
)
|
||||
function compileJs(m) {
|
||||
return src(`${m || WEB_ROOT}/assets/js/**/*.js`)
|
||||
.pipe(babel(BABEL_OPTIONS))
|
||||
.pipe(
|
||||
debug({
|
||||
title: 'Compiled : '
|
||||
title: 'Compiled : ',
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
dest(`${OUT_ROOT}/assets/js`)
|
||||
)
|
||||
.pipe(dest(`${OUT_ROOT}/assets/js`))
|
||||
}
|
||||
|
||||
function compileCss(cb) {
|
||||
return src(`${WEB_ROOT}/assets/css/**/*.css`)
|
||||
.pipe(
|
||||
cleanCSS()
|
||||
)
|
||||
function compileCss(m) {
|
||||
return src(`${m || WEB_ROOT}/assets/css/**/*.css`)
|
||||
.pipe(cleanCSS())
|
||||
.pipe(
|
||||
debug({
|
||||
title: 'Compiled : '
|
||||
title: 'Compiled : ',
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
dest(`${OUT_ROOT}/assets/css`)
|
||||
)
|
||||
.pipe(dest(`${OUT_ROOT}/assets/css`))
|
||||
}
|
||||
|
||||
const _assetsHexCached = {}
|
||||
|
||||
function _assetsHex(file) {
|
||||
function _assetsHex(file, m) {
|
||||
let hex = _assetsHexCached[file]
|
||||
if (!hex) {
|
||||
try {
|
||||
hex = revHash(fs.readFileSync(`${WEB_ROOT}${file}`))
|
||||
hex = revHash(fs.readFileSync(`${WEB_ROOT || m}${file}`))
|
||||
} catch (err) {
|
||||
console.log('Cannot #revHash : ' + file, err)
|
||||
// Use date
|
||||
|
@ -72,68 +67,62 @@ function _assetsHex(file) {
|
|||
return hex
|
||||
}
|
||||
|
||||
function compileHtml(cb) {
|
||||
return src(`${WEB_ROOT}/**/*.html`)
|
||||
.pipe(filter(file => !/node_modules/.test(file.path)))
|
||||
function compileHtml(m) {
|
||||
return src(`${WEB_ROOT || m}/**/*.html`)
|
||||
.pipe(filter((file) => !/node_modules/.test(file.path)))
|
||||
.pipe(
|
||||
replace(/<script type="text\/babel">([\s\S]*)<\/script>/igm, (m, p) => {
|
||||
replace(/<script type="text\/babel">([\s\S]*)<\/script>/gim, (m, p) => {
|
||||
if (p.trim().length === 0) return '<!-- No script -->'
|
||||
const min = babelCore.transformSync(p, BABEL_OPTIONS).code
|
||||
return '<script>\n' + min + '\n</script>'
|
||||
const jsmin = babelCore.transformSync(p, BABEL_OPTIONS).code
|
||||
return '<script>\n' + jsmin + '\n</script>'
|
||||
})
|
||||
)
|
||||
.pipe(replace(/ type="text\/babel"/gi, ''))
|
||||
.pipe(
|
||||
replace(/ type="text\/babel"/ig, '')
|
||||
)
|
||||
.pipe(
|
||||
replace(/<script th:src="@\{(.*)\}"><\/script>/ig, (m, p) => {
|
||||
replace(/<script th:src="@\{(.*)\}"><\/script>/gi, (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 th:src="@{' + file + '}"></script>'
|
||||
} else {
|
||||
file += '?v=' + _assetsHex(file.split('?')[0])
|
||||
file += '?v=' + _assetsHex(file.split('?')[0], m)
|
||||
return '<script th:src="@{' + file + '}"></script>'
|
||||
}
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
replace(/<style type="text\/css">([\s\S]*)<\/style>/igm, (m, p) => {
|
||||
replace(/<style type="text\/css">([\s\S]*)<\/style>/gim, (m, 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>'
|
||||
const cssmin = new cleanCSS2({}).minify(p).styles
|
||||
return '<style type="text/css">\n' + cssmin + '\n</style>'
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
replace(/<link rel="stylesheet" type="text\/css" th:href="@\{(.*)\}" \/>/ig, (m, p) => {
|
||||
replace(/<link rel="stylesheet" type="text\/css" th:href="@\{(.*)\}" \/>/gi, (m, p) => {
|
||||
let file = p
|
||||
if (file.includes('/lib/')) {
|
||||
return '<link rel="stylesheet" type="text/css" th:href="@{' + file + '}" />'
|
||||
} else {
|
||||
file += '?v=' + _assetsHex(file.split('?')[0])
|
||||
file += '?v=' + _assetsHex(file.split('?')[0], m)
|
||||
return '<link rel="stylesheet" type="text/css" th:href="@{' + file + '}" />'
|
||||
}
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
debug({
|
||||
title: 'Compiled : '
|
||||
title: 'Compiled : ',
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
dest(OUT_ROOT)
|
||||
)
|
||||
.pipe(dest(OUT_ROOT))
|
||||
}
|
||||
|
||||
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'
|
||||
const mvn = require('child_process').spawnSync(process.platform === 'win32' ? 'mvn.cmd' : 'mvn', ['clean', 'package', '-f', pomfile], {
|
||||
stdio: 'inherit',
|
||||
})
|
||||
|
||||
if (mvn.status !== 0) {
|
||||
|
@ -143,29 +132,15 @@ function maven(cb) {
|
|||
cb()
|
||||
}
|
||||
|
||||
// const RELEASE_HOME = 'D:/GitHub/for-production/rebuild-standalone/REBUILD'
|
||||
exports.default = series(
|
||||
parallel(
|
||||
() => compileJs(WEB_ROOT),
|
||||
() => compileCss(WEB_ROOT),
|
||||
() => compileJs(RBV_ROOT),
|
||||
() => compileCss(RBV_ROOT)
|
||||
),
|
||||
() => compileHtml(WEB_ROOT),
|
||||
() => compileHtml(RBV_ROOT)
|
||||
)
|
||||
|
||||
// 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), compileHtml)
|
||||
exports.mvn = maven
|
||||
// exports.p = series(maven, parallel(compileJs, compileCss), compileHtml, release)
|
|
@ -17,17 +17,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
public class ActionFactory {
|
||||
|
||||
private static final TriggerAction NORBV_FOUND = new TriggerAction() {
|
||||
@Override
|
||||
public ActionType getType() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@Override
|
||||
public void execute(OperatingContext operatingContext) throws TriggerException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
|
@ -49,16 +38,42 @@ public class ActionFactory {
|
|||
* @return
|
||||
*/
|
||||
public static TriggerAction createAction(String type, ActionContext context) {
|
||||
ActionType actionType = null;
|
||||
try {
|
||||
return ActionType.valueOf(type).newInstance(context);
|
||||
actionType = ActionType.valueOf(type);
|
||||
return actionType.newInstance(context);
|
||||
} catch (ClassNotFoundException rbv) {
|
||||
|
||||
if (rbv.getLocalizedMessage().contains(".rbv.")) {
|
||||
return NORBV_FOUND;
|
||||
return new NoRbv(actionType);
|
||||
} else {
|
||||
throw new TriggerException("Unknown trigger type : " + type, rbv);
|
||||
}
|
||||
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw new TriggerException("Unknown trigger type : " + type, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RBV 功能
|
||||
*/
|
||||
private static class NoRbv implements TriggerAction {
|
||||
|
||||
private ActionType actionType;
|
||||
NoRbv(ActionType actionType) {
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionType getType() {
|
||||
if (actionType == null) throw new UnsupportedOperationException();
|
||||
else return actionType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(OperatingContext operatingContext) throws TriggerException {
|
||||
log.warn("Unsupportted @rbv feature");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ public enum ActionType {
|
|||
* @return
|
||||
* @throws ReflectiveOperationException
|
||||
*/
|
||||
public TriggerAction newInstance(ActionContext context) throws ReflectiveOperationException {
|
||||
protected TriggerAction newInstance(ActionContext context) throws ReflectiveOperationException {
|
||||
Class<?> clazz = ClassUtils.getClass(getActionClass());
|
||||
Constructor<?> c = ReflectionUtils.accessibleConstructor(clazz, ActionContext.class);
|
||||
return (TriggerAction) c.newInstance(context);
|
||||
|
|
Loading…
Add table
Reference in a new issue