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

Add .ts files in create-svelte/shared to both +typescript and -typescript (as .js files) #4368

Merged
merged 3 commits into from
Mar 17, 2022
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
5 changes: 5 additions & 0 deletions .changeset/perfect-mayflies-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

[fix] use .ts extension for tests when using TypeScript
88 changes: 54 additions & 34 deletions packages/create-svelte/scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} shared */
async function generate_templates(shared) {
const templates = fs.readdirSync('templates');
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -152,36 +155,53 @@ async function generate_shared() {
/** @type {Set<string>} */
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'));

Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/shared/+playwright/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"devDependencies": {
"@playwright/test": "^1.19.1"
"@playwright/test": "^1.20.0"
},
"scripts": {
"test": "playwright test"
Expand Down