diff --git a/package.json b/package.json index baede1a4..a78f5d3b 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/packages/create/package.json b/packages/create/package.json index d00d890a..5c70a4af 100644 --- a/packages/create/package.json +++ b/packages/create/package.json @@ -26,6 +26,9 @@ ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" + }, + "./build": { + "default": "./scripts/build-templates.js" } }, "devDependencies": { @@ -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" }, diff --git a/packages/create/scripts/build-templates.js b/packages/create/scripts/build-templates.js index 4dd3f86b..52d8c71a 100644 --- a/packages/create/scripts/build-templates.js +++ b/packages/create/scripts/build-templates.js @@ -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) { @@ -44,11 +47,11 @@ function strip_jsdoc(content) { } /** - * @param {Set} shared * @param {string} dist + * @param {Set} 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; @@ -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)) { @@ -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} */ const shared = new Set(); @@ -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); +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 94045a0a..61e160da 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: '@rollup/plugin-node-resolve': specifier: ^15.2.3 version: 15.2.3(rollup@4.21.0) + '@svelte-cli/create': + specifier: workspace:* + version: link:packages/create '@sveltejs/eslint-config': specifier: ^8.0.1 version: 8.0.1(@stylistic/eslint-plugin-js@2.6.4(eslint@9.9.0))(eslint-config-prettier@9.1.0(eslint@9.9.0))(eslint-plugin-n@17.10.2(eslint@9.9.0))(eslint-plugin-svelte@2.43.0(eslint@9.9.0)(svelte@4.2.18))(eslint@9.9.0)(typescript-eslint@8.2.0(eslint@9.9.0)(typescript@5.5.4))(typescript@5.5.4) diff --git a/rollup.config.js b/rollup.config.js index bc3ac1ce..545fe1e6 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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" */ @@ -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`); } }; } diff --git a/tsconfig.json b/tsconfig.json index d771e1cd..850cc6f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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",