From 7c7b8873defb69c3cf229c487febcd19fbf5265a Mon Sep 17 00:00:00 2001 From: printempw Date: Thu, 9 Aug 2018 23:36:12 +0800 Subject: [PATCH] Update gulpfile.js --- .eslintignore | 1 - gulpfile.js | 217 ++++++++++++++++++++++++++------------------------ package.json | 2 + yarn.lock | 22 ++++- 4 files changed, 137 insertions(+), 105 deletions(-) diff --git a/.eslintignore b/.eslintignore index 12d18299..f06240de 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,3 @@ node_modules/ resources/assets/dist/ resources/assets/src/vendor/ -gulpfile.js diff --git a/gulpfile.js b/gulpfile.js index c4fe727d..0d4fa0b6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,27 +1,29 @@ 'use strict'; -var gulp = require('gulp'), +const babel = require('gulp-babel'), - eslint = require('gulp-eslint'), - uglify = require('gulp-uglify'), - stylus = require('gulp-stylus'), + chalk = require('chalk'), cleanCss = require('gulp-clean-css'), - del = require('del'), - exec = require('child_process').exec, concat = require('gulp-concat'), - zip = require('gulp-zip'), - replace = require('gulp-batch-replace'), - notify = require('gulp-notify'), - sourcemaps = require('gulp-sourcemaps'), + del = require('del'), + eslint = require('gulp-eslint'), + execSync = require('child_process').execSync, + gulp = require('gulp'), merge = require('merge2'), - runSequence = require('run-sequence'); + replace = require('gulp-batch-replace'), + runSequence = require('run-sequence'), + sourcemaps = require('gulp-sourcemaps'), + stylus = require('gulp-stylus'), + through2 = require('through2'), + uglify = require('gulp-uglify'), + zip = require('gulp-zip'); -var version = require('./package.json').version; +const version = require('./package.json').version; -var srcPath = 'resources/assets/src'; -var distPath = 'resources/assets/dist'; +const srcPath = 'resources/assets/src'; +const distPath = 'resources/assets/dist'; -var vendorScripts = [ +const vendorScripts = [ 'jquery/dist/jquery.min.js', 'bootstrap/dist/js/bootstrap.min.js', 'admin-lte/dist/js/adminlte.min.js', @@ -33,14 +35,14 @@ var vendorScripts = [ 'jqPaginator/dist/1.2.0/jqPaginator.min.js', ]; -var vendorScriptsToBeMinified = [ +const vendorScriptsToBeMinified = [ 'regenerator-runtime/runtime.js', 'datatables.net/js/jquery.dataTables.js', 'datatables.net-bs/js/dataTables.bootstrap.js', 'resources/assets/dist/js/common.js', ]; -var vendorStyles = [ +const vendorStyles = [ 'bootstrap/dist/css/bootstrap.min.css', 'admin-lte/dist/css/AdminLTE.min.css', 'datatables.net-bs/css/dataTables.bootstrap.css', @@ -51,22 +53,22 @@ var vendorStyles = [ 'sweetalert2/dist/sweetalert2.min.css', ]; -var styleReplacements = [ +const styleReplacements = [ ['blue.png', '"../images/blue.png"'], ['blue@2x.png', '"../images/blue@2x.png"'], ['../img/loading.gif', '"../images/loading.gif"'], ['../img/loading-sm.gif', '"../images/loading-sm.gif"'], ]; -var scriptReplacements = []; +const scriptReplacements = []; -var fonts = [ +const fonts = [ 'font-awesome/fonts/**', 'bootstrap/dist/fonts/**', 'resources/assets/src/fonts/**', ]; -var images = [ +const images = [ 'icheck/skins/square/blue.png', 'icheck/skins/square/blue@2x.png', 'resources/assets/src/images/**', @@ -74,17 +76,21 @@ var images = [ 'bootstrap-fileinput/img/loading-sm.gif', ]; +const argv = require('minimist')(process.argv.slice(2)); + +// Determine if we are in production mode, +// run `gulp [task] --production` to enable. +if (argv.production) { + console.log(chalk.green('>> Running in PRODUCTION mode <<')); + process.env.NODE_ENV = 'production'; +} + // aka. `yarn run build` gulp.task('default', ['build']); // Build the things! gulp.task('build', callback => { - runSequence('clean', 'lint', ['compile-es6', 'compile-stylus'], 'publish-vendor', 'notify', callback); -}); - -// Send a notification -gulp.task('notify', () => { - return gulp.src('').pipe(notify('Assets compiled!')); + runSequence('clean', 'lint', ['compile-scripts', 'compile-stylus'], 'publish-vendor', callback); }); // Check JavaScript files with ESLint @@ -96,40 +102,39 @@ gulp.task('lint', () => { }); // Concentrate all vendor scripts & styles to one dist file -gulp.task('publish-vendor', ['compile-es6'], callback => { +gulp.task('publish-vendor', callback => { + // Collect pre-complied and raw library files + const vendorJs = gulp.src(collect(vendorScripts)).pipe(replace(scriptReplacements)); + const rawVendorJs = gulp.src(collect(vendorScriptsToBeMinified)).pipe(uglify()); // JavaScript files - var js = gulp.src(convertNpmRelativePath(vendorScripts)) - .pipe(replace(scriptReplacements)); - var jsToBeMinified = gulp.src(convertNpmRelativePath(vendorScriptsToBeMinified)) - .pipe(uglify()); - merge(js, jsToBeMinified) + merge(vendorJs, rawVendorJs) .pipe(sourcemaps.init({ loadMaps: true })) .pipe(concat('app.js')) - // Remove source mappings in the precompiled files + // Remove source mappings in the pre-compiled files .pipe(sourcemaps.write({ addComment: false })) .pipe(gulp.dest(`${distPath}/js/`)); // CSS files - gulp.src(convertNpmRelativePath(vendorStyles)) + gulp.src(collect(vendorStyles)) .pipe(sourcemaps.init({ loadMaps: true })) .pipe(concat('style.css')) .pipe(replace(styleReplacements)) .pipe(sourcemaps.write({ addComment: false })) .pipe(gulp.dest(`${distPath}/css/`)); // Fonts - gulp.src(convertNpmRelativePath(fonts)) + gulp.src(collect(fonts)) .pipe(gulp.dest(`${distPath}/fonts/`)); // Images - gulp.src(convertNpmRelativePath(images)) + gulp.src(collect(images)) .pipe(gulp.dest(`${distPath}/images/`)); // AdminLTE skins - gulp.src(convertNpmRelativePath(['admin-lte/dist/css/skins/*.min.css'])) + gulp.src(collect(['admin-lte/dist/css/skins/*.min.css'])) .pipe(gulp.dest(`${distPath}/css/skins/`)); - // 3D skin preview - gulp.src(convertNpmRelativePath(['three/build/three.min.js', 'skinview3d/build/skinview3d.min.js'])) + // Libraries for 3D skin preview + gulp.src(collect(['three/build/three.min.js', 'skinview3d/build/skinview3d.min.js'])) .pipe(concat('skinview3d.js')) .pipe(gulp.dest(`${distPath}/js/`)); // Chart.js - gulp.src(convertNpmRelativePath(['chart.js/dist/Chart.min.js'])) + gulp.src(collect(['chart.js/dist/Chart.min.js'])) .pipe(concat('chart.js')) .pipe(gulp.dest(`${distPath}/js/`)); @@ -139,99 +144,109 @@ gulp.task('publish-vendor', ['compile-es6'], callback => { // Compile stylus to css gulp.task('compile-stylus', () => { return gulp.src(`${srcPath}/stylus/*.styl`) - .pipe(sourcemaps.init()) + .pipe(dev(sourcemaps.init())) .pipe(stylus()) .pipe(cleanCss()) - .pipe(sourcemaps.write('./maps')) + .pipe(dev(sourcemaps.write('./maps'))) .pipe(gulp.dest(`${distPath}/css`)); }); // Compile ES6 scripts to ES5 -gulp.task('compile-es6', callback => { +gulp.task('compile-scripts', callback => { ['common', 'admin', 'auth', 'skinlib', 'user'].forEach(moduleName => { return gulp.src(`${srcPath}/js/${moduleName}/*.js`) - .pipe(sourcemaps.init()) + .pipe(dev(sourcemaps.init())) .pipe(babel()) .pipe(concat(`${moduleName}.js`)) .pipe(uglify()) - .pipe(sourcemaps.write('./maps')) + .pipe(dev(sourcemaps.write('./maps'))) .pipe(gulp.dest(`${distPath}/js`)); }); callback(); }); -// Delete cache files -gulp.task('clean', () => { +// Delete cache and built files +gulp.task('clean', callback => { + del([`${distPath}/**/*`]); clearCache(); - - return clearDist(); + callback(); }); -// Release archive file +// Release a zip archive file // aka. `yarn run release` gulp.task('zip', () => { + console.log(`Don't forget to run ${ chalk.underline.yellow('gulp build --production') } first!`); + + console.log('Cleaning cache files'); clearCache(); - console.log('Cache file deleted'); - exec('composer dump-autoload --no-dev', () => { - console.log('Autoload files generated without autoload-dev'); - }); + // Generate autoload files without autoload-dev + execSync('composer dump-autoload --no-dev', { stdio: 'inherit' }); - let zipPath = `blessing-skin-server-v${version}.zip`; + const savePath = argv['save-to'] || '..'; + const zipFile = `blessing-skin-server-v${version}.zip`; - console.log(`Zip archive will be saved to ${zipPath}.`); + console.log('Zip archive will be saved to ' + chalk.underline.blue( + require('path').join(savePath, zipFile) + )); return gulp.src([ - '**/*', - '**/.gitignore', - '**/.htaccess', - '.env.example', - // Exclude unnecessary files - '!.gitignore', - '!composer.*', - '!gulpfile.js', - '!ISSUE_TEMPLATE.md', - '!package.json', - '!phpunit.xml', - '!yarn.lock', - // Exclud unnecessary directories - '!plugins/**', - '!resources/assets/{src,src/**}', - '!resources/assets/dist/**/{maps,maps/**}', - '!resources/lang/overrides/**', - '!resources/views/overrides/**', - '!storage/textures/**', - '!{coverage,coverage/**}', - '!{node_modules,node_modules/**,node_modules/**/.gitignore}', - '!{tests,tests/**}', - // Exclude require-dev packages - '!vendor/fzaninotto/**', - '!vendor/mikey179/**', - '!vendor/mockery/**', - '!vendor/phpunit/**', - '!vendor/symfony/css-selector/**', - '!vendor/symfony/dom-crawler/**', - ]) - .pipe(zip(zipPath)) - .pipe(notify('Don\'t forget to build front-end resources before publishing a release!')) - .pipe(gulp.dest('../')) - .pipe(notify({ message: `Zip archive saved to ${zipPath}!` })); + '**/*', + '**/.gitignore', + '**/.htaccess', + '.env.example', + // Exclude unnecessary files + '!.gitignore', + '!composer.*', + '!gulpfile.js', + '!ISSUE_TEMPLATE.md', + '!package.json', + '!phpunit.xml', + '!yarn.lock', + // Exclude unnecessary directories + '!plugins/**', + '!resources/assets/{src,src/**}', + '!resources/assets/dist/**/{maps,maps/**}', + '!resources/lang/overrides/**', + '!resources/views/overrides/**', + '!storage/textures/**', + '!{coverage,coverage/**}', + '!{node_modules,node_modules/**,node_modules/**/.gitignore}', + '!{tests,tests/**}', + // Exclude require-dev packages + '!vendor/fzaninotto/**', + '!vendor/mikey179/**', + '!vendor/mockery/**', + '!vendor/phpunit/**', + '!vendor/symfony/css-selector/**', + '!vendor/symfony/dom-crawler/**', + ]) + .pipe(zip(zipFile)) + .pipe(gulp.dest(savePath)) + .pipe(through2.obj(function (chunk, enc, callback) { + console.log('Zip archive saved!'); + // Generate autoload files with autoload-dev + execSync('composer dump-autoload', { stdio: 'inherit' }); + callback(); + })); }); -gulp.task('watch', ['compile-stylus', 'compile-es6'], () => { - // watch .scss files - gulp.watch(`${srcPath}/stylus/*.scss`, ['compile-stylus'], () => notify('Stylus files compiled!')); - // watch .js files - gulp.watch(`${srcPath}/js/**/*.js`, ['compile-es6'], () => notify('ES6 scripts compiled!')); - gulp.watch(`${srcPath}/js/general.js`, ['publish-vendor']); +gulp.task('watch', ['compile-stylus', 'compile-scripts'], () => { + gulp.watch(`${srcPath}/stylus/*.styl`, ['compile-stylus']); + gulp.watch(`${srcPath}/js/**/*.js`, ['compile-scripts']); + gulp.watch(`${srcPath}/js/common/*.js`, ['publish-vendor']); }); -function convertNpmRelativePath(paths) { +function dev(transformFunction) { + return argv.production ? through2.obj() : transformFunction; +} + +const collect = function convertNpmRelativePath(paths) { return paths.map(relativePath => { return relativePath.startsWith('resources') ? relativePath : `node_modules/${relativePath}`; }); -} +}; function clearCache() { return del([ @@ -247,7 +262,3 @@ function clearCache() { '!storage/framework/sessions/index.html' ]); } - -function clearDist() { - return del([`${distPath}/**/*`]); -} diff --git a/package.json b/package.json index 722b3083..28c8d744 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "babel-plugin-transform-inline-environment-variables": "^0.2.0", "babel-plugin-transform-remove-console": "^6.9.0", "babel-preset-env": "^1.6.1", + "chalk": "^2.4.1", "codecov": "^3.0.0", "del": "^3.0.0", "gulp": "^3.9.1", @@ -55,6 +56,7 @@ "gulp-zip": "^4.1.0", "jest": "^20.0.4", "merge2": "^1.2.1", + "minimist": "^1.2.0", "run-sequence": "^2.2.1", "stylus": "^0.54.5" }, diff --git a/yarn.lock b/yarn.lock index 3a67c4b5..3ed14763 100644 --- a/yarn.lock +++ b/yarn.lock @@ -207,6 +207,12 @@ ansi-styles@^3.0.0, ansi-styles@^3.2.0: dependencies: color-convert "^1.9.0" +ansi-styles@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" + dependencies: + color-convert "^1.9.0" + ansi-wrap@0.1.0, ansi-wrap@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/ansi-wrap/-/ansi-wrap-0.1.0.tgz#a82250ddb0015e9a27ca82e82ea603bbfa45efaf" @@ -1273,6 +1279,14 @@ chalk@^2.0.0, chalk@^2.1.0: escape-string-regexp "^1.0.5" supports-color "^5.2.0" +chalk@^2.4.1: + version "2.4.1" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.1.tgz#18c49ab16a037b6eb0152cc83e3471338215b66e" + dependencies: + ansi-styles "^3.2.1" + escape-string-regexp "^1.0.5" + supports-color "^5.3.0" + chardet@^0.4.0: version "0.4.2" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2" @@ -4054,7 +4068,7 @@ minimist@0.0.8: version "0.0.8" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" -minimist@^1.1.0, minimist@^1.1.1: +minimist@^1.1.0, minimist@^1.1.1, minimist@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" @@ -5379,6 +5393,12 @@ supports-color@^5.2.0: dependencies: has-flag "^3.0.0" +supports-color@^5.3.0: + version "5.4.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54" + dependencies: + has-flag "^3.0.0" + sweetalert2@^6.11.5: version "6.11.5" resolved "https://registry.yarnpkg.com/sweetalert2/-/sweetalert2-6.11.5.tgz#a1ede34089225eb864898f4b613db4fec5dbe334"