-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-plugins.js
More file actions
91 lines (80 loc) · 3.35 KB
/
Copy pathbuild-plugins.js
File metadata and controls
91 lines (80 loc) · 3.35 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
import { build, defineConfig } from 'vite';
import path from 'path';
import { readdir, stat } from 'fs/promises';
import { existsSync, rmSync } from 'fs';
const root = process.cwd();
const distDir = path.resolve(root, 'dist');
const pluginTypes = ['editors', 'menu', 'validators', 'wizards'];
// Clear the dist folder if it exists.
function clearDistFolder(dir) {
if (existsSync(dir)) {
rmSync(dir, { recursive: true, force: true });
console.log(`Cleared output directory: ${dir}`);
} else {
console.log(`Output directory does not exist, so no clearing needed: ${dir}`);
}
}
async function buildAll() {
// Clear the dist directory before building.
clearDistFolder(distDir);
for (const pluginType of pluginTypes) {
const folderPath = path.resolve(root, 'apps/plugins/src', pluginType);
// Check if the current plugin type folder exists.
try {
await stat(folderPath);
} catch (error) {
console.warn(`Source folder does not exist for plugin type "${pluginType}" at: ${folderPath}`);
continue;
}
// Read the contents of the folder.
let entries;
try {
entries = await readdir(folderPath);
} catch (error) {
console.error(`Error reading folder at ${folderPath}:`, error);
continue;
}
// Filter for TypeScript files.
const tsFiles = entries.filter(file => file.endsWith('.ts'));
// Process each TypeScript file.
for (const file of tsFiles) {
const filePath = path.resolve(folderPath, file);
const baseName = path.basename(file, '.ts');
const pluginName = `${baseName.charAt(0).toUpperCase()}${baseName.slice(1)}Plugin`;
const outFileName = baseName.toLowerCase();
// Build configuration with output in a subfolder based on the plugin type.
// Externalize common libraries like lit-element, lit-html, and lit-translate.
const config = defineConfig({
build: {
outDir: distDir, // Global output folder is the root-level "dist"
lib: {
entry: filePath,
name: pluginName,
fileName: () => `${pluginType}/${outFileName}.js`,
formats: ['es'],
},
rollupOptions: {
output: {
inlineDynamicImports: true
},
},
target: 'esnext',
emptyOutDir: false,
configFile: false,
},
resolve: {
alias: {
'@openscd/compas-open-scd': path.resolve(root, 'libs/compas-open-scd'),
'@openscd/open-scd': path.resolve(root, 'libs/openscd/open-scd'),
'@openscd/core': path.resolve(root, 'libs/openscd/core'),
'@openscd/xml': path.resolve(root, 'libs/openscd/xml/src'),
},
},
});
console.log(`📦 Building Plugin ${pluginName} from ${pluginType}/${file}...`);
await build(config);
console.log(`✅ Built: ${pluginType}/${outFileName}.js`);
}
}
}
buildAll();