{"id":16226827,"url":"https://github.com/srmullen/svelte-subcomponent-preprocessor","last_synced_at":"2025-03-19T13:30:55.425Z","repository":{"id":49500095,"uuid":"418150324","full_name":"srmullen/svelte-subcomponent-preprocessor","owner":"srmullen","description":"A Svelte preprocessor for writing more than one component per file.","archived":false,"fork":false,"pushed_at":"2021-11-02T21:10:48.000Z","size":109,"stargazers_count":44,"open_issues_count":1,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-02-28T18:47:41.345Z","etag":null,"topics":["component-architecture","javascript","preprocessor","svelte","sveltejs","sveltekit"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/srmullen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-17T14:03:25.000Z","updated_at":"2024-03-08T23:58:10.000Z","dependencies_parsed_at":"2022-09-07T13:40:12.008Z","dependency_job_id":null,"html_url":"https://github.com/srmullen/svelte-subcomponent-preprocessor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srmullen%2Fsvelte-subcomponent-preprocessor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srmullen%2Fsvelte-subcomponent-preprocessor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srmullen%2Fsvelte-subcomponent-preprocessor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srmullen%2Fsvelte-subcomponent-preprocessor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srmullen","download_url":"https://codeload.github.com/srmullen/svelte-subcomponent-preprocessor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243989729,"owners_count":20379648,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["component-architecture","javascript","preprocessor","svelte","sveltejs","sveltekit"],"created_at":"2024-10-10T12:50:35.396Z","updated_at":"2025-03-19T13:30:55.156Z","avatar_url":"https://github.com/srmullen.png","language":"JavaScript","readme":"svelte-subcomponent-preprocessor\n================================\n\n[![npm package](https://img.shields.io/npm/v/svelte-subcomponent-preprocessor)](https://www.npmjs.com/package/svelte-subcomponent-preprocessor)\n\nThe `svelte-subcomponent-preprocessor` allows you to write more than one component within a svelte file. The subcomponents are written in a `{#component}` block and remain local to the svelte file. Here's an example\n\n```html\n\u003c!-- List.svelte --\u003e\n\u003cscript\u003e\n  export let items = ['svelte', 'subcomponent', 'preprocessor'];\n\u003c/script\u003e\n\n\u003cul\u003e\n  {#each items as item}\n    \u003cItem {item} /\u003e\n  {/each}\n\u003c/ul\u003e\n\n{#component Item}\n  \u003cscript\u003e\n    import { onMount } from 'svelte';\n    export let item;\n\n    onMount(() =\u003e {\n      console.log(item);\n    });\n  \u003c/script\u003e\n\n  \u003cli\u003e{item}!!!\u003c/li\u003e\n\n  \u003cstyle\u003e\n    li {\n      color: red;\n    }\n  \u003c/style\u003e\n{/component}\n```\n\nInstallation and Configuration\n------------------------------\n\n`npm install --save-dev svelte-subcomponent-preprocessor`\n\nIn your svelte config import the preprocessor and add it to the preprocess array.\n\n```js\nimport subcomponentPreprocessor from 'svelte-subcomponent-preprocessor';\nimport sveltePreprocess from 'svelte-preprocess';\n\nsvelte({\n  preprocess: [\n    subcomponentPreprocessor(),\n    sveltePreprocess() // must come after subcomponentPreprocessor\n  ]\n})\n```\n\nIf you're using [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess) it must run after `svelte-subcomponent-preprocessor`.\n\n`svelte-subcomponent-preprocessor` works by extracting the `{#component}` blocks from your svelte code and writing them to disk. By default they are written to `./node_modules/.svelte-subcomponent-preprocessor/`. This can be changed with a configuration object passed to the preprocessor.\n\n`subcomponentPreprocessor({ out: './subcomponents' });`\n\n### Config with Vite or SvelteKit\n\nIf you're using [Vite](https://github.com/vitejs/vite) or [SvelteKit](https://github.com/sveltejs/kit) you'll need a bit of extra configuration to get the subcomponents to work with the dev server. You need the following in your vite config.\n\n```js\n{\n  // ...\n  server: {\n    watch: {\n      ignored: ['!**/node_modules/.svelte-subcomponent-preprocessor/**']\n    }\n  },\n  optimizeDeps: {\n    exclude: ['.svelte-subcomponent-preprocessor']\n  },\n  // ...\n}\n```\n\nFull vite configuration.\n\n```js\nimport { defineConfig } from \"vite\";\nimport { svelte } from \"@sveltejs/vite-plugin-svelte\";\n\n// https://vitejs.dev/config/\nexport default defineConfig({\n  plugins: [svelte()],\n  server: {\n    watch: {\n      ignored: ['!**/node_modules/.svelte-subcomponent-preprocessor/**']\n    }\n  },\n  optimizeDeps: {\n    exclude: ['.svelte-subcomponent-preprocessor']\n  }\n});\n```\n\nOr in SvelteKit the `server` and `optimizeDeps` config would go inside the [`svelte.config.js` `vite` object](https://kit.svelte.dev/docs#configuration-vite).\n\n### Config with Snowpack\n\nWith [snowpack](https://github.com/snowpackjs/snowpack) the default out configuration will not work. Change the out location to someplace that will be watched by the snowpack dev server.\n\nUsage\n-----\n\nTo define a subcomponent put your component code inside a `#component` block. Pass the name of the subcomponent to the block, like so...\n\n```html\n{#component ComponentName}    \n  \u003ch1\u003eMy Component\u003c/h1\u003e\n{/component}\n\n\u003cdiv\u003e\n  \u003cComponentName /\u003e\n\u003cdiv\u003e\n```\n\nLimitations\n-----------\n\n- Does not currently work with [snowpack](https://github.com/snowpackjs/snowpack). It may be possible to change the snowpack config to get it to work, but I'm not sure how.\n- Subcomponents cannot have interdependencies. i.e. Only the default export component can use the subcomponents.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrmullen%2Fsvelte-subcomponent-preprocessor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrmullen%2Fsvelte-subcomponent-preprocessor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrmullen%2Fsvelte-subcomponent-preprocessor/lists"}