-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter-custom-static.js
176 lines (144 loc) · 5.28 KB
/
adapter-custom-static.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { existsSync, mkdirSync, writeFileSync, copyFileSync, readFileSync, rmSync } from 'node:fs';
import { readdir } from 'node:fs/promises';
import { join, parse, relative, dirname } from 'node:path';
import * as sass from 'sass';
import glob from 'fast-glob';
// import { compile } from 'svelte/compiler';
async function cleanupPreviousBuild(directories) {
await Promise.all(directories.map(async (dir) => {
if (existsSync(dir)) {
console.log(`Cleaning up ${dir}`);
await rmSync(dir, { recursive: true, force: true });
}
}));
}
const directories = {
sassUtilities: 'sass-utilities',
utilities: 'src/utilities',
base: 'src/',
layout: 'src/layouts',
}
async function processScssFiles(sourceDir, outDir, distDir) {
const scssFiles = await glob('**/*.scss', {
cwd: sourceDir,
absolute: true
});
const customImporter = [
{
findFileUrl(url) {
const patterns = [
// { pattern: '../.../sass-utilities', path: 'src/styles/sass-utilities/_index.scss' },
{ pattern: '/base', path: 'src/styles/base/_index.scss' },
{ pattern: './layouts', path: 'src/layouts/_index.scss' }
];
const match = patterns.find(({ pattern }) => url.startsWith(pattern));
if (match) {
return new URL(`file://${join(process.cwd(), match.path)}`);
}
return null;
}
}
];
await Promise.all(scssFiles.map(async (scssFile) => {
const relativePath = relative(sourceDir, scssFile);
const parsedPath = parse(relativePath);
const targetDir = join(distDir, dirname(relativePath));
if (!existsSync(targetDir)) {
mkdirSync(targetDir, { recursive: true });
}
copyFileSync(scssFile, join(targetDir, parsedPath.base));
if (relativePath.includes('sass-utilities/') ||
relativePath.includes('assets/') ||
parsedPath.name.startsWith('_')) {
return;
}
try {
const result = sass.compile(scssFile, {
importers: customImporter,
loadPaths: [
sourceDir,
join(process.cwd(), 'src'),
join(process.cwd(), 'src/styles'),
join(process.cwd(), 'src/base'),
join(process.cwd(), 'src/layouts'),
join(process.cwd(), 'src/styles/sass-utilities'),
dirname(scssFile)
],
style: 'compressed'
});
writeFileSync(join(targetDir, `${parsedPath.name}.css`), result.css);
} catch (error) {
console.error(`Error compiling SCSS for ${relativePath}:`, error);
}
}));
}
async function processStructure(sourceDir, outDir) {
// First, ensure ComponentTemplate is available globally
const componentTemplatePath = join(sourceDir, 'lib', 'ComponentTemplate.svelte');
if (!existsSync(componentTemplatePath)) {
console.error('ComponentTemplate.svelte not found in lib directory');
return;
}
// Copy ComponentTemplate to a location where it can be imported globally
const globalComponentsDir = join(outDir, 'lib');
mkdirSync(globalComponentsDir, { recursive: true });
copyFileSync(componentTemplatePath, join(globalComponentsDir, 'ComponentTemplate.svelte'));
const sections = ['components', 'layouts'];
await Promise.all(sections.map(async (section) => {
const sectionPath = join(sourceDir, section);
const outputPath = join(outDir, section);
if (!existsSync(sectionPath)) return;
const entries = await readdir(sectionPath, { withFileTypes: true });
await Promise.all(entries.map(async (entry) => {
if (!entry.isDirectory()) return;
const itemPath = join(sectionPath, entry.name);
const itemOutputPath = join(outputPath, entry.name);
mkdirSync(itemOutputPath, { recursive: true });
const files = await readdir(itemPath);
await Promise.all(files.map(async (file) => {
const filePath = join(itemPath, file);
const fileInfo = parse(file);
if (file.endsWith('.svelte')) {
try {
// Dynamically import the Svelte component
const module = await import(filePath);
const Component = module.default;
// Basic render with no props
const { html } = Component.render();
writeFileSync(join(itemOutputPath, `${fileInfo.name}.html`), html);
} catch (error) {
console.error(`Error processing ${filePath}:`, error);
}
}
}));
}));
}));
}
const adapterCustomStatic = (options = {}) => {
return {
name: 'adapter-custom-static',
async adapt(builder) {
const outDir = options.outDir || 'build';
const sourceDir = options.sourceDir || 'src';
const distDir = options.distDir || 'dist';
console.log(outDir, sourceDir, distDir);
const directories = [
outDir,
distDir,
join(outDir, 'assets'),
join(outDir, 'components'),
join(outDir, 'layouts')
];
await cleanupPreviousBuild(directories);
await Promise.all(directories.map(async (dir) => {
mkdirSync(dir, { recursive: true });
}));
await processScssFiles(sourceDir, outDir, distDir);
await processStructure(sourceDir, outDir);
console.log('Build completed:');
console.log(`- Distribution files: ${distDir}`);
console.log(`- Public files: ${outDir}`);
}
};
};
export { adapterCustomStatic as default };