8000 chore: add basic svelte-add stuff by manuel3108 · Pull Request #2 · sveltejs/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: add basic svelte-add stuff #2

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

Merged
merged 18 commits into from
Aug 20, 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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ jobs:
node-version: 18
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm run test
- run: pnpm build
- run: pnpm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
node_modules
dist
temp/
package-lock.json
yarn.lock
vite.config.js.timestamp-*
Expand Down
4 changes: 1 addition & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
}
},
{
"files": [
"**/CHANGELOG.md"
],
"files": ["**/CHANGELOG.md"],
"options": {
"requirePragma": true
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Web development, streamlined. Read the [documentation](https://kit.svelte.dev/do

### Packages

| Package | Changelog |
| --------------------------------------------------------------------------- | ------------------------------------------------------------- |
| [sv](packages/cli) | [Changelog](packages/cli/CHANGELOG.md) |
| Package | Changelog |
| ------------------ | -------------------------------------- |
| [sv](packages/cli) | [Changelog](packages/cli/CHANGELOG.md) |

[Additional adders](https://www.sveltesociety.dev/templates?category=svelte-add) are maintained by the community.

Expand Down
10 changes: 8 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error
import svelte_config from '@sveltejs/eslint-config';

/** @type {import('eslint').Linter.Config[]} */
Expand All @@ -17,11 +18,16 @@ export default [
rules: {
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/require-await': 'error',
},
'@typescript-eslint/require-await': 'error'
}
},
{
ignores: [
'packages/cli/shared/**/*',
'packages/cli/templates/**/*',
'temp/**/*',
'.test-tmp/**/*',
'**/dist/*'
]
}
];
19 changes: 16 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@
"private": true,
"type": "module",
"scripts": {
"check": "pnpm -r check",
"lint": "pnpm -r lint && eslint --cache --cache-location node_modules/.eslintcache 'packages/**/*.js'",
"check": "tsc --noEmit",
"lint": "pnpm -r lint && eslint --cache --cache-location node_modules/.eslintcache",
"format": "pnpm -r format",
"changeset:version": "changeset version && pnpm -r generate:version && git add --all",
"changeset:release": "changeset publish",
"build": "pnpm -r build",
"dev": "rollup --config --watch",
"build": "pnpm -r build && rollup -c",
"test": "pnpm -r test"
},
"devDependencies": {
"@changesets/cli": "^2.27.6",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@sveltejs/eslint-config": "^8.0.1",
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
"@types/node": "^22.3.0",
"eslint": "^9.6.0",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.2.6",
"rollup": "^4.20.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-preserve-shebangs": "^0.2.0",
"typescript": "^5.3.3",
"typescript-eslint": "^8.0.0"
},
"packageManager": "pnpm@9.7.0",
Expand Down
63 changes: 63 additions & 0 deletions packages/adders/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { ScriptFileEditorArgs } from '@svelte-cli/core';
import type { Question } from '../core/internal';

export function addEslintConfigPrettier({
ast,
imports,
exports,
common
}: ScriptFileEditorArgs<Record<string, Question>>) {
// if a default import for `eslint-plugin-svelte` already exists, then we'll use their specifier's name instead
const importNodes = ast.body.filter((n) => n.type === 'ImportDeclaration');
const sveltePluginImport = importNodes.find(
(n) =>
n.type === 'ImportDeclaration' &&
n.source.value === 'eslint-plugin-svelte' &&
n.specifiers?.some((n) => n.type === 'ImportDefaultSpecifier')
);

let svelteImportName: string;
for (const specifier of sveltePluginImport?.specifiers ?? []) {
if (specifier.type === 'ImportDefaultSpecifier' && specifier.local?.name) {
svelteImportName = specifier.local.name;
}
}

svelteImportName ??= 'svelte';
imports.addDefault(ast, 'eslint-plugin-svelte', svelteImportName);
imports.addDefault(ast, 'eslint-config-prettier', 'prettier');

const fallbackConfig = common.expressionFromString('[]');
const defaultExport = exports.defaultExport(ast, fallbackConfig);
const array = defaultExport.value;
if (array.type !== 'ArrayExpression') return;

const prettier = common.expressionFromString('prettier');
const sveltePrettierConfig = common.expressionFromString(
`${svelteImportName}.configs['flat/prettier']`
);
const configSpread = common.createSpreadElement(sveltePrettierConfig);

const nodesToInsert = [];
if (!common.hasNode(array, prettier)) nodesToInsert.push(prettier);
if (!common.hasNode(array, configSpread)) nodesToInsert.push(configSpread);

// finds index of `...svelte.configs["..."]`
const idx = array.elements.findIndex(
(el) =>
el?.type === 'SpreadElement' &&
el.argument.type === 'MemberExpression' &&
el.argument.object.type === 'MemberExpression' &&
el.argument.object.property.type === 'Identifier' &&
el.argument.object.property.name === 'configs' &&
el.argument.object.object.type === 'Identifier' &&
el.argument.object.object.name === svelteImportName
);

if (idx !== -1) {
array.elements.splice(idx + 1, 0, ...nodesToInsert);
} else {
// append to the end as a fallback
array.elements.push(...nodesToInsert);
}
}
Loading
0