This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
186 lines (165 loc) · 4.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const typescript = require("typescript");
const path = require("path");
const rimraf = require("rimraf");
const gulp = require("gulp");
const ts = require("gulp-typescript");
const sass = require("gulp-sass");
const sourcemaps = require("gulp-sourcemaps");
const cleanCSS = require("gulp-clean-css");
const autoprefixer = require("gulp-autoprefixer");
const concat = require("gulp-concat");
// const eslint = require("gulp-eslint");
/**
* TypeScript transformers
* @returns {typescript.TransformerFactory<typescript.SourceFile>}
*/
function createTransformer() {
/**
* @param {typescript.Node} node
*/
function shouldMutateModuleSpecifier(node) {
if (!typescript.isImportDeclaration(node) && !typescript.isExportDeclaration(node)) return false;
if (node.moduleSpecifier === undefined) return false;
if (!typescript.isStringLiteral(node.moduleSpecifier)) return false;
if (!node.moduleSpecifier.text.startsWith("./") && !node.moduleSpecifier.text.startsWith("../")) return false;
if (path.extname(node.moduleSpecifier.text) !== "") return false;
return true;
}
/**
* Transforms import/export declarations to append `.js` extension
* @param {typescript.TransformationContext} context
*/
function importTransformer(context) {
return (node) => {
/**
* @param {typescript.Node} node
*/
function visitor(node) {
if (shouldMutateModuleSpecifier(node)) {
if (typescript.isImportDeclaration(node)) {
const newModuleSpecifier = typescript.createLiteral(`${node.moduleSpecifier.text}.js`);
return typescript.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
node.importClause,
newModuleSpecifier
);
} else if (typescript.isExportDeclaration(node)) {
const newModuleSpecifier = typescript.createLiteral(`${node.moduleSpecifier.text}.js`);
return typescript.updateExportDeclaration(
node,
node.decorators,
node.modifiers,
node.exportClause,
newModuleSpecifier
);
}
}
return typescript.visitEachChild(node, visitor, context);
}
return typescript.visitNode(node, visitor);
};
}
return importTransformer;
}
const tsConfig = ts.createProject("tsconfig.json", {
// noImplicitAny: true,
// declaration: true,
// allowJs: true,
allowSyntheticDefaultImports: true,
getCustomTransformers: (_program) => ({
after: [createTransformer()],
}),
});
/**
* Cleaning the dist directory
*/
const clean = () => {
rimraf.sync(__dirname + "/dist/**/*");
};
/**
* Linting
*/
// const lint = (cb) => {
// gulp
// .src(["**/*.ts", "!node_modules/**"])
// .pipe(
// eslint({
// // configFile: __dirname + "/.eslintrc.json",
// })
// )
// .pipe(eslint.formatEach("visualstudio", process.stderr))
// .pipe(eslint.failAfterError());
// cb();
// };
/**
* Compiling
*/
const compileJavaScript = () => {
return gulp.src("src/**/*.js").pipe(gulp.dest("dist"));
};
const compileTypeScript = () => {
return gulp
.src("src/**/*.ts")
.pipe(sourcemaps.init())
.pipe(tsConfig())
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("dist"));
};
const compileScript = (cb) => {
clean();
compileJavaScript();
compileTypeScript();
cb();
};
const compileStyle = (cb) => {
return gulp
.src("src/**/*.sass")
.pipe(sass().on("error", sass.logError))
.pipe(cleanCSS())
.pipe(autoprefixer("last 2 version", "safari 5", "ie 8", "ie 9"))
.pipe(concat("style.min.css"))
.pipe(gulp.dest("dist/style"));
};
/**
* File/Folder processing
*/
const copyImages = (cb) => {
return gulp.src("./src/img/**/*").pipe(gulp.dest("./dist/img/"));
cb();
};
const copyLanguages = (cb) => {
gulp.src("./src/lang/**/*.json").pipe(gulp.dest("./dist/lang/"));
cb();
};
/**
* Watching
*/
const watchTypeScript = (cb) => {
gulp.watch("src/**/*.ts", { ignoreInitial: false }, compileTypeScript);
};
const watchJavaScript = (cb) => {
gulp.watch("src/**/*.js", { ignoreInitial: false }, compileJavaScript);
};
const watchScript = (cb) => {
watchTypeScript();
watchJavaScript();
cb();
};
const watchStyle = (cb) => {
gulp.watch("src/**/*.sass", { ignoreInitial: false }, compileStyle);
};
const watchImages = (cb) => {
gulp.watch("src/lang/*.json", { ignoreInitial: false }, copyImages);
};
const watchLanguages = (cb) => {
gulp.watch("./src/lang/**/*.json", { ignoreInitial: false }, copyLanguages);
};
const compile = gulp.parallel(compileScript, compileStyle, copyImages, copyLanguages);
compile.description = "compile all sources";
const watch = gulp.parallel(watchScript, watchStyle, watchImages, watchLanguages);
watch.description = "watch for changes to all source";
gulp.task("watch", watch);
gulp.task("build", compile);
// gulp.task("lint", lint);