You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdminLTE/gulpfile.js

325 lines
7.6 KiB

4 years ago
/* eslint-disable no-console */
4 years ago
/* eslint-disable camelcase */
/* eslint-disable no-undef */
/* eslint-disable unicorn/prefer-module */
const browserSync = require('browser-sync').create()
const cleanCss = require('gulp-clean-css')
const del = require('del')
4 years ago
const esbuild = require('esbuild')
const { ESLint } = require('eslint')
const { src, dest, lastRun, watch, series } = require('gulp')
const fileinclude = require('gulp-file-include')
4 years ago
const npmDist = require('gulp-npm-dist')
const postcss = require('gulp-postcss')
4 years ago
const rename = require('gulp-rename')
4 years ago
const sass = require('gulp-sass')
4 years ago
const sourcemaps = require('gulp-sourcemaps')
4 years ago
const gulpStylelint = require('gulp-stylelint')
const wait = require('gulp-wait')
4 years ago
sass.compiler = require('sass')
4 years ago
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)
*/`
4 years ago
// Define paths
const paths = {
dist: {
base: './dist/',
css: './dist/css',
4 years ago
js: './dist/js',
4 years ago
html: './dist/pages',
assets: './dist/assets',
img: './dist/assets/img',
vendor: './dist/vendor'
},
base: {
base: './',
node: './node_modules'
},
src: {
base: './src/',
css: './src/css',
html: './src/pages/**/*.html',
assets: './src/assets/**/*.*',
partials: './src/partials/**/*.html',
scss: './src/scss',
4 years ago
ts: './src/ts',
4 years ago
node_modules: './node_modules/',
vendor: './vendor'
},
temp: {
base: './.temp/',
css: './.temp/css',
4 years ago
js: './.temp/js',
4 years ago
html: './.temp/pages',
assets: './.temp/assets',
vendor: './.temp/vendor'
}
}
4 years ago
const sassOptions = {
outputStyle: 'expanded',
includePaths: ['./node_modules/']
}
function postcssOptions() {
4 years ago
return {
map: {
inline: false,
annotation: true,
sourcesContent: true
},
plugins: [
require('autoprefixer')({
cascade: false
})
]
}
}
4 years ago
function postcssRtlOptions() {
return {
map: {
inline: false,
annotation: true,
sourcesContent: true
},
plugins: [
require('autoprefixer')({
cascade: false
}),
require('rtlcss')({})
]
}
}
4 years ago
// Compile SCSS
4 years ago
const scss = () => {
return src(paths.src.scss + '/adminlte.scss')
.pipe(wait(500))
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(postcssOptions))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.temp.css))
.pipe(browserSync.stream())
}
// Lint SCSS
const lintScss = () => {
return src([paths.src.scss + '/**/*.scss'], { since: lastRun(lintScss) })
.pipe(gulpStylelint({
reporters: [
{ formatter: 'string', console: true }
]
}))
}
4 years ago
4 years ago
const ts = () => {
4 years ago
return esbuild.build({
entryPoints: [paths.src.ts + '/adminlte.ts'],
banner: {
js: banner
},
bundle: true,
format: 'iife',
sourcemap: true,
target: ['chrome60'],
outfile: paths.temp.js + '/adminlte.js'
}).catch(
error => console.error(error)
)
4 years ago
}
4 years ago
4 years ago
// Lint TS
async function lintTs() {
const eslint = new ESLint()
const results = await eslint.lintFiles([paths.src.ts + '/**/*.ts'])
const formatter = await eslint.loadFormatter('stylish')
const resultText = formatter.format(results)
console.log(resultText)
}
const index = () => {
return src([paths.src.base + '*.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
environment: 'development'
}
}))
.pipe(dest(paths.temp.base))
.pipe(browserSync.stream())
}
4 years ago
4 years ago
const html = () => {
return src([paths.src.html])
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
environment: 'development'
}
}))
.pipe(dest(paths.temp.html))
.pipe(browserSync.stream())
}
4 years ago
4 years ago
const assets = () => {
return src([paths.src.assets])
.pipe(dest(paths.temp.assets))
.pipe(browserSync.stream())
}
4 years ago
4 years ago
const vendor = () => {
return src(npmDist({ copyUnminified: true }), { base: paths.src.node_modules })
.pipe(dest(paths.temp.vendor))
}
4 years ago
4 years ago
const serve = () => {
4 years ago
browserSync.init({
server: paths.temp.base
})
4 years ago
watch([paths.src.scss], series(scss))
watch([paths.src.scss], series(lintScss))
watch([paths.src.ts], series(ts))
watch([paths.src.html, paths.src.base + '*.html', paths.src.partials], series(html, index))
watch([paths.src.assets], series(assets))
watch([paths.src.vendor], series(vendor))
}
4 years ago
// Minify CSS
4 years ago
const minifyDistCss = () => {
return src([
4 years ago
paths.dist.css + '/**/*.css'
], { base: paths.dist.css })
4 years ago
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(cleanCss())
.pipe(rename({ suffix: '.min' }))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.dist.css))
}
4 years ago
4 years ago
// Minify JS
4 years ago
const minifyDistJs = () => {
4 years ago
return esbuild.build({
entryPoints: [paths.dist.js + '/adminlte.js'],
format: 'iife',
minify: true,
sourcemap: true,
target: ['chrome60'],
outfile: paths.dist.js + '/adminlte.min.js'
}).catch(
error => console.error(error)
)
4 years ago
}
4 years ago
4 years ago
// Copy assets
4 years ago
const copyDistAssets = () => {
return src(paths.src.assets)
.pipe(dest(paths.dist.assets))
}
4 years ago
// Clean
4 years ago
const cleanDist = () => {
4 years ago
return del([paths.dist.base])
4 years ago
}
4 years ago
// Compile and copy scss/css
4 years ago
const copyDistCss = () => {
return src([paths.src.scss + '/adminlte.scss'])
.pipe(wait(500))
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(postcssOptions))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.dist.css))
}
4 years ago
4 years ago
const copyDistCssDark = () => {
return src([paths.src.scss + '/dark/*.scss'])
.pipe(wait(500))
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(postcssOptions))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.dist.css + '/dark'))
}
4 years ago
4 years ago
const copyDistCssRtl = () => {
return src([
4 years ago
paths.dist.css + '/*.css',
paths.dist.css + '/dark/*.css'
])
4 years ago
.pipe(wait(500))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(postcss(postcssRtlOptions))
.pipe(rename({ suffix: '.rtl' }))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.dist.css + '/rtl'))
}
4 years ago
// Compile and copy ts/js
4 years ago
const copyDistJs = () => {
4 years ago
return esbuild.build({
entryPoints: [paths.src.ts + '/adminlte.ts'],
banner: {
js: banner
},
bundle: true,
format: 'iife',
sourcemap: true,
target: ['chrome60'],
outfile: paths.dist.js + '/adminlte.js'
}).catch(
error => console.error(error)
)
4 years ago
}
4 years ago
4 years ago
// Copy Html
4 years ago
const copyDistHtml = () => {
return src([paths.src.html])
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
environment: 'production'
}
}))
.pipe(dest(paths.dist.html))
}
4 years ago
// Copy index
4 years ago
const copyDistHtmlIndex = () => {
return src([paths.src.base + '*.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: './src/partials/',
context: {
environment: 'production'
}
}))
.pipe(dest(paths.dist.base))
}
4 years ago
// Copy node_modules to vendor
4 years ago
const copyDistVendor = () => {
return src(npmDist({ copyUnminified: true }), { base: paths.src.node_modules })
.pipe(dest(paths.dist.vendor))
}
4 years ago
4 years ago
exports.build = series(lintScss, lintTs, cleanDist, copyDistCss, copyDistCssDark, copyDistCssRtl, minifyDistCss, copyDistJs, minifyDistJs, copyDistHtml, copyDistHtmlIndex, copyDistAssets, copyDistVendor)
4 years ago
// Default
4 years ago
exports.default = series(lintScss, scss, lintTs, ts, html, index, assets, vendor, serve)