1. var path = require('path');
    2. var fs = require("fs");
    3. var glob = require('glob');
    4. var Parser = require('xml2js').Parser();
    5. // 获取 src/js下 所有.js文件
    6. var files = glob.sync('src/js/**/*.js');
    7. var entries = {};
    8. files.forEach(function(filepath) {
    9. // 取倒数第二层(view下面的文件夹)做包名
    10. var filepathNotSrc = filepath.substring(filepath.indexOf('/') + 1, filepath.length);
    11. var filepathName = filepathNotSrc.substring(0, filepathNotSrc.length - 3);
    12. entries[filepathName] = './' + filepath;
    13. });
    14. var deleteFolderRecursive = function(path) {
    15. var files = [];
    16. if( fs.existsSync(path) ) {
    17. files = fs.readdirSync(path);
    18. files.forEach(function(file,index){
    19. var curPath = path + "/" + file;
    20. if(fs.statSync(curPath).isDirectory()) { // recurse
    21. deleteFolderRecursive(curPath,function(err){
    22. if(err){
    23. throw err;
    24. }
    25. });
    26. } else { // delete file
    27. fs.unlinkSync(curPath);
    28. }
    29. });
    30. fs.rmdirSync(path);
    31. }
    32. };
    33. function readFile(path, newPath){
    34. var Suffix = path.substring(path.lastIndexOf('.') + 1, path.length);
    35. if(Suffix === 'html') {
    36. fs.readFile(path,'utf8',function(err, data) {
    37. if (err) {
    38. console.log("读取失败");
    39. return false;
    40. }
    41. // Parser.parseString(data.toString(), function (error, result) {
    42. // if (error) {
    43. // console.log(path);
    44. // console.log(error);
    45. // return false;
    46. // }
    47. // // console.dir(result);
    48. // });
    49. writeFile(data, newPath);
    50. });
    51. } else {
    52. fs.readFile(path, function(err, data) {
    53. if (err) {
    54. console.log("读取失败");
    55. return false;
    56. }
    57. writeFile(data, newPath);
    58. });
    59. }
    60. }
    61. function writeFile(data,path){
    62. fs.writeFile(path, data, function(error){
    63. if(error){
    64. throw error;
    65. }else{
    66. // console.log("文件已保存");
    67. }
    68. });
    69. }
    70. var addFolderRecursive = function(srcPath, distPath) {
    71. var files = [];
    72. if( fs.existsSync(srcPath) ) {
    73. files = fs.readdirSync(srcPath);
    74. files.forEach(function(file,index){
    75. var curPath = srcPath + "/" + file;
    76. var newPath = distPath + "/" + file;
    77. if(fs.statSync(curPath).isDirectory()) {
    78. fs.exists(newPath, function( exists ){
    79. if (!exists ) {
    80. fs.mkdirSync(newPath);
    81. }
    82. });
    83. addFolderRecursive(curPath,newPath);
    84. } else {
    85. var data = readFile(curPath,newPath);
    86. }
    87. });
    88. }
    89. }
    90. deleteFolderRecursive(path.join(__dirname, "./dist"));
    91. fs.mkdirSync(path.join(__dirname, "./dist"));
    92. addFolderRecursive(path.join(__dirname, "./src"), path.join(__dirname, "./dist"));
    93. module.exports = {
    94. entry: entries,
    95. output: {
    96. path: path.join(__dirname, "./dist/"), //文件输出目录
    97. filename: '[name].js',
    98. chunkFilename: '[id].[hash].common.js'
    99. },
    100. module: {
    101. loaders: [
    102. { test: /\.css$/, loader: 'style!css' },
    103. {
    104. test: /\.js$/, // 正则表达式
    105. exclude: /(node_modules|bower_components)/, // 不包含的意思
    106. loader: 'babel-loader', // 加载器
    107. query: { // 查询 此为查询属性
    108. presets: ['es2015'], // 预设参数
    109. plugins: ['transform-runtime']
    110. }
    111. },
    112. {
    113. test: /\.((woff2?|svg)(\?v=[0-9]\.[0-9]\.[0-9]))|(woff2?|svg|jpe?g|png|gif|ico)$/,
    114. loaders: [
    115. // 小于10KB的图片会自动转成dataUrl url? 表示使用url加载器 limit 表示限制 此限制大小未10kb
    116. 'url?limit=10240&name=img/[hash:8].[name].[ext]',
    117. 'image?{bypassOnDebug:true, progressive:true,optimizationLevel:3,pngquant:{quality:"65-80",speed:4}}'
    118. ]
    119. },
    120. {
    121. test: /\.((ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9]))|(ttf|eot)$/,
    122. loader: 'url?limit=10000&name=fonts/[hash:8].[name].[ext]'
    123. },
    124. ]
    125. },
    126. resolve: {
    127. modulesDirectories: ['node_modules', 'bower_components', 'web_modules'] // 告诉我们先找 node_modules 在找 bower_components 最后找web_modules 寻找第三方模块
    128. }
    129. };
    1. // gulp.js
    2. var gulp = require('gulp');
    3. var md5 = require('gulp-md5-plus');
    4. var cssmin = require('gulp-minify-css');
    5. var uglify = require('gulp-uglify');
    6. var del = require('del');
    7. var spriter = require('gulp-css-spriter');
    8. var base64 = require('gulp-css-base64');
    9. var path = require('path');
    10. var fs = require("fs");
    11. var deleteFolderRecursive = function(path) {
    12. var files = [];
    13. if( fs.existsSync(path) ) {
    14. files = fs.readdirSync(path);
    15. files.forEach(function(file,index){
    16. var curPath = path + "/" + file;
    17. if(fs.statSync(curPath).isDirectory()) { // recurse
    18. deleteFolderRecursive(curPath,function(err){
    19. if(err){
    20. throw err;
    21. }
    22. });
    23. } else { // delete file
    24. fs.unlinkSync(curPath);
    25. }
    26. });
    27. fs.rmdirSync(path);
    28. }
    29. };
    30. // deleteFolderRecursive(path.join(__dirname, "./dist/img"));
    31. deleteFolderRecursive(path.join(__dirname, "./dist/css"));
    32. // deleteFolderRecursive(path.join(__dirname, "./dist/js"));
    33. // gulp.task('clean', function(cb) {
    34. // del(['dist/css/*.css', 'dist/js/main/*.js'], cb)
    35. // });
    36. gulp.task('md5:js', function (done) {
    37. gulp.src('dist/js/main/*.js')
    38. .pipe(uglify())
    39. .pipe(md5(10, 'dist/**/*.html'))
    40. .pipe(gulp.dest('dist/js/main'))
    41. .on('end', done);
    42. });
    43. gulp.task('md5:css', function (done) {
    44. var timestamp = +new Date();
    45. gulp.src('src/css/*.css')
    46. // .pipe(spriter({
    47. // spriteSheet: 'dist/images/spritesheet' + timestamp + '.png',
    48. // pathToSpriteSheetFromCSS: '../images/spritesheet' + timestamp + '.png',
    49. // spritesmithOptions: {
    50. // padding: 10
    51. // }
    52. // }))
    53. // .pipe(base64())
    54. .pipe(cssmin({
    55. advanced: false,//类型:Boolean 默认:true [是否开启高级优化(合并选择器等)]
    56. compatibility: 'ie7',//保留ie7及以下兼容写法 类型:String 默认:''or'*' [启用兼容模式; 'ie7':IE7兼容模式,'ie8':IE8兼容模式,'*':IE9+兼容模式]
    57. keepBreaks: true,//类型:Boolean 默认:false [是否保留换行]
    58. keepSpecialComments: '*'
    59. //保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀,如果不加这个参数,有可能将会删除你的部分前缀
    60. }))
    61. .pipe(md5(10, 'dist/**/*.html'))
    62. .pipe(gulp.dest('dist/css'))
    63. .on('end', done);
    64. });
    65. gulp.task('default', function (done) {
    66. gulp.run('md5:js', 'md5:css');
    67. });