Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. 'use strict'
  2. /****************************************************************************************************/
  3. //MODULES REQUIRE
  4. /****************************************************************************************************/
  5. const { src, dest, symlink, lastRun, task, watch, series, parallel } = require('gulp')
  6. const less = require('gulp-less')
  7. const validator = require('gulp-w3c-html-validator')
  8. const fileinclude = require('gulp-file-include')
  9. const postcss = require('gulp-postcss')
  10. const csso = require('postcss-csso')
  11. const customProperties = require('postcss-custom-properties')
  12. const postcssNesting = require('postcss-nesting')
  13. const postcssNested = require('postcss-nested')
  14. const gulpPurgeCss = require('gulp-purgecss')
  15. const autoprefixer = require('autoprefixer')
  16. const postcssImport = require('postcss-import')
  17. const postcssCustomMedia = require('postcss-custom-media')
  18. const mqp = require('css-mqpacker')
  19. const imagemin = require('gulp-imagemin')
  20. const svgSprite = require('gulp-svg-sprite')
  21. const mainNpmFiles = require('npmfiles')
  22. const webpack = require('webpack')
  23. const webp = require('gulp-webp')
  24. const gulpwebpack = require('webpack-stream')
  25. const favicons = require('gulp-favicons')
  26. const plumber = require('gulp-plumber')
  27. const newer = require('gulp-newer')
  28. const debug = require('gulp-debug')
  29. const gulpIf = require('gulp-if')
  30. const del = require('del')
  31. const flatten = require('gulp-flatten')
  32. const remember = require('gulp-remember')
  33. const cached = require('gulp-cached')
  34. const path = require('path')
  35. const browserSync = require('browser-sync').create()
  36. /****************************************************************************************************/
  37. //DEV OR PRODUCTION
  38. /****************************************************************************************************/
  39. const isDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV === 'development'
  40. /****************************************************************************************************/
  41. //PATHS AND SETTINGS
  42. /****************************************************************************************************/
  43. const cms = {
  44. modx: {
  45. html: 'build/',
  46. css: 'build/assets/css/',
  47. js: 'build/assets/js/',
  48. img: 'build/assets/',
  49. libs: 'build/assets/libs/',
  50. fonts: 'build/assets/fonts/'
  51. }
  52. }
  53. /****************************************************************************************************/
  54. //HTML task
  55. /****************************************************************************************************/
  56. const html = () =>
  57. src('src/*.html', { since: lastRun('html') })
  58. .pipe(plumber())
  59. .pipe(fileinclude())
  60. .pipe(dest(cms.modx.html))
  61. /****************************************************************************************************/
  62. //HTML templates task
  63. /****************************************************************************************************/
  64. const htmltemplates = () =>
  65. src('src/*.html')
  66. .pipe(plumber())
  67. .pipe(fileinclude())
  68. .pipe(dest(cms.modx.html))
  69. /****************************************************************************************************/
  70. //HTML validation task
  71. /****************************************************************************************************/
  72. const validation = () =>
  73. src('build/*.html')
  74. .pipe(validator())
  75. .pipe(validator.reporter())
  76. /****************************************************************************************************/
  77. //CSS task
  78. /****************************************************************************************************/
  79. const css = (cb) => {
  80. let processors = [
  81. postcssImport({ path: ['src/css'] }),
  82. customProperties({ preserve: true }),
  83. postcssCustomMedia,
  84. postcssNesting,
  85. postcssNested,
  86. autoprefixer({ cascade: false, flexbox: false }),
  87. mqp({ sort: true })
  88. ]
  89. src('src/css/style.css', { sourcemaps: true })
  90. .pipe(plumber())
  91. .pipe(less())
  92. .pipe(postcss(processors))
  93. .pipe(gulpIf(!isDevelopment, gulpPurgeCss({ content: ['src/*.html', 'src/templates/*.html', 'src/js/**/*.js'] })))
  94. .pipe(gulpIf(!isDevelopment, postcss([csso({ restructure: false, comments: false })])))
  95. .pipe(gulpIf(!isDevelopment, dest(cms.modx.css), dest(cms.modx.css, { sourcemaps: '.' })))
  96. .pipe(browserSync.stream())
  97. cb()
  98. }
  99. /****************************************************************************************************/
  100. //JS task
  101. /****************************************************************************************************/
  102. const js = () =>
  103. src('src/js/main.js')
  104. .pipe(plumber())
  105. .pipe(gulpwebpack(require('./webpack.config.js'), webpack))
  106. .pipe(dest(cms.modx.js))
  107. .pipe(browserSync.stream())
  108. /****************************************************************************************************/
  109. //LIBS task
  110. /****************************************************************************************************/
  111. const libs = () =>
  112. src(mainNpmFiles(), { base: './node_modules' })
  113. .pipe(flatten({ includeParents: 1 }))
  114. .pipe(newer(cms.modx.libs))
  115. .pipe(dest(cms.modx.libs))
  116. /****************************************************************************************************/
  117. //FONTS task
  118. /****************************************************************************************************/
  119. const fonts = () =>
  120. src('src/fonts/**/*.*')
  121. .pipe(newer(cms.modx.fonts))
  122. .pipe(gulpIf(isDevelopment, symlink(cms.modx.fonts), dest(cms.modx.fonts)))
  123. /****************************************************************************************************/
  124. //IMG task (jpg,png,gif)
  125. /****************************************************************************************************/
  126. const img = () =>
  127. src(['src/img/**/*.*', 'src/images/**/*.*', '!src/img/icons/*.*'], { base: 'src' })
  128. .pipe(newer(cms.modx.img))
  129. .pipe(gulpIf(!isDevelopment, imagemin([
  130. imagemin.gifsicle({ interlaced: true }),
  131. imagemin.jpegtran({ progressive: true }),
  132. imagemin.optipng({ optimizationLevel: 5 }),
  133. imagemin.svgo({ removeViewBox: false, collapseGroups: true })
  134. ])))
  135. .pipe(gulpIf(!isDevelopment, dest(cms.modx.img), symlink(cms.modx.img)))
  136. /****************************************************************************************************/
  137. //IMG task (jpg,png,gif)
  138. /****************************************************************************************************/
  139. const webpimages = () =>
  140. src(['src/img/**/*.{jpg,png,gif}', 'src/images/**/*.{jpg,png,gif}', '!src/img/favicons/*.*'], {
  141. base: 'src',
  142. since: lastRun(webpimages)
  143. })
  144. .pipe(plumber())
  145. .pipe(webp())
  146. .pipe(dest('src'))
  147. /****************************************************************************************************/
  148. //SVG sprite icons
  149. /****************************************************************************************************/
  150. let config = {
  151. shape: {
  152. dimension: {
  153. maxWidth: 50,
  154. maxHeight: 50
  155. },
  156. spacing: {
  157. padding: 0,
  158. box: 'icon'
  159. },
  160. transform: [
  161. {
  162. svgo: {
  163. plugins: [
  164. { removeXMLNS: true },
  165. { cleanupListOfValues: true },
  166. { convertShapeToPath: false },
  167. { removeAttrs: { attrs: ['data-name', 'version'] } },
  168. { removeStyleElement: true },
  169. { removeScriptElement: true }
  170. ],
  171. floatPrecision: 1
  172. }
  173. }
  174. ]
  175. },
  176. svg: {
  177. xmlDeclaration: false,
  178. doctypeDeclaration: false,
  179. dimensionAttributes: false
  180. },
  181. mode: {
  182. stack: {
  183. dest: '.',
  184. sprite: 'img/sprite.svg',
  185. render: {
  186. css: {
  187. template: 'src/templates/icon_template.html',
  188. dest: 'css/modules/sprite.css'
  189. }
  190. }
  191. }
  192. }
  193. }
  194. const svgicons = () =>
  195. src('src/img/icons/*.svg')
  196. .pipe(cached('svg:icons'))
  197. .pipe(remember('svg:icons'))
  198. .pipe(svgSprite(config))
  199. .pipe(dest('src'))
  200. /****************************************************************************************************/
  201. //DEL build directory
  202. /****************************************************************************************************/
  203. const clean = () => {
  204. return del('build')
  205. }
  206. /****************************************************************************************************/
  207. //Copy favicon
  208. /****************************************************************************************************/
  209. const faviconsGenerator = () =>
  210. src('src/img/favicons/favicon.png')
  211. .pipe(favicons({
  212. appName: 'My App',
  213. appShortName: 'App',
  214. appDescription: 'This is my application',
  215. background: '#020307',
  216. path: 'assets/img/favicons/',
  217. url: 'http://mysite.ru/',
  218. display: 'standalone',
  219. lang: 'ru-RU',
  220. orientation: 'portrait',
  221. scope: '/',
  222. start_url: '/',
  223. version: 1.0,
  224. logging: false,
  225. html: 'index.html',
  226. pipeHTML: true,
  227. replace: true,
  228. icons: {
  229. coast: false,
  230. firefox: false,
  231. windows: false,
  232. yandex: false
  233. }
  234. }))
  235. .pipe(dest('build/assets/img/favicons'))
  236. /****************************************************************************************************/
  237. //WATCHERS
  238. /****************************************************************************************************/
  239. const watchers = (cb) => {
  240. watch('src/*.html', html).on('unlink', (filepath) => {
  241. let filePathFromSrc = path.relative(path.resolve('src/'), filepath)
  242. let destFilePath = path.resolve(cms.modx.html, filePathFromSrc)
  243. del.sync(destFilePath)
  244. })
  245. watch('src/templates/*.html', htmltemplates)
  246. watch('build/*.html').on('change', browserSync.reload)
  247. watch('src/css/**/*.css', css)
  248. watch('src/css/**/*.less', css)
  249. watch('src/js/**/*.js', js)
  250. watch(['src/**/*.{jpg,png,gif,webp}', '!src/img/icons/'], img).on('unlink', (filepath) => {
  251. let filePathFromSrc = path.relative(path.resolve('src/'), filepath)
  252. let destFilePath = path.resolve(cms.modx.img, filePathFromSrc)
  253. del.sync(destFilePath)
  254. })
  255. watch('src/img/icons/*.svg', svgicons).on('unlink', (filepath) => {
  256. remember.forget('svg:icons', path.resolve(filepath))
  257. delete cached.caches['svg:icons'][path.resolve(filepath)]
  258. })
  259. watch('src/fonts/**/*.*', fonts).on('unlink', (filepath) => {
  260. let filePathFromSrc = path.relative(path.resolve('src/fonts'), filepath)
  261. let destFilePath = path.resolve(cms.modx.fonts, filePathFromSrc)
  262. del.sync(destFilePath)
  263. })
  264. cb()
  265. }
  266. /****************************************************************************************************/
  267. //BROWSER-SYNC task
  268. /****************************************************************************************************/
  269. const serve = (cb) => {
  270. browserSync.init({
  271. server: {
  272. baseDir: './build/'
  273. },
  274. open: false,
  275. notify: false
  276. })
  277. cb()
  278. }
  279. /****************************************************************************************************/
  280. //EXPORT TASKS
  281. /****************************************************************************************************/
  282. exports.html = html
  283. exports.validation = validation
  284. exports.css = css
  285. exports.js = js
  286. exports.libs = libs
  287. exports.fonts = fonts
  288. exports.img = img
  289. exports.webp = webpimages
  290. exports.svgicons = svgicons
  291. exports.favicons = faviconsGenerator
  292. exports.watchers = watchers
  293. exports.serve = serve
  294. exports.clean = clean
  295. /****************************************************************************************************/
  296. //GLOBAL TASKS
  297. /****************************************************************************************************/
  298. task('build', series(svgicons, parallel(html, css, js, libs, fonts, img)))
  299. exports.dev = series('build', parallel(watchers, serve))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement