-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathesbuild.js
130 lines (117 loc) · 3.6 KB
/
esbuild.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
import fs from 'fs';
import path from 'path';
import esbuild from 'esbuild';
import * as sentry from '@sentry/esbuild-plugin';
/**
* Ensures a directory exists. If not, creates it.
* @param {string} dirPath - The path to the directory.
*/
const ensureDirSync = (dirPath) => {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
};
/**
* Recursively copies files from source to destination with a filter option.
* @param {string} srcDir - The source directory.
* @param {string} destDir - The destination directory.
* @param {Function} filterFn - A function to filter which files to copy.
*/
const copySync = (srcDir, destDir, filterFn) => {
const entries = fs.readdirSync(srcDir, { withFileTypes: true });
// eslint-disable-next-line no-restricted-syntax
for (const entry of entries) {
const srcPath = path.join(srcDir, entry.name);
const destPath = path.join(destDir, entry.name);
if (entry.isDirectory()) {
ensureDirSync(destPath);
copySync(srcPath, destPath, filterFn);
} else if (!filterFn || filterFn(entry.name)) {
fs.copyFileSync(srcPath, destPath);
}
}
};
/**
* Copies specific files from source to destination based on a pattern.
* @param {string} srcDir - The source directory.
* @param {string} pattern - A wildcard pattern to filter files. e.g., '*.html'
* @param {string} destDir - The destination directory.
*/
const copyFiles = (srcDir, pattern, destDir) => {
const files = fs
.readdirSync(srcDir)
.filter((file) => file.match(new RegExp(pattern.replace('*', '.*'))));
files.forEach((file) => {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
fs.copyFileSync(srcPath, destPath);
});
};
/**
* Performs preparatory tasks for production mode.
* Clears the 'dist' directory, ensures it exists,
* and copies necessary files.
*/
const prebuildDist = () => {
if (fs.existsSync('dist')) {
fs.rmSync('build', { recursive: true });
}
ensureDirSync('dist');
copySync('src', 'dist', (file) => !file.endsWith('.js'));
};
/**
* Performs preparatory tasks for development mode.
* Clears the 'build' directory, ensures it exists,
* copies necessary files, and copies HTML pages from cypress.
*/
const prebuild = () => {
if (fs.existsSync('build')) {
fs.rmSync('build', { recursive: true });
}
ensureDirSync('build');
copySync('src', 'build', (file) => !file.endsWith('.js'));
copyFiles('cypress/pages', '*.html', 'build');
};
const files = fs
.readdirSync('src/js/', { recursive: true })
.filter((file) => file.endsWith('.js'));
if (process.env.NODE_ENV === 'development') {
prebuild();
} else {
prebuildDist();
}
const devOptions = {
bundle: true,
minify: process.env.MINIFY === 'true',
};
const prodOptions = {
bundle: true,
minify: true,
};
files.forEach((file) => {
const baseName = path.basename(file, '.js');
const dir = path.dirname(file);
if (process.env.NODE_ENV === 'development') {
esbuild.build({
...devOptions,
entryPoints: [`src/js/${file}`],
outfile: `build/js/${dir}/${baseName}.min.js`,
});
} else {
esbuild.build({
...prodOptions,
entryPoints: [`src/js/${file}`],
outfile: `dist/js/${dir}/${baseName}.min.js`,
sourcemap: true, // Source map generation must be turned on
plugins: [
// Put the Sentry esbuild plugin after all other plugins
sentry.sentryEsbuildPlugin({
applicationKey: 'smileid-web-client',
authToken: process.env.SENTRY_AUTH_TOKEN,
org: 'smile-identity',
project: 'web-client',
}),
],
});
}
});