| 1 |
const {src, dest} = require('gulp'); |
| 2 |
const {createGulpEsbuild} = require('gulp-esbuild'); |
| 3 |
const concat = require('gulp-concat'); |
| 4 |
const plumber = require('gulp-plumber'); |
| 5 |
const terser = require('gulp-terser'); |
| 6 |
const sass = require('gulp-sass')(require('sass')); |
| 7 |
const postcss = require('gulp-postcss'); |
| 8 |
const autoprefixer = require('autoprefixer'); |
| 9 |
const cssnano = require('cssnano'); |
| 10 |
const sourcemaps = require('gulp-sourcemaps'); |
| 11 |
const livereload = require('gulp-livereload'); |
| 12 |
const log = require('fancy-log'); |
| 13 |
const packageImporter = require('node-sass-package-importer'); |
| 14 |
|
| 15 |
const getEsBuildOptions = (buildSourceMap = true) => { |
| 16 |
const esbuildOptions = { |
| 17 |
minify: false, |
| 18 |
target: 'es2015', |
| 19 |
bundle: false, |
| 20 |
}; |
| 21 |
|
| 22 |
if (buildSourceMap) { |
| 23 |
esbuildOptions.sourcemap = 'inline'; |
| 24 |
} |
| 25 |
|
| 26 |
return esbuildOptions; |
| 27 |
}; |
| 28 |
|
| 29 |
const devEsBuild = () => |
| 30 |
createGulpEsbuild({ |
| 31 |
incremental: true, |
| 32 |
pipe : true, |
| 33 |
}); |
| 34 |
|
| 35 |
const prodEsBuild = () => |
| 36 |
createGulpEsbuild({ |
| 37 |
pipe: true, |
| 38 |
}); |
| 39 |
|
| 40 |
/** |
| 41 |
* @param {string[]|string} sourceJsFiles |
| 42 |
* @param {string} outputLocation |
| 43 |
* @param {string} outputFileName |
| 44 |
* @param {boolean} isProd |
| 45 |
*/ |
| 46 |
function getJsCompiler(sourceJsFiles, outputLocation, outputFileName, isProd) { |
| 47 |
return function compileJS() { |
| 48 |
let task = src(sourceJsFiles) |
| 49 |
.pipe(plumber()) |
| 50 |
.pipe(sourcemaps.init().on('error', log.error)) |
| 51 |
.pipe(concat(outputFileName)) |
| 52 |
// Save inline the source map, the terser will remove it in production. |
| 53 |
.pipe(sourcemaps.write().on('error', log.error)); |
| 54 |
|
| 55 |
if (isProd) { |
| 56 |
task = task.pipe(prodEsBuild()(getEsBuildOptions(false))); |
| 57 |
} else { |
| 58 |
task = task.pipe(devEsBuild()(getEsBuildOptions(true))); |
| 59 |
} |
| 60 |
|
| 61 |
if (isProd) { |
| 62 |
task = task.pipe(terser({toplevel: false})); |
| 63 |
} else { |
| 64 |
task = task.pipe(livereload()); |
| 65 |
} |
| 66 |
|
| 67 |
task = task.pipe(dest(outputLocation)); |
| 68 |
|
| 69 |
return task; |
| 70 |
}; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
function getPostCssPlugins(isProd = false) { |
| 75 |
// The postcss plugins will read from package.json for the browserslist. |
| 76 |
const postCssPlugins = [ |
| 77 |
// Add auto-prefixer to remove outdated vendor prefixes. |
| 78 |
autoprefixer({remove: true}), |
| 79 |
]; |
| 80 |
|
| 81 |
if (isProd) { |
| 82 |
postCssPlugins.push( |
| 83 |
cssnano({ |
| 84 |
preset: [ |
| 85 |
'default', |
| 86 |
{ |
| 87 |
discardComments: { |
| 88 |
removeAll: true, |
| 89 |
}, |
| 90 |
}, |
| 91 |
], |
| 92 |
}), |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
return postCssPlugins; |
| 97 |
} |
| 98 |
|
| 99 |
function getSCSSCompiler(sourceScssFiles, outputLocation, isProd = false) { |
| 100 |
return function compileCss() { |
| 101 |
let task = src(sourceScssFiles) |
| 102 |
.pipe(plumber()); |
| 103 |
|
| 104 |
if (!isProd) { |
| 105 |
task = task.pipe(sourcemaps.init()); |
| 106 |
} |
| 107 |
|
| 108 |
task = task.pipe( |
| 109 |
sass.sync({ |
| 110 |
outputStyle: isProd ? 'compressed' : 'expanded', |
| 111 |
importer : packageImporter(), |
| 112 |
}) |
| 113 |
.on('error', sass.logError), |
| 114 |
) |
| 115 |
.pipe(postcss(getPostCssPlugins(isProd))); |
| 116 |
|
| 117 |
if (!isProd) { |
| 118 |
task = task.pipe(sourcemaps.write('./')); |
| 119 |
} |
| 120 |
|
| 121 |
task = task.pipe(dest(outputLocation)); |
| 122 |
|
| 123 |
if (!isProd) { |
| 124 |
task = task.pipe(livereload()); |
| 125 |
} |
| 126 |
|
| 127 |
return task; |
| 128 |
}; |
| 129 |
} |
| 130 |
|
| 131 |
module.exports = { |
| 132 |
getJsCompiler, |
| 133 |
getSCSSCompiler, |
| 134 |
}; |
| 135 |
|