AdminLTE/gulpfile.js

309 lines
8.2 KiB
JavaScript
Raw Normal View History

2021-09-26 11:04:01 +00:00
/* eslint-env node */
2021-05-16 21:10:19 +00:00
2021-05-20 23:03:54 +00:00
const autoprefix = require('autoprefixer')
2021-05-16 21:10:19 +00:00
const browserSync = require('browser-sync').create()
const del = require('del')
const { src, dest, lastRun, watch, series, parallel } = require('gulp')
2021-05-18 02:13:11 +00:00
const cleanCss = require('gulp-clean-css')
2023-01-27 13:47:48 +00:00
const gulpESLintNew = require('gulp-eslint-new')
2021-05-17 20:19:12 +00:00
const fileinclude = require('gulp-file-include')
const validator = require('gulp-html')
2021-05-28 04:15:47 +00:00
const gulpIf = require('gulp-if')
2021-05-16 21:10:19 +00:00
const postcss = require('gulp-postcss')
2021-05-17 03:12:33 +00:00
const rename = require('gulp-rename')
2021-07-11 16:19:32 +00:00
const sass = require('gulp-sass')(require('sass'))
2022-06-19 09:46:59 +00:00
const gulpStylelint = require('@ronilaukkarinen/gulp-stylelint')
const terser = require('gulp-terser')
2021-06-23 17:49:10 +00:00
const rollup = require('rollup')
const rollupTypescript = require('@rollup/plugin-typescript')
2021-05-20 23:03:54 +00:00
const rtlcss = require('rtlcss')
2021-05-17 03:12:33 +00:00
2021-05-16 22:14:52 +00:00
const pkg = require('./package')
const year = new Date().getFullYear()
const banner = `/*!
* AdminLTE v${pkg.version} (${pkg.homepage})
* Copyright 2014-${year} ${pkg.author}
* Licensed under MIT (https://github.com/ColorlibHQ/AdminLTE/blob/master/LICENSE)
*/`
2021-05-16 21:10:19 +00:00
// Define paths
const paths = {
dist: {
base: './dist/',
css: './dist/css',
2021-10-17 15:38:01 +00:00
js: './dist/js',
2021-05-16 21:10:19 +00:00
html: './dist/pages',
2021-11-07 16:56:23 +00:00
assets: './dist/assets'
2021-05-16 21:10:19 +00:00
},
src: {
base: './src/',
html: './src/pages/**/*.html',
assets: './src/assets/**/*.*',
partials: './src/partials/**/*.html',
scss: './src/scss',
2021-11-07 16:56:23 +00:00
ts: './src/ts'
2021-05-16 21:10:19 +00:00
},
temp: {
base: './.temp/',
css: './.temp/css',
2021-05-16 22:14:52 +00:00
js: './.temp/js',
2021-05-16 21:10:19 +00:00
html: './.temp/pages',
2021-11-07 16:56:23 +00:00
assets: './.temp/assets'
2021-09-26 11:04:01 +00:00
}
2021-05-16 21:10:19 +00:00
}
2021-05-17 03:12:33 +00:00
const sassOptions = {
outputStyle: 'expanded',
2021-09-26 11:04:01 +00:00
includePaths: ['./node_modules/']
2021-05-17 03:12:33 +00:00
}
2021-05-20 23:03:54 +00:00
const postcssOptions = [
2021-09-26 11:04:01 +00:00
autoprefix({ cascade: false })
2021-05-20 23:03:54 +00:00
]
2021-05-16 21:10:19 +00:00
2021-05-20 23:03:54 +00:00
const postcssRtlOptions = [
autoprefix({ cascade: false }),
2021-09-26 11:04:01 +00:00
rtlcss({})
2021-05-20 23:03:54 +00:00
]
// From here Dev mode will Start
2021-05-17 03:12:33 +00:00
// Lint SCSS
const lintScss = () => src([paths.src.scss + '/**/*.scss'], { since: lastRun(lintScss) })
.pipe(gulpStylelint({
failAfterError: false,
reporters: [
{ formatter: 'string', console: true }
2022-06-19 09:46:59 +00:00
],
debug: true
}))
2021-05-16 21:10:19 +00:00
// Compile SCSS
2021-06-23 17:49:10 +00:00
const scss = () => src(paths.src.scss + '/adminlte.scss', { sourcemaps: true })
2021-05-22 09:53:59 +00:00
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(postcssOptions))
.pipe(dest(paths.temp.css, { sourcemaps: '.' }))
.pipe(browserSync.stream())
2021-05-23 04:10:36 +00:00
// Compile SCSS Dark
// const scssDark = () => src(paths.src.scss + '/dark/adminlte-dark-addon.scss', { sourcemaps: true })
// .pipe(sass(sassOptions).on('error', sass.logError))
// .pipe(postcss(postcssOptions))
// .pipe(dest(paths.temp.css + '/dark', { sourcemaps: '.' }))
// .pipe(browserSync.stream())
2021-05-17 20:19:12 +00:00
// Lint TS
function isFixed(file) {
// Has ESLint fixed the file contents?
return file.eslint !== null && file.eslint.fixed
}
2021-05-16 21:10:19 +00:00
const lintTs = () => src([paths.src.ts + '/**/*.ts'], { since: lastRun(lintTs) })
2023-01-27 13:47:48 +00:00
.pipe(gulpESLintNew({ fix: true }))
.pipe(gulpIf(isFixed, dest(paths.src.ts)))
2023-01-27 13:47:48 +00:00
.pipe(gulpESLintNew.format())
.pipe(gulpESLintNew.failAfterError())
// Compile TS
2021-06-23 17:49:10 +00:00
const tsCompile = () =>
rollup.rollup({
input: paths.src.ts + '/adminlte.ts',
output: {
2021-09-26 11:04:01 +00:00
banner
2021-05-16 22:14:52 +00:00
},
2021-06-23 17:49:10 +00:00
plugins: [
2021-09-26 11:04:01 +00:00
rollupTypescript()
]
2021-06-23 17:49:10 +00:00
}).then(bundle => bundle.write({
file: paths.temp.js + '/adminlte.js',
format: 'umd',
name: 'adminlte',
2021-09-26 11:04:01 +00:00
sourcemap: true
2021-06-23 17:49:10 +00:00
}))
2021-05-16 22:14:52 +00:00
const assets = () => src([paths.src.assets])
.pipe(dest(paths.temp.assets))
.pipe(browserSync.stream())
2021-05-17 20:19:12 +00:00
2021-06-23 17:49:10 +00:00
const index = () => src([paths.src.base + '*.html'])
2021-05-17 20:19:12 +00:00
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
2021-09-26 11:04:01 +00:00
environment: 'development'
}
2021-05-17 20:19:12 +00:00
}))
.pipe(dest(paths.temp.base))
.pipe(browserSync.stream())
2021-05-16 21:10:19 +00:00
2021-06-23 17:49:10 +00:00
const html = () => src([paths.src.html])
2021-05-17 20:19:12 +00:00
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
2021-09-26 11:04:01 +00:00
environment: 'development'
}
2021-05-17 20:19:12 +00:00
}))
.pipe(dest(paths.temp.html))
.pipe(browserSync.stream())
2021-05-16 21:10:19 +00:00
2021-11-07 18:12:17 +00:00
const lintHtml = () => src([paths.temp.html + '/**/*.html', paths.temp.base + '*.html'], { since: lastRun(lintHtml) })
.pipe(validator())
2021-05-16 21:10:19 +00:00
2021-05-17 20:19:12 +00:00
const serve = () => {
2021-05-16 21:10:19 +00:00
browserSync.init({
2021-09-26 11:04:01 +00:00
server: paths.temp.base
2021-05-16 21:10:19 +00:00
})
2021-05-23 04:10:36 +00:00
watch([paths.src.scss], series(lintScss))
watch([paths.src.scss + '/**/*.scss', '!' + paths.src.scss + '/bootstrap-dark/**/*.scss', '!' + paths.src.scss + '/dark/**/*.scss'], series(scss))
watch([paths.src.scss + '/bootstrap-dark/', paths.src.scss + '/dark/'])
2021-05-28 04:15:47 +00:00
watch([paths.src.ts], series(lintTs, tsCompile))
watch([paths.src.html, paths.src.base + '*.html', paths.src.partials], series(html, index, lintHtml))
2021-05-22 09:53:59 +00:00
watch([paths.src.assets], series(assets))
}
2021-05-20 23:03:54 +00:00
// From here Dist will Start
2021-05-16 21:10:19 +00:00
// Clean
2021-05-20 23:03:54 +00:00
const cleanDist = () => del([paths.dist.base])
2021-05-16 21:10:19 +00:00
2022-02-13 15:12:07 +00:00
const lintDistScss = () => src([paths.src.scss + '/**/*.scss'], ['./.temp/**/*.css'])
.pipe(gulpStylelint({
failAfterError: false,
reporters: [
{ formatter: 'string', console: true }
]
}))
2021-05-18 02:13:11 +00:00
// Compile and copy all scss/css
2021-06-23 17:49:10 +00:00
const copyDistCssAll = () => src([paths.src.scss + '/**/*.scss'], {
base: paths.src.scss,
2021-09-26 11:04:01 +00:00
sourcemaps: true
2021-06-23 17:49:10 +00:00
})
2021-05-17 20:19:12 +00:00
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(postcssOptions))
2021-05-20 23:03:54 +00:00
.pipe(dest(paths.dist.css, { sourcemaps: '.' }))
2021-05-16 21:10:19 +00:00
2021-06-23 17:49:10 +00:00
const copyDistCssRtl = () => src(paths.dist.css + '/*.css', { sourcemaps: true })
2021-05-17 20:19:12 +00:00
.pipe(postcss(postcssRtlOptions))
.pipe(rename({ suffix: '.rtl' }))
2021-05-20 23:03:54 +00:00
.pipe(dest(paths.dist.css + '/rtl', { sourcemaps: '.' }))
2021-05-17 03:12:33 +00:00
// Minify CSS
const minifyDistCss = () => src([
paths.dist.css + '/**/*.css'
], {
base: paths.dist.css,
sourcemaps: true
})
.pipe(cleanCss({ format: { breakWith: 'lf' } }))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(paths.dist.css, { sourcemaps: '.' }))
const lintDistTs = () => src([paths.src.ts + '/**/*.ts'])
2023-01-27 13:47:48 +00:00
.pipe(gulpESLintNew())
.pipe(gulpESLintNew.failAfterError())
2021-05-17 03:12:33 +00:00
// Compile and copy ts/js
2021-06-23 17:49:10 +00:00
const copyDistJs = () =>
rollup.rollup({
input: paths.src.ts + '/adminlte.ts',
output: {
2021-09-26 11:04:01 +00:00
banner
2021-05-17 03:12:33 +00:00
},
2021-06-23 17:49:10 +00:00
plugins: [
2021-09-26 11:04:01 +00:00
rollupTypescript()
]
2021-06-23 17:49:10 +00:00
}).then(bundle => bundle.write({
2021-10-17 15:38:01 +00:00
file: paths.dist.js + '/adminlte.js',
2021-06-23 17:49:10 +00:00
format: 'umd',
name: 'adminlte',
2021-09-26 11:04:01 +00:00
sourcemap: true
2021-06-23 17:49:10 +00:00
}))
2021-05-17 03:12:33 +00:00
// Minify JS
2021-11-12 14:01:09 +00:00
const minifyDistJs = () => src(paths.dist.js + '/adminlte.js', { sourcemaps: true })
2021-11-14 12:06:05 +00:00
.pipe(terser({ compress: { passes: 2 } }))
2021-11-12 14:01:09 +00:00
.pipe(rename({ suffix: '.min' }))
.pipe(dest(paths.dist.js, { sourcemaps: '.' }))
// Copy assets
const copyDistAssets = () => src(paths.src.assets)
.pipe(dest(paths.dist.assets))
// Copy index
const copyDistHtmlIndex = () => src([paths.src.base + '*.html'])
2021-05-17 20:19:12 +00:00
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
2021-09-26 11:04:01 +00:00
environment: 'production'
}
2021-05-17 20:19:12 +00:00
}))
.pipe(dest(paths.dist.base))
2021-05-16 21:10:19 +00:00
// Copy Html
const copyDistHtml = () => src([paths.src.html])
2021-05-17 20:19:12 +00:00
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
2021-09-26 11:04:01 +00:00
environment: 'production'
}
2021-05-17 20:19:12 +00:00
}))
.pipe(dest(paths.dist.html))
2021-11-07 16:56:23 +00:00
// HTML Lint
// Copy index for Lint
const copyDistHtmlIndexForLint = () => src([paths.src.base + '*.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
environment: 'production'
}
}))
.pipe(dest(paths.temp.base))
// Copy Html for Lint
const copyDistHtmlForLint = () => src([paths.src.html])
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
environment: 'production'
}
}))
.pipe(dest(paths.temp.html))
// Now Lint
const lintDistHtmlCopied = () => src([paths.temp.html + '/**/*.html', paths.temp.base + '*.html'])
.pipe(validator())
2021-05-16 21:10:19 +00:00
2021-11-07 16:56:23 +00:00
const lintDistHtml = series(copyDistHtmlIndexForLint, copyDistHtmlForLint, lintDistHtmlCopied)
2021-05-16 21:10:19 +00:00
const lint = parallel(
lintDistScss,
lintDistTs,
2021-11-07 16:56:23 +00:00
lintDistHtml
)
exports.lint = lint
const compile = series(
cleanDist,
parallel(
2021-11-29 15:53:04 +00:00
series(copyDistCssAll, copyDistCssRtl, minifyDistCss),
series(copyDistJs, minifyDistJs),
copyDistAssets,
copyDistHtmlIndex,
2021-11-07 16:56:23 +00:00
copyDistHtml
)
)
exports.compile = compile
// For Production Release
exports.production = series(lint, compile)
2021-05-16 21:10:19 +00:00
2021-05-22 09:53:59 +00:00
// Default - Only for light mode AdminLTE
exports.default = series(scss, tsCompile, html, index, assets, serve)