mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-21 01:46:28 +08:00
ui build
This commit is contained in:
1 parent
9c45527522
commit
7dc31d02fa
15 files changed
+10521
-3
No files matched your search
@@ -0,0 +1,90 @@
|
||||
/* eslint-disable */
|
||||
import _ from 'lodash';
|
||||
import moment from 'moment';
|
||||
import gulp from 'gulp';
|
||||
import log from 'fancy-log';
|
||||
import chalk from 'chalk';
|
||||
import svgmin from 'gulp-svgmin';
|
||||
import vinylBuffer from 'vinyl-buffer';
|
||||
import iconfont from 'gulp-iconfont';
|
||||
import nunjucks from 'gulp-nunjucks';
|
||||
import plumber from 'gulp-plumber';
|
||||
import gulpif from 'gulp-if';
|
||||
import vjGenerateLocales from '../plugins/gulpGenerateLocales';
|
||||
import vjTouch from '../plugins/gulpTouch';
|
||||
|
||||
let isInWatchMode = false;
|
||||
export let tasks = {};
|
||||
const iconTimestamp = moment.utc([2017, 0, 1, 0, 0, 0, 0]).unix();
|
||||
|
||||
function handleWatchChange(name, r = 300) {
|
||||
return _.debounce((ev) => {
|
||||
log('File %s: %s', chalk.yellow(ev.type), ev.path);
|
||||
tasks[name]();
|
||||
}, r);
|
||||
}
|
||||
|
||||
function offsetMtimeAtFirstBuild() {
|
||||
// Offset the mtime at first build to
|
||||
// workaround webpack/watchpack issue #25.
|
||||
return gulpif(!isInWatchMode, vjTouch(~~((Date.now() - 30 * 1000) / 1000)));
|
||||
}
|
||||
|
||||
export default function ({ watch, production, errorHandler }) {
|
||||
let iconfontTemplateArgs = null;
|
||||
|
||||
tasks['iconfont:template'] = () => gulp
|
||||
.src('misc/icons/template/*.styl')
|
||||
.pipe(nunjucks.compile(iconfontTemplateArgs))
|
||||
.pipe(gulp.dest('misc/.iconfont'))
|
||||
.pipe(offsetMtimeAtFirstBuild());
|
||||
|
||||
|
||||
tasks.iconfont = () => gulp
|
||||
.src('misc/icons/*.svg')
|
||||
.pipe(plumber({ errorHandler }))
|
||||
.pipe(svgmin())
|
||||
.pipe(gulp.dest('misc/icons'))
|
||||
.pipe(offsetMtimeAtFirstBuild())
|
||||
.pipe(iconfont({
|
||||
fontHeight: 1000,
|
||||
prependUnicode: false,
|
||||
descent: 6.25 / 100 * 1000,
|
||||
fontName: 'vj4icon',
|
||||
formats: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
|
||||
timestamp: iconTimestamp,
|
||||
}))
|
||||
.on('glyphs', (glyphs, options) => {
|
||||
iconfontTemplateArgs = { glyphs, options };
|
||||
tasks['iconfont:template']();
|
||||
})
|
||||
.pipe(gulp.dest('misc/.iconfont'))
|
||||
.pipe(vinylBuffer())
|
||||
.pipe(offsetMtimeAtFirstBuild());
|
||||
|
||||
tasks.locale = () => gulp
|
||||
.src('../locales/*.yaml')
|
||||
.pipe(plumber({ errorHandler }))
|
||||
.pipe(vjGenerateLocales())
|
||||
.pipe(gulp.dest('static/locale'))
|
||||
.pipe(offsetMtimeAtFirstBuild());
|
||||
|
||||
tasks.watch = () => {
|
||||
isInWatchMode = true;
|
||||
gulp.watch('misc/icons/*.svg', handleWatchChange('iconfont'));
|
||||
gulp.watch('../locales/*.yaml', handleWatchChange('locale'));
|
||||
};
|
||||
|
||||
for (const key in tasks) {
|
||||
// gulp4 uses function name directly as task name
|
||||
Object.defineProperty(tasks[key], 'name', {
|
||||
value: key,
|
||||
configurable: true,
|
||||
});
|
||||
tasks[key] = gulp.series(tasks[key]);
|
||||
}
|
||||
|
||||
tasks.default = gulp.series(gulp.parallel(tasks.iconfont, tasks.locale));
|
||||
|
||||
return tasks;
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
/* eslint-disable */
|
||||
import _ from 'lodash';
|
||||
|
||||
import webpack from 'webpack';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
import FriendlyErrorsPlugin from 'friendly-errors-webpack-plugin';
|
||||
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin';
|
||||
import ExtractTextPlugin from 'extract-text-webpack-plugin';
|
||||
import CopyWebpackPlugin from 'copy-webpack-plugin';
|
||||
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
|
||||
import StaticManifestPlugin from '../plugins/webpackStaticManifestPlugin.js';
|
||||
import DummyOutputPlugin from '../plugins/webpackDummyOutputPlugin.js';
|
||||
import mapWebpackUrlPrefix from '../utils/mapWebpackUrlPrefix.js';
|
||||
import root from '../utils/root.js';
|
||||
|
||||
const extractProjectCSS = new ExtractTextPlugin({ filename: 'vj4.css?[sha1:contenthash:hex:10]', allChunks: true });
|
||||
const extractVendorCSS = new ExtractTextPlugin({ filename: 'vendors.css?[sha1:contenthash:hex:10]', allChunks: true });
|
||||
|
||||
const beautifyOutputUrl = mapWebpackUrlPrefix([
|
||||
{ prefix: 'node_modules/katex/dist/', replace: 'katex/' },
|
||||
{ prefix: 'misc/.iconfont', replace: 'ui/iconfont' },
|
||||
]);
|
||||
|
||||
export default function (env = {}) {
|
||||
function eslintLoader() {
|
||||
return {
|
||||
loader: 'eslint-loader',
|
||||
options: {
|
||||
configFile: root('.eslintrc.js'),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function babelLoader() {
|
||||
let cacheDirectory = root('.cache/babel');
|
||||
try {
|
||||
fs.ensureDirSync(cacheDirectory);
|
||||
} catch (ignored) {
|
||||
cacheDirectory = false;
|
||||
}
|
||||
return {
|
||||
loader: 'babel-loader',
|
||||
options: {
|
||||
...require(root('package.json')).babelForProject,
|
||||
cacheDirectory,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function jsonLoader() {
|
||||
return 'json-loader';
|
||||
}
|
||||
|
||||
function postcssLoader() {
|
||||
return {
|
||||
loader: 'postcss-loader',
|
||||
options: {
|
||||
sourceMap: env.production,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function styleLoader() {
|
||||
return 'style-loader';
|
||||
}
|
||||
|
||||
function cssLoader() {
|
||||
return 'css-loader?importLoaders=1';
|
||||
}
|
||||
|
||||
function stylusLoader() {
|
||||
return 'stylus-loader';
|
||||
}
|
||||
|
||||
function fileLoader() {
|
||||
return {
|
||||
loader: 'file-loader',
|
||||
options: {
|
||||
name: '[path][name].[ext]?[sha1:hash:hex:10]',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const config = {
|
||||
bail: true,
|
||||
profile: true,
|
||||
context: root(),
|
||||
devtool: env.production ? 'source-map' : false,
|
||||
entry: {
|
||||
vj4: './Entry.js',
|
||||
},
|
||||
output: {
|
||||
path: root('../.uibuild'),
|
||||
publicPath: '/', // overwrite in entry.js
|
||||
hashFunction: 'sha1',
|
||||
hashDigest: 'hex',
|
||||
hashDigestLength: 10,
|
||||
filename: '[name].js?[chunkhash]',
|
||||
chunkFilename: '[name].chunk.js?[chunkhash]',
|
||||
},
|
||||
resolve: {
|
||||
modules: [
|
||||
root('node_modules'),
|
||||
],
|
||||
alias: {
|
||||
vj: root(),
|
||||
},
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.jsx?$/,
|
||||
exclude: /node_modules[\/\\]/,
|
||||
enforce: 'pre',
|
||||
use: [eslintLoader()],
|
||||
},
|
||||
{
|
||||
// fonts and images
|
||||
test: /\.(svg|ttf|eot|woff|woff2|png|jpg|jpeg|gif)$/,
|
||||
use: [fileLoader()],
|
||||
},
|
||||
{
|
||||
// ES2015 scripts
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules[\/\\]/,
|
||||
use: [babelLoader()],
|
||||
},
|
||||
{
|
||||
test: /\.json$/,
|
||||
use: [jsonLoader()],
|
||||
},
|
||||
{
|
||||
// fix pickadate loading
|
||||
test: /pickadate/,
|
||||
use: [
|
||||
{
|
||||
loader: 'imports-loader',
|
||||
options: { define: '>false' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
// project stylus stylesheets
|
||||
test: /\.styl$/,
|
||||
use: env.watch
|
||||
? [styleLoader(), cssLoader(), postcssLoader(), stylusLoader()]
|
||||
: extractProjectCSS.extract([cssLoader(), postcssLoader(), stylusLoader()])
|
||||
,
|
||||
},
|
||||
{
|
||||
// vendors stylesheets
|
||||
test: /\.css$/,
|
||||
include: /node_modules[\/\\]/,
|
||||
use: env.watch
|
||||
? [styleLoader(), cssLoader()]
|
||||
: extractVendorCSS.extract([cssLoader()])
|
||||
,
|
||||
},
|
||||
{
|
||||
// project stylesheets
|
||||
test: /\.css$/,
|
||||
exclude: /node_modules[\/\\]/,
|
||||
use: env.watch
|
||||
? [styleLoader(), cssLoader(), postcssLoader()]
|
||||
: extractProjectCSS.extract([cssLoader(), postcssLoader()])
|
||||
,
|
||||
},
|
||||
],
|
||||
},
|
||||
plugins: [
|
||||
|
||||
new webpack.ProvidePlugin({
|
||||
$: 'jquery',
|
||||
jQuery: 'jquery',
|
||||
'window.jQuery': 'jquery',
|
||||
katex: 'katex/dist/katex.js',
|
||||
React: 'react',
|
||||
}),
|
||||
|
||||
new FriendlyErrorsPlugin(),
|
||||
|
||||
// don't include momentjs locale files
|
||||
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
||||
|
||||
// don't include timeago.js locale files
|
||||
// no use. FIXME.
|
||||
// new webpack.IgnorePlugin(/locales\//, /timeago/),
|
||||
|
||||
// extract stylesheets into a standalone file
|
||||
env.watch
|
||||
? new DummyOutputPlugin('vendors.css')
|
||||
: extractVendorCSS,
|
||||
|
||||
env.watch
|
||||
? new DummyOutputPlugin('vj4.css')
|
||||
: extractProjectCSS,
|
||||
|
||||
// extract 3rd-party JavaScript libraries into a standalone file
|
||||
env.watch
|
||||
? new DummyOutputPlugin('vendors.js')
|
||||
: new webpack.optimize.CommonsChunkPlugin({
|
||||
name: 'vendors',
|
||||
minChunks: (module, count) => (
|
||||
module.resource
|
||||
&& module.resource.indexOf(root()) === -1
|
||||
&& module.resource.match(/\.jsx?$/)
|
||||
),
|
||||
}),
|
||||
|
||||
// extract manifest into a standalone file
|
||||
env.watch
|
||||
? new DummyOutputPlugin('manifest.js')
|
||||
: new webpack.optimize.CommonsChunkPlugin({
|
||||
name: 'manifest',
|
||||
}),
|
||||
|
||||
new webpack.optimize.CommonsChunkPlugin({
|
||||
children: true,
|
||||
async: true,
|
||||
minChunks: 2,
|
||||
}),
|
||||
|
||||
// copy static assets
|
||||
new CopyWebpackPlugin([{ from: root('static') }]),
|
||||
|
||||
// copy emoji images
|
||||
new CopyWebpackPlugin([{ from: root('node_modules/emojify.js/dist/images/basic'), to: 'img/emoji/' }]),
|
||||
|
||||
// Options are provided by LoaderOptionsPlugin until webpack#3136 is fixed
|
||||
new webpack.LoaderOptionsPlugin({
|
||||
test: /\.styl$/,
|
||||
stylus: {
|
||||
default: {
|
||||
preferPathResolver: 'webpack',
|
||||
use: [
|
||||
require('rupture')(),
|
||||
],
|
||||
import: [
|
||||
'~vj/common/common.inc.styl',
|
||||
],
|
||||
},
|
||||
},
|
||||
}),
|
||||
|
||||
// Make sure process.env.NODE_ENV === 'production' in production mode
|
||||
new webpack.DefinePlugin({
|
||||
'process.env': {
|
||||
NODE_ENV: env.production ? '"production"' : '"debug"',
|
||||
},
|
||||
}),
|
||||
|
||||
env.production
|
||||
? new UglifyJsPlugin({ sourceMap: true })
|
||||
: function () { },
|
||||
env.production
|
||||
? new OptimizeCssAssetsPlugin()
|
||||
: function () { },
|
||||
env.production
|
||||
? new webpack.optimize.ModuleConcatenationPlugin()
|
||||
: function () { },
|
||||
env.production
|
||||
? new webpack.LoaderOptionsPlugin({ minimize: true })
|
||||
: function () { },
|
||||
// Replace Module Id with hash or name
|
||||
env.production
|
||||
? new webpack.HashedModuleIdsPlugin()
|
||||
: new webpack.NamedModulesPlugin(),
|
||||
|
||||
new webpack.LoaderOptionsPlugin({
|
||||
options: {
|
||||
context: root(),
|
||||
|
||||
// Beautify the output path of assets
|
||||
customInterpolateName: (url, name, options) => beautifyOutputUrl(url),
|
||||
},
|
||||
}),
|
||||
|
||||
// Finally, output asset hashes
|
||||
new StaticManifestPlugin({
|
||||
fileName: 'static-manifest.json',
|
||||
ignore: [
|
||||
'img/emoji/',
|
||||
'katex/',
|
||||
],
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
||||
return config;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
/* eslint-disable */
|
||||
require('@babel/register');
|
||||
require('./main.js');
|
||||
@@ -0,0 +1,13 @@
|
||||
/* eslint-disable */
|
||||
import { argv } from 'yargs';
|
||||
import root from './utils/root';
|
||||
import runGulp from './runGulp';
|
||||
import runWebpack from './runWebpack';
|
||||
|
||||
async function main() {
|
||||
await runGulp(argv);
|
||||
runWebpack(argv);
|
||||
}
|
||||
|
||||
process.chdir(root());
|
||||
main();
|
||||
@@ -0,0 +1,32 @@
|
||||
/* eslint-disable */
|
||||
import yaml from 'js-yaml';
|
||||
|
||||
import PluginError from 'plugin-error';
|
||||
import through from 'through2';
|
||||
import path from 'path';
|
||||
|
||||
export default function generateLocales() {
|
||||
function bufferContents(file, encoding, callback) {
|
||||
if (file.isNull()) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
if (file.isStream()) {
|
||||
this.emit('error', new PluginError('gulpGenerateLocales', 'Stream not supported'));
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
const doc = yaml.safeLoad(file.contents);
|
||||
file.contents = Buffer.from(`window.LOCALES = ${JSON.stringify(doc, null, 2)}`);
|
||||
file.path = path.join(
|
||||
path.dirname(file.path),
|
||||
`${path.basename(file.path, path.extname(file.path))}.js`
|
||||
);
|
||||
|
||||
this.push(file);
|
||||
callback();
|
||||
}
|
||||
|
||||
return through.obj(bufferContents);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/* eslint-disable */
|
||||
import PluginError from 'plugin-error';
|
||||
import through from 'through2';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
export default function touch(mtime) {
|
||||
async function touchFile(file) {
|
||||
const fd = await fs.open(file.path, 'a');
|
||||
try {
|
||||
await fs.futimes(fd, mtime, mtime);
|
||||
} finally {
|
||||
await fs.close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
function processStream(file, encoding, callback) {
|
||||
if (file.isNull()) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
if (file.isStream()) {
|
||||
this.emit('error', new PluginError('gulpTouch', 'Stream not supported'));
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
touchFile(file)
|
||||
.catch(err => this.emit('error', err))
|
||||
.then(() => {
|
||||
this.push(file);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
return through.obj(processStream);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/* eslint-disable */
|
||||
export default class DummyOutputPlugin {
|
||||
constructor(file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
compiler.plugin('emit', (compilation, callback) => {
|
||||
compilation.assets[this.file] = {
|
||||
source: () => '',
|
||||
size: () => 0,
|
||||
};
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* eslint-disable */
|
||||
const _ = require('lodash');
|
||||
const crypto = require('crypto');
|
||||
|
||||
export default class StaticManifestPlugin {
|
||||
constructor({ fileName, ignore }) {
|
||||
this.fileName = fileName;
|
||||
this.ignore = ignore;
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
compiler.plugin('emit', (compilation, callback) => {
|
||||
const stats = compilation.getStats().toJson();
|
||||
const manifest = _(stats.assets)
|
||||
.map((asset) => {
|
||||
const { name } = asset;
|
||||
// Skip files listed in ignore
|
||||
if (_.some(this.ignore, ignorePattern => name.indexOf(ignorePattern) > -1)) {
|
||||
return null;
|
||||
}
|
||||
// Skip calculating hash for names like ?xxxx
|
||||
if (name.indexOf('?') > -1) {
|
||||
return [
|
||||
name.replace(/\?[\s\S]*/, ''), // origin name
|
||||
name, // name with hash
|
||||
];
|
||||
}
|
||||
// We need to calculate hash for the remaining files.
|
||||
const source = compilation.assets[asset.name].source();
|
||||
const shasum = crypto.createHash('sha1');
|
||||
shasum.update(source);
|
||||
const hash = shasum.digest('hex').substr(0, 10);
|
||||
return [
|
||||
name, // origin name
|
||||
`${name}?${hash}`, // name with hash
|
||||
];
|
||||
})
|
||||
.filter()
|
||||
.orderBy(['0'])
|
||||
.fromPairs()
|
||||
.value();
|
||||
const json = JSON.stringify(manifest, null, 2);
|
||||
compilation.assets[this.fileName] = {
|
||||
source: () => json,
|
||||
size: () => json.length,
|
||||
};
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/* eslint-disable */
|
||||
import _ from 'lodash';
|
||||
import gulp from 'gulp';
|
||||
import log from 'fancy-log';
|
||||
import chalk from 'chalk';
|
||||
import gulpConfig from './config/gulp.js';
|
||||
|
||||
export default async function ({ watch, production }) {
|
||||
function handleError(err) {
|
||||
log(chalk.red('Error: %s'), chalk.reset(err.toString()));
|
||||
if (err && !watch) { process.exit(1); }
|
||||
}
|
||||
const gulpTasks = gulpConfig({ watch, production, errorHandler: handleError });
|
||||
return new Promise((resolve) => {
|
||||
const taskList = {};
|
||||
|
||||
gulp.on('start', ({ uid, name }) => {
|
||||
log(chalk.blue('Starting task: %s'), chalk.reset(name));
|
||||
taskList[uid] = true;
|
||||
});
|
||||
gulp.on('stop', ({ uid, name }) => {
|
||||
log(chalk.green('Finished: %s'), chalk.reset(name));
|
||||
taskList[uid] = false;
|
||||
|
||||
if (Object.values(taskList).filter(b => b).length === 0) {
|
||||
if (watch) {
|
||||
gulpTasks.watch();
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
gulpTasks.default();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/* eslint-disable */
|
||||
import fs from 'fs';
|
||||
import webpack from 'webpack';
|
||||
import root from './utils/root';
|
||||
import webpackConfig from './config/webpack';
|
||||
|
||||
export default function ({ watch, production }) {
|
||||
const compiler = webpack(webpackConfig({ watch, production }));
|
||||
compiler.apply(new webpack.ProgressPlugin());
|
||||
|
||||
const outputOptions = {
|
||||
colors: true,
|
||||
errorDetails: true,
|
||||
optimizationBailout: production,
|
||||
};
|
||||
|
||||
function compilerCallback(err, stats) {
|
||||
if (err) {
|
||||
// config errors
|
||||
console.error(err.stack || err);
|
||||
if (err.details) console.error(err.details);
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
console.log(stats.toString(outputOptions));
|
||||
fs.writeFileSync(root('./.webpackStats.json'), JSON.stringify(stats.toJson(), null, 2));
|
||||
if (!watch && stats.hasErrors()) process.exitCode = 1;
|
||||
}
|
||||
|
||||
if (watch) compiler.watch({}, compilerCallback);
|
||||
else compiler.run(compilerCallback);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/* eslint-disable */
|
||||
import _ from 'lodash';
|
||||
|
||||
export default function mapWebpackUrlPrefix(mapList) {
|
||||
const rules = mapList.map((mappingRule) => {
|
||||
const regex = mappingRule.prefix.split('/').map(s => _.escapeRegExp(s)).join('[\\/\\\\]');
|
||||
return {
|
||||
from: new RegExp(`^${regex}`, mappingRule.flag),
|
||||
to: mappingRule.replace,
|
||||
};
|
||||
});
|
||||
return function mapUrl(url) {
|
||||
rules.forEach((rule) => {
|
||||
url = url.replace(rule.from, rule.to);
|
||||
});
|
||||
return url;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/* eslint-disable */
|
||||
import path from 'path';
|
||||
|
||||
export default function root(fn = '.') {
|
||||
return path.resolve(__dirname, '../../', fn);
|
||||
}
|
||||
Reference in new issue
Block a user