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 .d.ts files for improved TypeScript support #33

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ coverage

# In case contributors use yarn (just prevent it from being committed). Standardizing on npm for broad compatibility.
yarn.lock

# Ignore autogenerated .d.ts (and associated .map) files since this are automatically generated prior to publish to NPM.
patricknelson marked this conversation as resolved.
Show resolved Hide resolved
*.d.ts*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Changes since forking from [`svelte-tag`](https://github.com/crisward/svelte-tag
- Add step-by-step instructions and provided a simple MVP
example (https://github.com/patricknelson/svelte-retag/pull/24)
- Automatically forward all attributes to component (i.e. `attributes: true`) (https://github.com/patricknelson/svelte-retag/issues/34)
- Add better TypeScript support (https://github.com/patricknelson/svelte-retag/pull/33)

### v2

Expand Down
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,29 @@ function renderElements(timestamp) {
}


/**
* @typedef {new(...args: any[]) => any} Newable Type alias for a really generic class constructor
* @typedef {Newable} SvelteComponent Svelte component class constructor (basically a "newable" object)
patricknelson marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* Please see README.md for usage information.
*
* @param {object} opts Custom element options
*
* @param {any} opts.component Svelte component instance to incorporate into your custom element.
* @param {string} opts.tagname Name of the custom element tag you'd like to define.
* @param {string[]|boolean|undefined} opts.attributes Optional array of attributes that should be reactively forwarded to the component when modified. Set to true to automatically watch all attributes.
* @param {boolean?} opts.shadow Indicates if we should build the component in the shadow root instead of in the regular ("light") DOM.
* @param {string?} opts.href URL to the CSS stylesheet to incorporate into the shadow DOM (if enabled).
* @param {SvelteComponent} opts.component The Svelte component *class* constructor to incorporate into your custom element (this is the imported component class, *not* an instance)
* @param {string} opts.tagname Name of the custom element tag you'd like to define.
* @param {string[]|boolean} [opts.attributes=[]] Optional array of attributes that should be reactively forwarded to the component when modified. Set to true to automatically watch all attributes.
* @param {boolean} [opts.shadow=false] Indicates if we should build the component in the shadow root instead of in the regular ("light") DOM.
* @param {string} [opts.href=""] URL to the CSS stylesheet to incorporate into the shadow DOM (if enabled).
*
* Experimental:
* @param {boolean?} opts.hydratable Light DOM slot hydration (specific to svelte-retag): Enables pre-rendering of the
* web component (e.g. SSR) by adding extra markers (attributes & wrappers) during
* rendering to enable svelte-retag to find and restore light DOM slots when
* restoring interactivity.
* @param {boolean} [opts.hydratable=false] Light DOM slot hydration (specific to svelte-retag): Enables pre-rendering of the
* web component (e.g. SSR) by adding extra markers (attributes & wrappers) during
* rendering to enable svelte-retag to find and restore light DOM slots when
* restoring interactivity.
*
* @param {boolean|string?} opts.debugMode Hidden option to enable debugging for package development purposes.
* @param {boolean|string} [opts.debugMode=false] Hidden option to enable debugging for package development purposes.
*/
export default function svelteRetag(opts) {
/**
Expand Down
24 changes: 22 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"version": "1.4.0",
"description": "Light DOM custom element wrapper for Svelte 3 and 4 with slots, context and Vite HMR support",
"main": "index.js",
"types": "./types/index.d.ts",
"scripts": {
"test": "npm run eslint && vitest run",
"vitest": "vitest run",
"eslint": "eslint index.js tests/*",
"pub": "npm run test && npm run eslint && npm publish"
"build": "tsc --emitDeclarationOnly",
"pub": "npm run test && npm run eslint && npm run build && npm publish"
},
"repository": {
"type": "git",
Expand All @@ -23,6 +25,7 @@
"eslint-plugin-svelte": "^2.35.1",
"jsdom": "^22.1.0",
"svelte": "^4.2.8",
"typescript": "^5.3.3",
"vite": "^4.5.1",
"vitest": "^0.25.8"
},
Expand Down
24 changes: 24 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Only needed for JS files in the project root (exclude vite config).
"include": ["*.js"],
"exclude": ["vite.config.js"],

// See: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html
"compilerOptions": {
// Tells TypeScript to read JS files, as
// normally they are ignored as source files
"allowJs": true,
// Generate d.ts files
"declaration": true,
// This compiler run should
// only output d.ts files
"emitDeclarationOnly": true,
// Types should go into this directory.
// Removing this would place the .d.ts files
// next to the .js files
"outDir": "types",
// go to js file when using IDE functions like
// "Go to Definition" in VSCode
"declarationMap": true
}
}