wishthis/node_modules/fomantic-ui/tasks/build/javascript.js

167 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-01-13 13:00:31 +00:00
/*******************************
Build Task
*******************************/
const
2023-08-17 09:47:40 +00:00
gulp = require('gulp'),
// node dependencies
console = require('better-console'),
// gulp dependencies
chmod = require('gulp-chmod'),
concat = require('gulp-concat'),
dedupe = require('gulp-dedupe'),
flatten = require('gulp-flatten'),
gulpif = require('gulp-if'),
header = require('gulp-header'),
normalize = require('normalize-path'),
plumber = require('gulp-plumber'),
print = require('gulp-print').default,
rename = require('gulp-rename'),
replace = require('gulp-replace'),
uglify = require('gulp-uglify'),
// config
config = require('../config/user'),
docsConfig = require('../config/docs'),
tasks = require('../config/tasks'),
install = require('../config/project/install'),
// shorthand
globs = config.globs,
assets = config.paths.assets,
banner = tasks.banner,
filenames = tasks.filenames,
comments = tasks.regExp.comments,
log = tasks.log,
settings = tasks.settings
2022-01-13 13:00:31 +00:00
;
/**
* Concat and uglify the Javascript files
* @param {string|array} src - source files
* @param type
* @param config
* @return {*}
*/
function build(src, type, config) {
2023-08-17 09:47:40 +00:00
return gulp.src(src)
.pipe(plumber())
.pipe(flatten())
.pipe(replace(comments.license.in, comments.license.out))
.pipe(gulpif(config.hasPermissions, chmod(config.parsedPermissions)))
.pipe(gulp.dest(config.paths.output.uncompressed))
.pipe(print(log.created))
.pipe(uglify(settings.uglify))
.pipe(rename(settings.rename.minJS))
.pipe(header(banner, settings.header))
.pipe(gulpif(config.hasPermissions, chmod(config.parsedPermissions)))
.pipe(gulp.dest(config.paths.output.compressed))
.pipe(print(log.created))
2022-01-13 13:00:31 +00:00
;
}
/**
* Packages the Javascript files in dist
* @param {string} type - type of the js processing (none, rtl, docs)
* @param {boolean} compress - should the output be compressed
*/
function pack(type, compress) {
2023-08-17 09:47:40 +00:00
const output = type === 'docs' ? docsConfig.paths.output : config.paths.output;
const concatenatedJS = compress ? filenames.concatenatedMinifiedJS : filenames.concatenatedJS;
return gulp.src(output.uncompressed + '/**/' + globs.components + globs.ignored + '.js')
.pipe(plumber())
.pipe(dedupe())
.pipe(replace(assets.uncompressed, assets.packaged))
.pipe(concat(concatenatedJS))
.pipe(gulpif(config.stripHeaders, replace(comments.header.in, comments.header.out)))
.pipe(gulpif(compress, uglify(settings.concatUglify)))
.pipe(header(banner, settings.header))
.pipe(gulpif(config.hasPermissions, chmod(config.parsedPermissions)))
.pipe(gulp.dest(output.packaged))
.pipe(print(log.created))
2022-01-13 13:00:31 +00:00
;
}
function buildJS(src, type, config, callback) {
2023-08-17 09:47:40 +00:00
if (!install.isSetup()) {
console.error('Cannot build Javascript. Run "gulp install" to set-up Fomantic');
callback();
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
return;
}
if (callback === undefined) {
callback = config;
config = type;
type = src;
src = config.paths.source.definitions + '/**/' + config.globs.components + (config.globs.ignored || '') + '.js';
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
if (globs.individuals !== undefined && typeof src === 'string') {
const components = config.globs.components.replace(/[{}]/g, '') + ',' + config.globs.individuals.replace(/[{}]/g, '');
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
src = config.paths.source.definitions + '/**/{' + components + '}' + (config.globs.ignored || '') + '.js';
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
// copy source javascript
const js = () => build(src, type, config);
js.displayName = 'Building un/compressed Javascript';
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
const packUncompressed = () => pack(type, false);
packUncompressed.displayName = 'Packing uncompressed Javascript';
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
const packCompressed = () => pack(type, true);
packCompressed.displayName = 'Packing compressed Javascript';
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
gulp.series(js, gulp.parallel(packUncompressed, packCompressed))(callback);
2022-01-13 13:00:31 +00:00
}
module.exports = function (callback) {
2023-08-17 09:47:40 +00:00
buildJS(false, config, callback);
2022-01-13 13:00:31 +00:00
};
// We keep the changed files in an array to call build with all of them at the same time
2023-08-17 09:47:40 +00:00
let
timeout,
files = []
;
2022-01-13 13:00:31 +00:00
module.exports.watch = function (type, config) {
2023-08-17 09:47:40 +00:00
gulp
.watch([normalize(config.paths.source.definitions + '/**/*.js')])
.on('all', function (event, path) {
// We don't handle deleted files yet
if (event === 'unlink' || event === 'unlinkDir') {
return;
}
// Clear timeout
if (timeout) {
clearTimeout(timeout);
}
// Add file to internal changed files array
if (!files.includes(path)) {
files.push(path);
}
// Update timeout
timeout = setTimeout(() => {
console.log('Change in javascript detected');
// Copy files to build in another array
const buildFiles = [...files];
// Call method
gulp.series((callback) => buildJS(buildFiles, type, config, callback))();
// Reset internal changed files array
files = [];
}, 1000);
})
;
2022-01-13 13:00:31 +00:00
};
2022-06-08 10:36:39 +00:00
module.exports.buildJS = buildJS;