-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
155 lines (131 loc) · 4.46 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
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
/* global process */
// ESBUILD PROJECT DEPENDENCIES
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar'); // Added chokidar for file watching
// Get command-line arguments
const args = process.argv.slice(2);
const isLocalBuild = args.includes('--local');
const isDevBuild = args.includes('--dev');
const isWatchBuild = args.includes('--watch');
// Directory paths
const srcDir = path.join(__dirname, 'src');
const distDir = path.join(__dirname, 'dist');
// Get the version from package.json
const packageJsonPath = path.join(__dirname, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const packageVersion = packageJson.version;
function getTimestamp() {
const now = new Date();
const month = String(now.getMonth());
const day = String(now.getDate());
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
return `${month}${day}${hours}${minutes}`;
}
// Add a timestamp only if it's a dev build
const timestamp = `${getTimestamp()}`
// Function to find all files in src directory
function findFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findFiles(filePath, fileList);
} else {
fileList.push(filePath);
}
});
return fileList;
}
// Copy non-CJS and non-CSS files to dist directory
function copyFile(src, dest) {
const destDir = path.dirname(dest);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
fs.copyFileSync(src, dest);
console.log(`Copied ${src} to ${dest}`);
}
// Update the version in manifest.json files
function updateManifestVersion(manifestPath) {
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
const oldVersion = manifest.version;
const version = isDevBuild ? `${packageVersion}${getTimestamp()}` : `${packageVersion}`;
const namespace = isDevBuild ? `qgds-bs5-dev` : `qgds-bs5`;
const namespaceLayout = isDevBuild ? `qgds-bs5-layout-dev` : `qgds-bs5-layout`;
if (!isLocalBuild) {
manifest.version = version;
}
if (manifestPath.includes("dist/layout/")){
manifest.namespace = namespaceLayout;
} else {
manifest.namespace = namespace;
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), 'utf-8');
console.log(`Updated version in ${manifestPath} to ${version} with namespace ${namespace}`);
}
function processFile(file) {
const relativePath = path.relative(srcDir, file);
const outFile = path.join(distDir, relativePath);
if (file.endsWith('.cjs')) {
esbuild.buildSync({
entryPoints: [file],
outfile: outFile,
minify: !isDevBuild,
sourcemap: isDevBuild,
treeShaking: true,
bundle: true,
format: 'cjs',
platform: 'node',
});
console.log(`Built ${file} to ${outFile}`);
} else if (file.endsWith('manifest.json')) {
// Update the version in manifest.json and copy to dist
const tempManifestPath = path.join(distDir, relativePath);
copyFile(file, tempManifestPath);
updateManifestVersion(tempManifestPath);
} else if (file.endsWith('.css')) {
// Process CSS files with esbuild
esbuild.buildSync({
entryPoints: [file],
outfile: outFile,
minify: !isDevBuild,
sourcemap: isDevBuild
});
console.log(`Processed CSS ${file} to ${outFile}`);
} else if (file.endsWith('.scss')) {
// Process SCSS files with esbuild and esbuild-sass-plugin
esbuild.buildSync({
entryPoints: [file],
outfile: outFile.replace('.scss', '.css'),
bundle: true,
minify: !isDevBuild,
sourcemap: isDevBuild,
plugins: [sassPlugin()],
});
console.log(`Processed SCSS ${file} to ${outFile.replace('.scss', '.css')}`);
} else {
// Copy non-CJS, non-CSS files
copyFile(file, outFile);
}
}
console.log("clean dist")
if (fs.existsSync("./dist")) {
fs.rmSync("./dist", { recursive: true });
}
const files = findFiles(srcDir);
files.forEach((file) => {
processFile(file);
});
if (isWatchBuild) {
chokidar.watch(srcDir, { ignoreInitial: true }).on('all', (event, filePath) => {
if (fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory()) {
console.log(`File ${event}: ${filePath}`);
processFile(filePath);
}
});
console.log('Watching for changes...');
}