. +-- src | +-- assets [各頁面引用文件] | | +-- css | | +-- js [可以使用es6語法,tools下文件需引入纔可用] | | | +-- tools | | | | +-- axios.js | | | | +-- global.js | | | +-- index.js | | | +-- login.js | | +-- less | | | +-- index.less | | | +-- login.less | +-- common [框架、插件] | | +-- css | | | +-- bootstrap.min.css | | +-- img | | +-- js | | | +-- bootstrap.min.js | +-- part [公用模塊] | | +-- less | | | +-- common.less | | | +-- footer.less | | | +-- header.less | | | +-- variable.less | | +-- tpl | | | +-- header.html | | | +-- footer.html | | | +-- meta.html | +-- index.html | +-- login.html +-- gulpfile.js +-- package.json +-- webpack.config.js
const gulp = require('gulp'); const plugins = require("gulp-load-plugins")({ pattern: '*' //讓gulp-load-plugins插件能匹配除了gulp插件以外的其餘插件 }); const fileinclude = require('gulp-file-include'); // 合併 html 文件 const cleanCSS = require('gulp-clean-css'); const path = require('path'); const webpack = require('webpack-stream'); const browserSync = require('browser-sync'); const reload = browserSync.reload; const proxy = require('http-proxy-middleware'); var sftp = require('gulp-sftp'); var isDev = !(process.env.NODE_ENV === 'production'); console.log('process.env.NODE_ENV',process.env.NODE_ENV) // html 中環境變量 const envOptions = (isDev) => { return { serverStaticPath: isDev ? './' : 'http://portal.yanxiu.com/statics/2018_yunnan_university/' } }; // 文件目錄結構 const pathDir = { html: { from: './src/*.html', to: './dist' }, less: { from: './src/assets/less/*.less', to: './dist/css' }, js: { from: './src/assets/js/*.js', to: './dist/js' } } gulp.task('html', () => { return gulp.src(pathDir.html.from) .pipe(fileinclude({ prefix: '@@', basepath: '@file' })) .pipe(plugins.template(envOptions(isDev))) // .pipe(plugins.changed(pathDir.html.to)) .pipe(gulp.dest(pathDir.html.to)) .pipe(reload({ stream:true })); }); gulp.task('less', () => { return gulp.src(pathDir.less.from) .pipe(plugins.less()) // .pipe(plugins.watch(path.LESS)) //只從新編譯被更改過的文件 .pipe(plugins.autoprefixer({ browsers: ['last 5 version', 'Android >= 4.0'], cascade: true, //是否美化屬性值 remove: true //是否去掉沒必要要的前綴 })) .pipe(cleanCSS({ compatibility: 'ie7', keepSpecialComments: '*' //保留全部特殊前綴 })) .pipe(gulp.dest(pathDir.less.to)) .pipe(reload({ stream:true })); }); gulp.task('js', () => { return gulp.src(pathDir.js.from) // .pipe(plugins.changed(path.JSDIR)) .pipe(webpack({ config: require('./webpack.config.js') })) .pipe(gulp.dest(pathDir.js.to)) .pipe(reload({ stream:true })); }); gulp.task('copy', () => { return gulp.src('./src/common/**') .pipe(gulp.dest('./dist/')) }); // 測試 - 上傳至服務器 gulp.task('dev-build', () => { return gulp.src('./dist/**') .pipe(sftp({ remotePath: '/App/web/phpcms/test/yxb-web', host: '192.168.11.11', user: 'root', pass: '123456' })) }) const taskArr = ['html', 'less', 'js', 'copy']; // 確保數組裏面的任務完成以後,再運行serve任務 gulp.task("serve", taskArr, () => { // const aipProxy = proxy('/api', { // target: 'http://orz.yanxiu.com', // changeOrigin: true, // pathRewrite: { // '^/api' : '/' // }, // }); browserSync.init({ server : { baseDir: './dist', index: 'login_teacher.html' // , // middleware: [aipProxy] }, port: 8080 }); gulp.watch([pathDir.html.from, './src/part/tpl/*.html'], ['html']); gulp.watch([pathDir.less.from, './src/part/less/*.less'], ['less']); gulp.watch([pathDir.js.from, './src/assets/tools/*.js'], ['js']); gulp.watch('./src/common/*/*.*', ['copy']) });
{ "name": "second", "version": "1.0.0", "description": "", "main": "gulpfile.js", "dependencies": { "axios": "^0.18.0", "yanxiu-passport": "^1.0.6" }, "devDependencies": { "@babel/core": "^7.1.2", "@babel/preset-env": "^7.1.0", "babel-loader": "^8.0.4", "browser-sync": "^2.24.5", "cross-env": "^5.2.0", "gulp": "^3.9.1", "gulp-autoprefixer": "^5.0.0", "gulp-changed": "^3.2.0", "gulp-clean-css": "^3.10.0", "gulp-file-include": "^2.0.1", "gulp-if": "^2.0.2", "gulp-less": "^3.5.0", "gulp-load-plugins": "^1.5.0", "gulp-postcss": "^8.0.0", "gulp-sftp": "^0.1.5", "gulp-template": "^5.0.0", "gulp-uglify": "^3.0.1", "gulp-watch": "^5.0.0", "http-proxy-middleware": "^0.19.0", "path": "^0.12.7", "webpack": "^4.23.1", "webpack-stream": "^5.1.1" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "cross-env NODE_ENV=development gulp serve", "build": "cross-env NODE_ENV=production gulp serve" }, "author": "", "license": "ISC" }
const path = require('path'); module.exports = { mode: 'development', entry: { index: './src/assets/js/index.js', login: './src/assets/js/login.js' }, output: { filename: '[name].js', chunkFilename: '[name].bundle.js', path: path.resolve(__dirname, 'dist/js') }, module: { rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components|common)/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] } } } ] } };
<!DOCTYPE html> <html lang="en"> <head> @@include('./part/tpl/meta.html', { "title": "首頁" }) <link rel="stylesheet" href="./css/swiper.min.css"> <link rel="stylesheet" href="./css/index.css"> </head> <body> @@include('./part/tpl/header-nav.html') @@include('./part/tpl/banner-swiper.html') <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="./js/bootstrap.min.js"></script> <script src="./js/swiper.min.js"></script> <script src="./js/index.js"></script> </body> </html>
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="./favicon.ico"> <title>@@title</title> <link href="./css/bootstrap.min.css" rel="stylesheet">
import passport from 'yanxiu-passport'; import { _axios } from './tools/axios' const $techUser = document.querySelector("#teacherUser"), $techPwd = document.querySelector("#teacherPwd"), $techRemb = document.querySelector("#teacherRemb"); passport.init({ passportHost: 'orz.yanxiu.com' }); function login () { passport.login({ 'appKey': 'f749edf6-bc39-6ef9-8f81-158se5fds842', 'path': '/uc/newLogin', 'data': { 'type': 'ALL'// IDCARD,PASSPORT,MOBILE,ALL }, 'passport': $techUser.value, 'password':$techPwd.value }).then(err => { debugger if(!err) { console.log('登陸成功'); _axios({ url: '/pxt/platform/data.api?method=app.platform.getUserRoles', method: 'get', params: { 'platId': '101' } }).then(response => { console.log('users', response) }) } else { if(err.code === 16){ err.msg = "帳號或密碼錯誤"; } console.log(err.msg || '登陸失敗') } }); } $("#loginBtn").click(() => { login(); });
axios.jsjavascript
這個配置方案,支持less、postcss的autoprefixer、代碼壓縮、es六、模板引用等。
下面是部分知識點的說明。php
cross-envcss
npm install --save-dev cross-env
// package.json { // ... "scripts": { "dev": "cross-env NODE_ENV=development gulp serve", "build": "cross-env NODE_ENV=production gulp serve" } }
// gulpfile.js var isDev = !(process.env.NODE_ENV === 'production');
webpack-stream
babel-loaderhtml
複用 html 公用部分
gulp-file-includejava
gulp-template
本項目中,主要用來處理 html 文件中,不一樣環境下 js、css文件引入路徑前綴的動態編譯問題。node
http-proxy-middlewarejquery
gulp-sftpwebpack
// 測試 - 上傳至服務器 gulp.task('dev-build', () => { return gulp.src('./dist/**') // 上傳 dist 文件夾下全部內容 .pipe(sftp({ remotePath: '/App/web/phpcms/test/yxb-web', // 上傳目錄 host: '192.168.11.11', // 服務器地址 user: 'root', // 用戶名 pass: '123456' // 密碼 })) })