日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

dist包编译html_gulp4 多页面项目管理打包(html, es6,less编译压缩版本控制)

發布時間:2023/11/27 生活经验 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dist包编译html_gulp4 多页面项目管理打包(html, es6,less编译压缩版本控制) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.如何使用

gulp4自動化構建工具,讓所有變的簡單起來,那么需要怎么使用呢?

官網入口?按照官網安裝工具與依賴

2.項目結構

-- gulp4

-- gulpfile.babel.js

- index.js

- **其他配置項

-- node_modules

-- project 項目地址

- css

- js

- pages

- images

- .babelrc

- package-lock.json

- package.json

- webpack.config.js

3. 多頁面配置

入口文件做最后處理

// gulpfile.babel.js -- index.js

import {

series,

parallel,

watch,

} from 'gulp';

import del from 'del';

// 本地服務同步刷新

import browser from 'browser-sync';

const browserSync = browser.create();

// 引入功能組件

import convertLess from './convert-less';

import convertJs from './convert-js';

import convertHtml from './convert-html';

import copyFile from './static-copy';

// 是否開發環境

let isDev = true;

// 開發項目類型

const devType = 'pc';

// 本地目錄

const filePath = 'project/' + devType + '/';

// 生產目錄

const distResourcesPath = 'dist/' + devType + '/assets/';

const distPagesPath = 'dist/' + devType + '/view/';

// 資源路徑

const basePath = '../assets/';

// 刪除css文件

export const delCssFile = () => {

return del([

distResourcesPath + 'css'

])

}

// 刪除js文件

export const delJsFile = () => {

return del([

distResourcesPath + 'js'

])

}

// 刪除資源文件夾

export const delStaticFile = () => {

return del([

distResourcesPath + 'images',

distResourcesPath + 'fonts',

])

}

// 導出任務

// 復制文件

export const copyStatic = cb => {

copyFile(filePath, distResourcesPath);

cb();

}

// 編譯css

export const compileCss = series(delCssFile, cb => {

convertLess(filePath, distResourcesPath);

cb();

});

// 編譯js

export const compileJs = series(delJsFile, cb => {

convertJs(filePath, distResourcesPath);

cb();

});

// 編譯html

export const freshHtml = cb => {

convertHtml(filePath, distPagesPath, basePath);

cb();

};

// 監測文件變化

let watchFiles = () => {

browserSync.init({});

watch(filePath + 'css/**/*.less', {

delay: 500,

}, compileCss);

watch(filePath + 'js/**/*.js', {

delay: 500,

}, compileJs);

watch(filePath + 'pages/**', {

delay: 500,

}, freshHtml);

watch(filePath + 'mapjson/**/*.json', {

delay: 500,

}, freshHtml);

}

// 默認任務

exports.default = series(parallel(compileCss, compileJs), freshHtml, copyStatic, watchFiles);

不同任務可以提取出不同文件,例如less轉譯壓縮功能convert-less.js, 代碼如下:

/*

* @Author: ZLL

* @Date: 2020-01-18 18:18:52

* @Last Modified by: Liliang Zhu

* @Last Modified time: 2020-01-18 18:18:52

* 編譯less

*/

// gulp模塊

import {

src,

dest,

lastRun

} from 'gulp';

// less語法轉譯

import less from 'gulp-less';

// css添加前綴

import lessAutoperfix from 'less-plugin-autoprefix';

// 壓縮css

import mixCss from 'gulp-clean-css';

// 僅編譯改變的文件

import changed from 'gulp-changed';

// 重命名

import rename from 'gulp-rename';

// 生成版本號

import rev from 'gulp-rev';

// 本地服務同步刷新

import browser from 'browser-sync';

const browserSync = browser.create();

// css編譯前綴

const autoprefix = new lessAutoperfix({

browsers: [

">0.25%",

"last 2 version",

]

});

let convertLess = (file, dist) => {

return src(file + 'css/*.less', {

since: lastRun(convertLess, 100)

})

.pipe(less({

plugins: [autoprefix]

// 生成前綴

}))

.pipe(mixCss({

keepSpecialComments: '*'

//保留所有特殊前綴 當你用autoprefixer生成的瀏覽器前綴,如果不加這個參數,有可能將會刪除你的部分前綴

}))

.pipe(rename(path => path.basename += '.min'))

.pipe(rev())

.pipe(dest(dist + 'css'))

.pipe(rev.manifest())

.pipe(dest(file + 'mapjson/css'))

.pipe(browserSync.reload({

stream: true

}));

}

export default convertLess;

在入口index.js中引入調用即可,

4. 全部gulp4代碼

代碼全部托管在github,項目忙碌,抽空寫下博客,有問題可以直接留言

總結

以上是生活随笔為你收集整理的dist包编译html_gulp4 多页面项目管理打包(html, es6,less编译压缩版本控制)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。