From dafc2bfab8909ca586dbc41dbdabb18682afdf06 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:37:17 -0400 Subject: [PATCH 1/6] poc: build templates with export --- package.json | 1 + packages/create/package.json | 3 ++ packages/create/scripts/build-templates.js | 33 ++++++++++++---------- pnpm-lock.yaml | 3 ++ rollup.config.js | 13 ++++++--- tsconfig.json | 7 ++++- 6 files changed, 40 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index baede1a4..6f5fd1f4 100644 --- a/package.json +++ b/package.json @@ -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..11254045 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": { diff --git a/packages/create/scripts/build-templates.js b/packages/create/scripts/build-templates.js index 4dd3f86b..ec797b14 100644 --- a/packages/create/scripts/build-templates.js +++ b/packages/create/scripts/build-templates.js @@ -44,11 +44,12 @@ function strip_jsdoc(content) { } /** - * @param {Set} shared * @param {string} dist + * @param {Set} shared + * @param {string} _cwd */ -async function generate_templates(shared, dist) { - const templates = fs.readdirSync('templates'); +async function generate_templates(dist, shared, _cwd) { + const templates = fs.readdirSync(path.resolve(_cwd, 'templates')); for (const template of templates) { if (template[0] === '.') continue; @@ -57,7 +58,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(_cwd, 'templates', template); const gitignore_file = path.join(cwd, '.gitignore'); if (!fs.existsSync(gitignore_file)) { @@ -225,9 +226,12 @@ async function replace_async(string, regexp, replacer) { return string.replace(regexp, () => replacements[i++]); } -/** @param {string} dist */ -async function generate_shared(dist) { - const cwd = path.resolve('shared'); +/** + * @param {string} dist + * @param {string} _cwd + */ +async function generate_shared(dist, _cwd) { + const cwd = path.resolve(_cwd, 'shared'); /** @type {Set} */ const shared = new Set(); @@ -316,14 +320,13 @@ export function mkdirp(dir) { } } -/** @param {string} dist */ -async function main(dist) { +/** + * @param {string} dist + * @param {string} cwd + */ +export async function buildTemplates(dist, cwd = process.cwd()) { mkdirp(dist); - const shared = await generate_shared(dist); - await generate_templates(shared, dist); + const shared = await generate_shared(dist, cwd); + await generate_templates(dist, shared, cwd); } - -main('dist'); -// also generates the templates in the package where `@svelte-cli/create` will be bundled -main(path.resolve('..', 'core', '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..ed96f387 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,15 @@ function getConfig(project) { // thus also removes the template files buildCliTemplatesPlugin = { name: 'build-cli-templates', - writeBundle() { + async buildEnd() { 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'), + path.resolve('packages', 'create') + ); + 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", From 8b0c20a61f3ed3fdfa3e8544ccc9e37bc26037bd Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:45:03 -0400 Subject: [PATCH 2/6] simplify --- packages/create/scripts/build-templates.js | 22 +++++++++++----------- rollup.config.js | 5 +---- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/create/scripts/build-templates.js b/packages/create/scripts/build-templates.js index ec797b14..e15df3cf 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) { @@ -46,10 +49,9 @@ function strip_jsdoc(content) { /** * @param {string} dist * @param {Set} shared - * @param {string} _cwd */ -async function generate_templates(dist, shared, _cwd) { - const templates = fs.readdirSync(path.resolve(_cwd, 'templates')); +async function generate_templates(dist, shared) { + const templates = fs.readdirSync(path.resolve(pkgRoot, 'templates')); for (const template of templates) { if (template[0] === '.') continue; @@ -58,7 +60,7 @@ async function generate_templates(dist, shared, _cwd) { const assets = path.join(dir, 'assets'); mkdirp(assets); - const cwd = path.resolve(_cwd, 'templates', template); + const cwd = path.resolve(pkgRoot, 'templates', template); const gitignore_file = path.join(cwd, '.gitignore'); if (!fs.existsSync(gitignore_file)) { @@ -228,10 +230,9 @@ async function replace_async(string, regexp, replacer) { /** * @param {string} dist - * @param {string} _cwd */ -async function generate_shared(dist, _cwd) { - const cwd = path.resolve(_cwd, 'shared'); +async function generate_shared(dist) { + const cwd = path.resolve(pkgRoot, 'shared'); /** @type {Set} */ const shared = new Set(); @@ -322,11 +323,10 @@ export function mkdirp(dir) { /** * @param {string} dist - * @param {string} cwd */ -export async function buildTemplates(dist, cwd = process.cwd()) { +export async function buildTemplates(dist) { mkdirp(dist); - const shared = await generate_shared(dist, cwd); - await generate_templates(dist, shared, cwd); + const shared = await generate_shared(dist); + await generate_templates(dist, shared); } diff --git a/rollup.config.js b/rollup.config.js index ed96f387..02ae3617 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -45,10 +45,7 @@ function getConfig(project) { async buildEnd() { console.log('building templates'); const start = performance.now(); - await buildTemplates( - path.resolve('packages', 'core', 'dist'), - path.resolve('packages', 'create') - ); + await buildTemplates(path.resolve('packages', 'core', 'dist')); const end = performance.now(); console.log(`finished building templates: ${Math.round(end - start)}ms`); } From e56eeb6c90defdb4fffb9a1ae10e3d1952b3c7d0 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:09:28 -0400 Subject: [PATCH 3/6] add to `create/dist` for tests --- rollup.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/rollup.config.js b/rollup.config.js index 02ae3617..95caadb1 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -45,6 +45,7 @@ function getConfig(project) { async buildEnd() { console.log('building templates'); const start = performance.now(); + await buildTemplates(path.resolve('packages', 'create', 'dist')); await buildTemplates(path.resolve('packages', 'core', 'dist')); const end = performance.now(); console.log(`finished building templates: ${Math.round(end - start)}ms`); From f7dd80be637bdeb927b913e7ba7879334a6c30c0 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:18:51 -0400 Subject: [PATCH 4/6] use `writeBundle` instead --- rollup.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index 95caadb1..6d724bd9 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -42,7 +42,7 @@ function getConfig(project) { // thus also removes the template files buildCliTemplatesPlugin = { name: 'build-cli-templates', - async buildEnd() { + async writeBundle() { console.log('building templates'); const start = performance.now(); await buildTemplates(path.resolve('packages', 'create', 'dist')); From 5c84ef06d420eed93e173b7aad5287517d82b00e Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:11:23 -0400 Subject: [PATCH 5/6] simplify so rollup builds only add templates to `core` --- package.json | 2 +- packages/create/package.json | 4 +--- packages/create/scripts/build-templates.js | 5 +++++ rollup.config.js | 1 - 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 6f5fd1f4..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": { diff --git a/packages/create/package.json b/packages/create/package.json index 11254045..5c70a4af 100644 --- a/packages/create/package.json +++ b/packages/create/package.json @@ -39,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 e15df3cf..5d4a0d56 100644 --- a/packages/create/scripts/build-templates.js +++ b/packages/create/scripts/build-templates.js @@ -330,3 +330,8 @@ export async function buildTemplates(dist) { const shared = await generate_shared(dist); await generate_templates(dist, shared); } + +const dist = process.argv[2]; +if (dist !== undefined) { + buildTemplates(dist); +} diff --git a/rollup.config.js b/rollup.config.js index 6d724bd9..545fe1e6 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -45,7 +45,6 @@ function getConfig(project) { async writeBundle() { console.log('building templates'); const start = performance.now(); - await buildTemplates(path.resolve('packages', 'create', 'dist')); await buildTemplates(path.resolve('packages', 'core', 'dist')); const end = performance.now(); console.log(`finished building templates: ${Math.round(end - start)}ms`); From 6ef6edeef4244f9dff872f6eaf02fa5c9142829f Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:16:29 -0400 Subject: [PATCH 6/6] oops --- packages/create/scripts/build-templates.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create/scripts/build-templates.js b/packages/create/scripts/build-templates.js index 5d4a0d56..52d8c71a 100644 --- a/packages/create/scripts/build-templates.js +++ b/packages/create/scripts/build-templates.js @@ -332,6 +332,6 @@ export async function buildTemplates(dist) { } const dist = process.argv[2]; -if (dist !== undefined) { +if (dist === 'dist') { buildTemplates(dist); }