This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
140 lines (125 loc) · 3.31 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
130
131
132
133
134
135
136
137
138
139
140
const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const sourceMaps = require("gulp-sourcemaps");
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const glob = require("gulp-sass-glob");
const terser = require("gulp-terser");
const gulpIf = require("gulp-if");
const rename = require("gulp-rename");
const rollup = require("@rollup/stream");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const resolve = require("@rollup/plugin-node-resolve");
const typescript = require("@rollup/plugin-typescript");
const commonjs = require("@rollup/plugin-commonjs");
const fg = require("fast-glob");
const env = process.env.NODE_ENV || "development";
let rollupCache;
function isProduction()
{
return env === "production";
}
function styles(input, output, useGlob = false, name = "")
{
return gulp
.src(input)
.pipe(gulpIf(isProduction(), sourceMaps.init()))
.pipe(
sass.sync({
importer: useGlob ? glob : undefined,
includePaths: ["./assets/scss", "./node_modules", "./src"],
outputStyle: !isProduction() ? "compressed" : "expanded"
})
)
.pipe(postcss([autoprefixer]))
.pipe(
rename(function(path)
{
if(name)
{
path.basename = name;
}
path.basename += ".min";
})
)
.pipe(
gulpIf(!isProduction(), sourceMaps.write("./", { addComment: false }))
)
.pipe(gulp.dest(output));
}
function globStyles()
{
return styles("./src/**/*.scss", "./src/", true);
}
function baseStyle()
{
return styles("./assets/scss/index.scss", "./resources", false, "main");
}
function scripts(input, output, name = "")
{
const filename = input.split("/").pop();
const options = {
input: input,
output: {
name: filename,
format: "iife",
sourcemap: true
},
plugins: [resolve(), typescript({ sourceMap: true }), commonjs()],
cache: rollupCache
};
return rollup(options)
.pipe(source(filename))
.pipe(buffer())
.pipe(gulpIf(!isProduction(), sourceMaps.init()))
.pipe(
rename(function(path)
{
if(name)
{
path.basename = name;
}
path.basename += ".min";
path.extname = ".js";
})
)
.pipe(gulpIf(isProduction(), terser({ format: { comments: false } })))
.pipe(
gulpIf(!isProduction(), sourceMaps.write("./", { addComment: false }))
)
.pipe(gulp.dest(output));
}
function baseScript()
{
return scripts("./assets/ts/index.ts", "./resources", "main");
}
function globScripts()
{
const globs = fg.sync("./src/**/*.ts");
globs.map(function(file)
{
console.log(file);
const filename = file.split("/").pop();
const output = file.replace(filename, "");
return scripts(file, output);
});
return Promise.resolve();
}
gulp.task("watch", function(done)
{
gulp.watch(
["./assets/scss/**/*.scss", "./src/**/*.scss"],
{ ignoreInitial: false },
gulp.series("styles")
);
gulp.watch(
["./assets/ts/**/*.ts", "./src/**/*.ts"],
{ ignoreInitial: false },
gulp.series("scripts")
);
done();
});
gulp.task("styles", gulp.series(baseStyle, globStyles));
gulp.task("scripts", gulp.series(baseScript, globScripts));
gulp.task("default", gulp.parallel("styles", "scripts"));