-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
135 lines (118 loc) · 4.33 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
const gulp = require('gulp');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const combine = require('gulp-scss-combine');
const ts = require('gulp-typescript');
const typescript = require('typescript');
const fs = require('fs-extra');
const path = require('path');
const configs = (() => {
const foundryConfigFileName = 'foundryconfig.json';
const configPath = path.resolve(process.cwd(), foundryConfigFileName);
return fs.existsSync(configPath) ? fs.readJSONSync(configPath) : {};
})();
const createTransformer = () => {
const 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;
}
return (context) => {
return (node) => {
/**
* @param {typescript.Node} node
*/
const 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);
};
};
}
const tsProject = ts.createProject('tsconfig.json', {
getCustomTransformers: (_program) => ({
after: [createTransformer()],
}),
});
gulp.task('compile-and-copy-ts', () => {
return gulp.src('./src/scripts/**/*.ts')
.pipe(tsProject()).js
.pipe(gulp.dest('./dist/scripts'));
});
const sassOptions = sass(
{
outputStyle: 'compressed'
}).on('error', sass.logError);
gulp.task('compile-minify-and-copy-sass', () => {
const { projectName } = configs;
return gulp.src('./src/styles/**/*.scss')
.pipe(combine())
.pipe(concat(projectName))
.pipe(sassOptions)
.pipe(gulp.dest('./dist/styles'));
});
gulp.task('move-non-convertible-files', () => {
return gulp.src([
'./src/**/*.*',
'!./src/scripts/**/*.ts',
'!./src/styles/**/*.scss'
]).pipe(gulp.dest('./dist'));
});
gulp.task('link', () => {
const distFolderName = 'dist';
const distPath = path.resolve(process.cwd(), distFolderName);
const { projectName, modulesPath } = configs;
if (!(fs.existsSync(distPath) && modulesPath && projectName)) return;
return gulp.src(`${distFolderName}/`)
.pipe(gulp.symlink(`${modulesPath}${projectName}`));
});
gulp.task('build', gulp.series(
'move-non-convertible-files',
'compile-and-copy-ts',
'compile-minify-and-copy-sass'
));
gulp.task('build-watch', () => {
gulp.watch([
'./src/**/*.*',
'!./src/scripts/**/*.ts',
'!./src/styles/**/*.scss'
], gulp.series('move-non-convertible-files'));
gulp.watch('./src/scripts/**/*.ts', gulp.series('compile-and-copy-ts'));
gulp.watch('./src/styles/**/*.scss', gulp.series('compile-minify-and-copy-sass'));
});