-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
129 lines (110 loc) · 3.92 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { src, dest } from "gulp";
import gulp from "gulp";
import ejs from "gulp-ejs";
import rename from "gulp-rename";
import browserSync from "browser-sync";
import * as dartSass from "sass";
import gulpSass from "gulp-sass";
import sourcemaps from "gulp-sourcemaps";
import cleanCSS from "gulp-clean-css";
import uglify from "gulp-uglify";
import rev from "gulp-rev";
import revReplace from "gulp-rev-replace";
import fs from "fs";
import autoprefixer from "gulp-autoprefixer";
import groupCssMediaQueries from "gulp-group-css-media-queries"; // パッケージをインポート
import { navData, cardData } from "./src/data/data.js"; // ✅ データをインポート
const bs = browserSync.create();
const sass = gulpSass(dartSass);
// EJS Compilation Task
const CompileEjs = () => {
let manifest = {};
if (fs.existsSync("rev-manifest.json")) {
manifest = JSON.parse(fs.readFileSync("rev-manifest.json", "utf8"));
}
return gulp
.src("src/views/index.ejs")
.pipe(
ejs(
{
title: "サイトタイトル",
navData,
cardData,
cssPath: manifest["css/main.css"] || "css/main.css",
jsPath: manifest["js/index.js"] || "js/index.js",
},
{},
{ ext: ".html" }
)
)
.pipe(rename({ extname: ".html" }))
.pipe(gulp.dest("dist"));
};
// SCSS Compilation and Cache Busting Task
const CompileSass = () => {
return gulp
.src("src/styles/main.scss")
.pipe(sourcemaps.init())
.pipe(sass().on("error", sass.logError))
.pipe(autoprefixer({ overrideBrowserslist: ["last 2 versions"], grid: true, cascade: false }))
.pipe(cleanCSS())
.pipe(groupCssMediaQueries()) // メディアクエリをグループ化
.pipe(rev())
.pipe(rename("main.css"))
.pipe(sourcemaps.write(".")) // ソースマップを同じフォルダに出力
.pipe(gulp.dest("dist/css"))
.pipe(rev.manifest("rev-manifest.json", { merge: true }))
.pipe(gulp.dest(".")); // マニフェストをルートディレクトリに出力
};
// JavaScript Minification and Cache Busting Task
const MinifyJs = () => {
return gulp
.src("src/js/index.js")
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(rev())
.pipe(rename("index.js"))
.pipe(sourcemaps.write(".")) // ソースマップを同じフォルダに出力
.pipe(gulp.dest("dist/js"))
.pipe(rev.manifest("rev-manifest.json", { merge: true }))
.pipe(gulp.dest(".")); // マニフェストをルートディレクトリに出力
};
// 画像コピータスク
const CopyImages = () => {
return gulp
.src("src/img/**/*",{encoding: false}) // src/img 配下のすべてのファイルを取得
.pipe(gulp.dest("dist/img")); // dist/img にコピー
};
// ファビコンをdistにコピー
const CopyFavicon = () => {
return src("favicon.ico",{encoding:false}) // ルートディレクトリのfaviconを指定
.pipe(dest("dist")); // distフォルダに出力
};
// HTML Cache Busting Links
const RevUpdateHtml = () => {
const manifest = gulp.src("rev-manifest.json", { allowEmpty: true });
return gulp
.src("dist/**/*.html")
.pipe(revReplace({ manifest }))
.pipe(gulp.dest("dist"));
};
// Server and File Watcher
const Server = () => {
bs.init({
server: {
baseDir: "dist", // サーバーのルートディレクトリ
},
open: true,
notify: false,
});
// ファイル変更を監視して自動更新
gulp.watch("src/views/**/*.ejs", gulp.series(CompileEjs, RevUpdateHtml)).on("change", bs.reload);
gulp.watch("src/styles/**/*.scss", gulp.series(CompileSass, RevUpdateHtml)).on("change", bs.reload);
gulp.watch("src/js/**/*.js", gulp.series(MinifyJs, RevUpdateHtml)).on("change", bs.reload);
};
// Build Task
export const build = gulp.series(CompileSass, MinifyJs, CompileEjs, RevUpdateHtml, CopyImages, CopyFavicon);
// Watch Task
export const watch = gulp.series(build, Server);
// Default Task
export default watch;