svelte-standalone
Version:
Transform Svelte components in standalone scripts!
44 lines (43 loc) • 1.48 kB
JavaScript
import { checkbox } from '@inquirer/prompts';
import { glob } from 'glob';
import { buildStandalone } from './methods/index.js';
import path from 'path';
const rootDir = process.cwd();
const c = glob
.sync(`${rootDir}/src/_standalone/**/embed.{js,ts}`) // Matches both .js and .ts
.map((file) => {
const normalizedPath = path.normalize(file);
const match = normalizedPath.match(/src[\\/]_standalone[\\/](.*?)[\\/]embed\.(js|ts)/);
return match ? { name: match[1], value: file, checked: true } : null;
})
.filter(Boolean);
export const buildStrategy = {
name: 'components',
message: 'Which components should be builded?',
choices: c
};
export async function build(prod, all, stripRuntime, mode) {
if (buildStrategy.choices.length === 0) {
console.warn("You don't have any standalone component. Create them running: standalone create.");
return;
}
const hasRuntime = stripRuntime
? false
: c.some(({ name }) => /(\$runtime|\+runtime|runtime)/.test(name ?? ''));
if (all) {
buildStandalone(c.map((co) => co.value), prod, hasRuntime, mode);
return;
}
const answers = await checkbox(buildStrategy);
try {
buildStandalone(answers, prod, hasRuntime, mode);
}
catch (error) {
if (error instanceof Error && error.name === 'ExitPromptError') {
// noop; silence this error
}
else {
throw error;
}
}
}