Repository navigation
Expand file tree
/
Copy pathbuild-dev.js
More file actions
159 lines (134 loc) · 4.52 KB
/
Copy pathbuild-dev.js
File metadata and controls
159 lines (134 loc) · 4.52 KB
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
// Dev build script
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const sass = require('sass');
const wpExternalsPlugin = {
name: 'wp-externals',
setup(build) {
build.onResolve({ filter: /^@wordpress\// }, args => {
const moduleName = args.path.replace('@wordpress/', '');
return {
path: args.path,
namespace: 'wp-external'
};
});
build.onLoad({ filter: /.*/, namespace: 'wp-external' }, args => {
const moduleName = args.path.replace('@wordpress/', '');
const wpGlobal = moduleName.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
return {
contents: `module.exports = window.wp['${wpGlobal}']`,
loader: 'js'
};
});
}
};
function getEntryPoints(dir) {
const files = fs.readdirSync(dir);
const entryPoints = [];
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
entryPoints.push(...getEntryPoints(fullPath));
} else if (file.endsWith('.ts') || file.endsWith('.tsx')) {
entryPoints.push(fullPath);
}
});
return entryPoints;
}
function getScssFiles(dir) {
if (!fs.existsSync(dir)) {
console.log(`Directory ${dir} does not exist, skipping SCSS compilation`);
return [];
}
const files = fs.readdirSync(dir);
const scssFiles = [];
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
scssFiles.push(...getScssFiles(fullPath));
} else if (file.endsWith('.scss') && !file.startsWith('_')) {
scssFiles.push(fullPath);
}
});
return scssFiles;
}
function compileScss(filePath) {
try {
const result = sass.compile(filePath, {
sourceMap: true,
style: 'expanded'
});
const fileName = path.basename(filePath, '.scss') + '.css';
const outputPath = path.join('dist', 'css', fileName);
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, result.css);
console.log(`✓ Compiled: ${filePath} → ${outputPath}`);
} catch (error) {
console.error(`✗ Error compiling ${filePath}:`, error.message);
}
}
function watchScss(dir) {
const scssFiles = getScssFiles(dir);
scssFiles.forEach(file => {
compileScss(file);
fs.watch(file, (eventType) => {
if (eventType === 'change') {
console.log(`\nChange detected in ${file}`);
compileScss(file);
}
});
});
fs.watch(dir, { recursive: true }, (eventType, filename) => {
if (filename && filename.endsWith('.scss')) {
const fullPath = path.join(dir, filename);
if (fs.existsSync(fullPath) && !path.basename(filename).startsWith('_')) {
console.log(`\nChange detected in ${fullPath}`);
const allScssFiles = getScssFiles(dir);
allScssFiles.forEach(compileScss);
} else if (path.basename(filename).startsWith('_')) {
console.log(`\nPartial changed: ${filename}, recompiling all SCSS...`);
const allScssFiles = getScssFiles(dir);
allScssFiles.forEach(compileScss);
}
}
});
}
const scriptEntryPoints = getEntryPoints('src/scripts/main');
const scssFiles = getScssFiles('src/styles');
const buildConfig = {
entryPoints: scriptEntryPoints,
bundle: true,
minify: false,
sourcemap: true,
outdir: 'dist/main',
plugins: [wpExternalsPlugin],
target: 'es2020',
format: 'iife',
loader: {
'.tsx': 'tsx',
'.ts': 'ts',
'.png': 'file',
'.jpg': 'file',
'.jpeg': 'file',
'.gif': 'file',
'.svg': 'file',
'.webp': 'file',
'.woff': 'file',
'.woff2': 'file',
'.ttf': 'file',
'.eot': 'file',
},
};
esbuild.context(buildConfig).then(ctx => {
ctx.watch();
console.log('Watching for changes...');
console.log(`Watching ${scriptEntryPoints.length} TypeScript file(s)`);
console.log(`Watching ${scssFiles.length} SCSS file(s)`);
watchScss('src/styles');
}).catch(() => process.exit(1));