Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: build create templates directly #12

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"changeset:version": "changeset version && pnpm -r generate:version && git add --all",
"changeset:release": "changeset publish",
"dev": "rollup --config --watch",
"build": "pnpm -r build && rollup -c",
"build": "rollup -c",
"test": "pnpm -r test"
},
"devDependencies": {
Expand All @@ -21,6 +21,7 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@sveltejs/eslint-config": "^8.0.1",
"@svelte-cli/create": "workspace:*",
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
"@types/node": "^22.3.0",
"eslint": "^9.6.0",
Expand Down
7 changes: 4 additions & 3 deletions packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./build": {
"default": "./scripts/build-templates.js"
}
},
"devDependencies": {
Expand All @@ -36,11 +39,9 @@
"vitest": "^2.0.1"
},
"scripts": {
"build": "pnpm build:templates",
"build:templates": "node scripts/build-templates",
"lint": "prettier --check . --config ../../.prettierrc --ignore-path ../../.gitignore --ignore-path .gitignore",
"format": "pnpm lint --write",
"test": "pnpm build && vitest run",
"test": "node scripts/build-templates dist && vitest run",
"check": "tsc",
"update-template-repo": "echo \"Updating template repo\" && bash ./scripts/update-template-repo.sh"
},
Expand Down
32 changes: 20 additions & 12 deletions packages/create/scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import parser from 'gitignore-parser';
import prettier from 'prettier';
import { transform } from 'sucrase';
import glob from 'tiny-glob/sync.js';
import { fileURLToPath } from 'node:url';

const pkgRoot = path.resolve(fileURLToPath(import.meta.url), '..', '..');

/** @param {string} content */
async function convert_typescript(content) {
Expand Down Expand Up @@ -44,11 +47,11 @@ function strip_jsdoc(content) {
}

/**
* @param {Set<string>} shared
* @param {string} dist
* @param {Set<string>} shared
*/
async function generate_templates(shared, dist) {
const templates = fs.readdirSync('templates');
async function generate_templates(dist, shared) {
const templates = fs.readdirSync(path.resolve(pkgRoot, 'templates'));

for (const template of templates) {
if (template[0] === '.') continue;
Expand All @@ -57,7 +60,7 @@ async function generate_templates(shared, dist) {
const assets = path.join(dir, 'assets');
mkdirp(assets);

const cwd = path.resolve('templates', template);
const cwd = path.resolve(pkgRoot, 'templates', template);

const gitignore_file = path.join(cwd, '.gitignore');
if (!fs.existsSync(gitignore_file)) {
Expand Down Expand Up @@ -225,9 +228,11 @@ async function replace_async(string, regexp, replacer) {
return string.replace(regexp, () => replacements[i++]);
}

/** @param {string} dist */
/**
* @param {string} dist
*/
async function generate_shared(dist) {
const cwd = path.resolve('shared');
const cwd = path.resolve(pkgRoot, 'shared');

/** @type {Set<string>} */
const shared = new Set();
Expand Down Expand Up @@ -316,14 +321,17 @@ export function mkdirp(dir) {
}
}

/** @param {string} dist */
async function main(dist) {
/**
* @param {string} dist
*/
export async function buildTemplates(dist) {
mkdirp(dist);

const shared = await generate_shared(dist);
await generate_templates(shared, dist);
await generate_templates(dist, shared);
}

main('dist');
// also generates the templates in the package where `@svelte-cli/create` will be bundled
main(path.resolve('..', 'core', 'dist'));
const dist = process.argv[2];
if (dist === 'dist') {
buildTemplates(dist);
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import { preserveShebangs } from 'rollup-plugin-preserve-shebangs';
import dts from 'unplugin-isolated-decl/rollup';
import esbuild from 'rollup-plugin-esbuild';
import { execSync } from 'node:child_process';
import { buildTemplates } from '@svelte-cli/create/build';

/** @import { Package } from "./packages/core/utils/common.js" */
/** @import { Plugin, RollupOptions } from "rollup" */
Expand Down Expand Up @@ -42,10 +42,12 @@ function getConfig(project) {
// thus also removes the template files
buildCliTemplatesPlugin = {
name: 'build-cli-templates',
writeBundle() {
async writeBundle() {
console.log('building templates');
execSync('node scripts/build-templates.js', { cwd: path.resolve('packages', 'create') });
console.log('finished building templates');
const start = performance.now();
await buildTemplates(path.resolve('packages', 'core', 'dist'));
const end = performance.now();
console.log(`finished building templates: ${Math.round(end - start)}ms`);
}
};
}
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"exclude": ["**/dist/**", "packages/cli/templates/**", "packages/cli/shared/**", "**/temp/**"],
"exclude": [
"**/dist/**",
"packages/create/templates/**",
"packages/create/shared/**",
"**/temp/**"
],
"compilerOptions": {
"checkJs": true,
"moduleResolution": "Bundler",
Expand Down