gulp+browserify前端模块化
来源:互联网 发布:codeforces难度知乎 编辑:程序博客网 时间:2024/05/28 06:06
包括js模块化,js压缩,css压缩,html模块化等。
github源码地址:https://github.com/xutongbao/gulp-es6
1.目录结构
2.index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>gulp 模版</title> <!-- build:mycss --> <link rel="stylesheet" href="style1.css"> <link rel="stylesheet" href="style2.css"> <!-- endbuild --></head><body><div id='mytpl'> <!-- build:mytpl1 --> <div>原内容1</div> <!-- endbuild --> <!-- build:mytpl2 --> <div>原内容2</div> <!-- endbuild --> <!-- build:mytpl3 --> <div>原内容3</div> <!-- endbuild --> <!-- build:mytpl4 --> <div>原内容4</div> <!-- endbuild --> <!-- build:mytpl5 --> <div>原内容5</div> <!-- endbuild --> <!-- build:mytpl6 --> <div>原内容6</div> <!-- endbuild --> </div><button id='back'>上一步</button> <button id='next'>下一步</button><script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <script src="index.js"></script> <script src="main.js"></script></body></html>
3.gulpfile.js
var gulp = require("gulp");var babel = require("gulp-babel");var watch = require("gulp-watch");var htmlreplace = require('gulp-html-replace');var fs = require("fs");let cleanCSS = require('gulp-clean-css');var concat = require('gulp-concat'); var gulpsync = require('gulp-sync')(gulp); var gulpbrowserify = require('gulp-browserify');var browserify = require('browserify');var babelify = require('babelify');var sourcemaps = require('gulp-sourcemaps');var source = require('vinyl-source-stream');var buffer = require('vinyl-buffer');var standalonify = require('standalonify');var uglify = require('gulp-uglify');gulp.task('default',gulpsync.sync(['babel', 'minify-css', 'mytpl', 'browserify']),function () { });//监听文件是否修改gulp.task('watch', () => { gulp.watch('src/*.*', ['default'])});gulp.task("babel", function () { return gulp.src(["src/People.js", "src/index.js"]) .pipe(concat('index.js')) .pipe(babel()) .pipe(uglify({mangle: false, compress: {properties: false}, output: {quote_keys: true}})) .pipe(gulp.dest("dist"));});gulp.task('gulpbrowserify', function() { gulp.src('src/main.js', { read: false }) .pipe(gulpbrowserify({ insertGlobals : true, debug : true })) //.pipe(babel()) .pipe(gulp.dest('dist'))});gulp.task('browserify', function() { var b = browserify({ entries: "src/main.js", debug: true //告知browserify在运行同时生成内联sourcemap用于调试}); return b.plugin(standalonify, { //使打包后的js文件符合UMD规范并指定外部依赖包name: 'FlareJ',deps: {'nornj': 'nj','react': 'React','react-dom': 'ReactDOM'}}).transform(babelify, { presets: [ 'es2015' //转换es6代码 ] }).bundle().pipe(source("main.js")).pipe(buffer()).pipe(uglify({mangle: false, compress: {properties: false}, output: {quote_keys: true}})).pipe(sourcemaps.init({loadMaps: true})).pipe(sourcemaps.write(".")).pipe(gulp.dest("./dist"));});var staticize = {getFileContent : function(tplPathName){let filePath = tplPathName; var readFile = ''; try { fs.accessSync(filePath) readFile = fs.readFileSync(filePath,"utf8"); return readFile; } catch (err) { console.log(filePath + ' is not exit '); return ''; }}};gulp.task('mytpl', function() {var data = {'mytpl1': staticize.getFileContent('src/index1.tpl'),'mytpl2': staticize.getFileContent('src/index2.tpl'),'mytpl3': staticize.getFileContent('src/index3.tpl'),'mytpl4': staticize.getFileContent('src/index4.tpl'),'mytpl5': staticize.getFileContent('src/index5.tpl'),'mytpl6': staticize.getFileContent('src/index6.tpl'),'mycss': 'styles.css'} gulp.src('src/index.html') .pipe(htmlreplace(data)) .pipe(gulp.dest('dist'));});gulp.task('minify-css', () => { return gulp.src('src/*.css') .pipe(concat('styles.css')) .pipe(cleanCSS({compatibility: 'ie8'})) .pipe(gulp.dest('dist'));});
4.package.json
{ "name": "demo2", "version": "1.0.0", "main": "index.js", "devDependencies": { "babel-core": "^6.26.0", "babel-preset-es2015": "^6.24.1", "babelify": "^8.0.0", "browserify": "^14.5.0", "gulp": "^3.9.1", "gulp-babel": "^7.0.0", "gulp-browserify": "^0.5.1", "gulp-clean-css": "^3.9.0", "gulp-concat": "^2.6.1", "gulp-html-replace": "^1.6.2", "gulp-sourcemaps": "^2.6.1", "gulp-sync": "^0.1.4", "gulp-uglify": "^3.0.0", "gulp-watch": "^4.3.11", "standalonify": "^0.1.3", "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^1.1.0" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "description": ""}
5.foo.js
let variable = 8;let sum = (a, b = 6) => (a + b);let square = (b) => { return b * b;};module.exports.variable = variable;module.exports.sum = sum;module.exports.square = square;
6.main.js
var bar = require('./foo')console.log(bar); // Objectconsole.log(bar.variable); // 8console.log(bar.sum(1)); // 7console.log(bar.square(5)); // 25$(function() {console.log('jquery');})
7.index1.tpl
<div id="tpl1" class="block" data-show=true>模版1</div>
8..babelrc
{ "presets": [ "es2015" ], "plugins": []}
阅读全文
1 0
- gulp+browserify前端模块化
- 前端模块化开发学习之gulp&browserify篇
- 使用npm+gulp+browserify网页前端开发
- gulp-前端模块及依赖管理的新选择:Browserify
- 前端模块化方案的比较(webpack/require.js/seajs/browserify
- Webpack、Browserify和Gulp
- 前端模块化之模块化工具gulp和webpack的安装。
- CommonJS-Browserify模块化教程
- 前端构建工具、browserify、gulp的组合使用及尝试感言
- angular + browserify + gulp + bower + less
- React之,gulp,browserify搭建
- 前端的四种模块化方案(webpack/require.js/seajs/browserify
- [Nodejs]Gulp + Browserify: The Everything Post
- webpack开发工具 (gulp、browserify、webpack)
- 使用gulp+Browserify构建React应用
- gulp/grunt和browserify/webpack的区别
- gulp/grunt和browserify/webpack说明
- 前端模块化
- NOIP2017普及组★棋盘
- 在JAVA向Mysql的数据库中插入数据过大所遇到的问题
- 统计学习方法 第3章 k近邻法
- 人生苦短,Python 当歌!
- Android 常用开发工具类之 AppVersionUtil (获取应用版本信息工具)
- gulp+browserify前端模块化
- Java注解
- eclipse导入约束
- 使用openssl生成https协议证书
- Spring注解
- SpringBoot自我整理学习1(springboot入门)
- Kubernetes受欢迎的由头
- Indellij IDEA项目启动报错Error:java: Compilation failed: internal java compiler error
- Google 搜索引擎设置搜索结果,在新的标签页打开