-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.mts
108 lines (100 loc) · 3.78 KB
/
gulpfile.mts
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
import * as path from "node:path";
import browserSync from "browser-sync";
import gulp from "gulp";
import autoprefixer from "gulp-autoprefixer";
import eslint from "gulp-eslint-new";
import sassFactory from "gulp-sass";
import stylelint from "gulp-stylelint-esm";
import tap from "gulp-tap";
import * as sassCompiler from "sass-embedded";
import type { RawSourceMap } from "source-map-js";
import type { TaskCallback } from "undertaker";
import type Vinyl from "vinyl";
const sass = sassFactory(sassCompiler);
const jsDir = "public/editor/js/";
const jsSrc = jsDir + "**/*.js";
const lintJS = () =>
gulp.src(jsSrc)
.pipe(eslint({ cwd: path.resolve(process.cwd(), jsDir) }))
.pipe(eslint.format());
gulp.task("lint_js", lintJS);
const prefixerOptions: autoprefixer.Options =
{
flexbox: "no-2009"
};
const sassDir = "public/editor/sass/";
const sassSrc = sassDir + "**/*.scss";
const sassDest = "public/editor/css/";
const lintSass = () =>
gulp.src(sassSrc)
.pipe(stylelint(
{
quietDeprecationWarnings: true,
failAfterError: false,
reporters: [{ formatter: "string", console: true }]
}));
gulp.task("lint_sass", lintSass);
const toPosixPath = path.sep !== path.posix.sep ?
(relativePath: string) => path.posix.join(...relativePath.split(path.sep)) :
(relativePath: string) => relativePath;
const compileSass = () =>
gulp.src(sassSrc, { sourcemaps: true })
.pipe(sass().on("error", sass.logError.bind(sass)))
.pipe(tap((file: Vinyl & { sourceMap?: RawSourceMap }) =>
{
if (file.sourceMap?.sources.length)
{
const relativeInitialPath = toPosixPath(path.relative(sassDir, file.history[0]!));
const prefixPath = path.posix.dirname(relativeInitialPath);
const prefix = prefixPath === '.' ? null : prefixPath + '/';
file.sourceMap.sources = file.sourceMap.sources.map((source) =>
{
if (prefix && source.startsWith(prefix))
{
source = source.substring(prefix.length);
}
if (source.startsWith('data:'))
{
return relativeInitialPath;
}
else if (source.startsWith('file:'))
{
const uriComponent = decodeURIComponent(new URL(source).pathname);
const absolutePath = path.sep === '\\' && uriComponent.startsWith('/') && uriComponent[2] === ':' ?
uriComponent.substring(1) : uriComponent;
return toPosixPath(path.relative(sassDir, absolutePath));
}
else
{
return source;
}
});
}
}))
.pipe(autoprefixer(prefixerOptions))
.pipe(gulp.dest(sassDest, { sourcemaps: '.' }));
const processSass = gulp.series(lintSass, compileSass);
gulp.task("sass", processSass);
const watch = (done: TaskCallback, browserSyncInstance?: browserSync.BrowserSyncInstance) =>
{
gulp.watch(jsSrc, lintJS);
gulp.watch(
sassSrc,
gulp.series(lintSass, browserSyncInstance ? () => compileSass().pipe(browserSyncInstance.stream()) : compileSass)
);
console.log("Watching for file changes...");
done();
};
gulp.task("watch", watch);
gulp.task("stream", (done: TaskCallback) =>
{
const browserSyncInstance = browserSync.create();
browserSyncInstance.init(
{
proxy: "http://localhost"
});
watch(done, browserSyncInstance);
});
const build = gulp.parallel(lintJS, processSass);
gulp.task("build", build);
gulp.task("default", gulp.series(build, watch));