From 4e53c51ceb8bdb43693894ee1c4ea7b2bdb847ac Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 17 Mar 2022 13:03:51 -0400 Subject: [PATCH] Add `.ts` files in `create-svelte/shared` to both `+typescript` and `-typescript` (as `.js` files) (#4368) * [fix] use .ts extension for tests when using TypeScript * convert .ts files automatically * oops Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- .changeset/perfect-mayflies-sneeze.md | 5 ++ .../create-svelte/scripts/build-templates.js | 88 ++++++++++++------- .../tests/{test.js => test.ts} | 0 .../tests/{test.js => test.ts} | 0 .../shared/+playwright/package.json | 2 +- 5 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 .changeset/perfect-mayflies-sneeze.md rename packages/create-svelte/shared/+playwright+default/tests/{test.js => test.ts} (100%) rename packages/create-svelte/shared/+playwright+skeleton/tests/{test.js => test.ts} (100%) diff --git a/.changeset/perfect-mayflies-sneeze.md b/.changeset/perfect-mayflies-sneeze.md new file mode 100644 index 000000000000..b99b3b9c4c68 --- /dev/null +++ b/.changeset/perfect-mayflies-sneeze.md @@ -0,0 +1,5 @@ +--- +'create-svelte': patch +--- + +[fix] use .ts extension for tests when using TypeScript diff --git a/packages/create-svelte/scripts/build-templates.js b/packages/create-svelte/scripts/build-templates.js index f05e4a93e6fa..3367ede9a46d 100644 --- a/packages/create-svelte/scripts/build-templates.js +++ b/packages/create-svelte/scripts/build-templates.js @@ -6,6 +6,21 @@ import { transform } from 'sucrase'; import glob from 'tiny-glob/sync.js'; import { mkdirp, rimraf } from '../utils.js'; +/** @param {string} typescript */ +function convert_typescript(typescript) { + const transformed = transform(typescript, { + transforms: ['typescript'] + }); + + return prettier.format(transformed.code, { + parser: 'babel', + useTabs: true, + singleQuote: true, + trailingComma: 'none', + printWidth: 100 + }); +} + /** @param {Set} shared */ async function generate_templates(shared) { const templates = fs.readdirSync('templates'); @@ -71,21 +86,9 @@ async function generate_templates(shared) { if (file.name.endsWith('.d.ts')) { if (file.name.endsWith('app.d.ts')) js.push(file); } else if (file.name.endsWith('.ts')) { - const transformed = transform(file.contents, { - transforms: ['typescript'] - }); - - const contents = prettier.format(transformed.code, { - parser: 'babel', - useTabs: true, - singleQuote: true, - trailingComma: 'none', - printWidth: 100 - }); - js.push({ name: file.name.replace(/\.ts$/, '.js'), - contents + contents: convert_typescript(file.contents) }); } else if (file.name.endsWith('.svelte')) { // we jump through some hoops, rather than just using svelte.preprocess, @@ -152,36 +155,53 @@ async function generate_shared() { /** @type {Set} */ const shared = new Set(); - const files = glob('**/*', { cwd, filesOnly: true, dot: true }) - .map((file) => { - const contents = fs.readFileSync(path.join(cwd, file), 'utf8'); + /** @type {Array<{ name: string, include: string[], exclude: string[], contents: string }>} */ + const files = []; - /** @type {string[]} */ - const include = []; + glob('**/*', { cwd, filesOnly: true, dot: true }).forEach((file) => { + const contents = fs.readFileSync(path.join(cwd, file), 'utf8'); - /** @type {string[]} */ - const exclude = []; + /** @type {string[]} */ + const include = []; - let name = file; + /** @type {string[]} */ + const exclude = []; - if (file.startsWith('+') || file.startsWith('-')) { - const [conditions, ...rest] = file.split(path.sep); + let name = file; - const pattern = /([+-])([a-z]+)/g; - let match; - while ((match = pattern.exec(conditions))) { - const set = match[1] === '+' ? include : exclude; - set.push(match[2]); - } + if (file.startsWith('+') || file.startsWith('-')) { + const [conditions, ...rest] = file.split(path.sep); - name = rest.join('/'); + const pattern = /([+-])([a-z]+)/g; + let match; + while ((match = pattern.exec(conditions))) { + const set = match[1] === '+' ? include : exclude; + set.push(match[2]); } - shared.add(name); + name = rest.join('/'); + } + + if (name.endsWith('.ts') && !include.includes('typescript')) { + const js_name = name.replace(/\.ts$/, '.js'); + shared.add(js_name); + + files.push({ + name: js_name, + include: [...include], + exclude: [...exclude, 'typescript'], + contents: convert_typescript(contents) + }); + + include.push('typescript'); + } + + shared.add(name); + + files.push({ name, include, exclude, contents }); + }); - return { name, include, exclude, contents }; - }) - .sort((a, b) => a.include.length + a.exclude.length - (b.include.length + b.exclude.length)); + files.sort((a, b) => a.include.length + a.exclude.length - (b.include.length + b.exclude.length)); fs.writeFileSync('dist/shared.json', JSON.stringify({ files }, null, '\t')); diff --git a/packages/create-svelte/shared/+playwright+default/tests/test.js b/packages/create-svelte/shared/+playwright+default/tests/test.ts similarity index 100% rename from packages/create-svelte/shared/+playwright+default/tests/test.js rename to packages/create-svelte/shared/+playwright+default/tests/test.ts diff --git a/packages/create-svelte/shared/+playwright+skeleton/tests/test.js b/packages/create-svelte/shared/+playwright+skeleton/tests/test.ts similarity index 100% rename from packages/create-svelte/shared/+playwright+skeleton/tests/test.js rename to packages/create-svelte/shared/+playwright+skeleton/tests/test.ts diff --git a/packages/create-svelte/shared/+playwright/package.json b/packages/create-svelte/shared/+playwright/package.json index 0181674d4b01..043ba45cce38 100644 --- a/packages/create-svelte/shared/+playwright/package.json +++ b/packages/create-svelte/shared/+playwright/package.json @@ -1,6 +1,6 @@ { "devDependencies": { - "@playwright/test": "^1.19.1" + "@playwright/test": "^1.20.0" }, "scripts": { "test": "playwright test"