• Stars
    star
    230
  • Rank 174,053 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically detect and import components/modules for SvelteKit projects

sveltekit-autoimport

Build Status

Automatically detect and import components/modules for SvelteKit projects.

Before

Code without sveltekit-autoimport

After

Code with sveltekit-autoimport

Installation

npm i -D sveltekit-autoimport

Basic configuration

Inside vite.config.js.

import { sveltekit } from '@sveltejs/kit/vite';
import autoImport from 'sveltekit-autoimport';

export default {
  plugins: [
    autoImport({
      components: ['./src/components'],
    }),
    // must be placed before sveltekit()
    sveltekit()
  ]
}

How it works?

This tool will NOT add global components blindly into your files. Instead, it searches for undefined components or modules, and then try to fix them by auto importing.

You need to guide it where to import the components from:

autoImport({
  components: ['./src/components']
})

Or tell it how to import for some specific variables:

autoImport({
  mapping: {
    API: `import API from '~/api/api.js'`,
    MY_FUNCTION: `import MY_FUNCTION from 'lib/my-function'`
  }
})

Or explictly list the components being used from a third party module:

autoImport({
  module: {
    'carbon-components-svelte': [
      'Button',
      'Accordion',
      'AccordionItem',
      'Grid as CarbonGrid', /* rename */
    ]
  }
})

Name strategy

By default the component names will be namespaced with their directory names and then normalized to upper camel case format. For example:

<MyComponent />
<!-- my-component.svelte -->

<MyAnotherComponent />
<!-- my_another_component.svelte -->

<FormInput />
<!-- form/input.svelte -->

<Modal />
<!-- modal/index.svelte -->

Prefix

Components can be prefixed with a given name.

autoImport({
  components: [{ name: './src/components', prefix: 'shared' } ],
})

So that

<SharedComponent />
<!-- component.svelte -->

<SharedFormInput />
<!-- form/input.svelte -->

Flat

If the flat option is set to be true, no namespace will be added.

autoImport({
  components: [{ name: './src/components', flat: true } ],
})

So that

<Input />
<!-- form/input.svelte -->

<Popup />
<!-- modal/inline/popup.svelte -->

Full options

// vite.config.js

import { sveltekit } from '@sveltejs/kit/vite';
import autoImport from 'sveltekit-autoimport';

export default {
  plugins: [
    autoImport({

      // where to search for the components
      components: [
        './src/components',
        './src/routes/_fragments',
        { name: './src/lib', flat: true, prefix: 'lib' },
      ],

      // some frequently used modules
      module: {
        svelte: ['onMount', 'createEventDispatcher']
      },

      // manually import
      mapping: {
        API:  `import API from '~/src/api'`,
        Icon: `import * as Icon from '$components/icon'`,
      },

      // autoimport only for .svelte files
      // and only search for .svelte files inside components
      include: ['**/*.svelte'],

      // node_modules is ignored by default
      exclude: ['**/node_modules/**'],

    }),

    sveltekit()
  ]
}

Example

https://stackblitz.com/edit/vitejs-vite-tfpzge?file=src%2Froutes%2F%2Bpage.svelte

More Repositories

1

node-watch

A wrapper and enhancements for fs.watch
JavaScript
340
star
2

game-of-life

A browser extension to play Conway's Game of Life on GitHub contribution board
JavaScript
209
star
3

talk

HTML
95
star
4

codepen-prefill

Create new pen from local HTML/JS/CSS files with ease
JavaScript
75
star
5

gradient-shapes

Shapes generated with CSS background gradients.
HTML
75
star
6

markdown-preview

Markdown preview made easy (with live update)
CSS
70
star
7

CSS-at

A collection of styleguides or approach to CSS at different companies
55
star
8

find

Find files or directories by name
JavaScript
47
star
9

css-shapes-patterns

CSS Shapes Patterns
HTML
37
star
10

match

Erlang-like pattern matching in JavaScript.
JavaScript
33
star
11

ikko-tanaka

Ikko Tanaka's graphic design in CSS
CSS
28
star
12

css-fractal

A proof of concept to show the elegance of css
HTML
27
star
13

svelte-pen

Create Svelte component demo quickly
JavaScript
11
star
14

style

Create stylesheets dynamically in javascript
JavaScript
10
star
15

intro-to-css-doodle

Presentation for the Groove Meetup
HTML
6
star
16

game-controller

Your mobile phone as a game controller
JavaScript
5
star
17

mock

Mock server for personal usage
JavaScript
5
star
18

jd

cd to path quickly
Go
5
star
19

FizzBuzzWhizz

FizzBuzzWhizz in css
JavaScript
5
star
20

redis-commands

Redis commands in javascript
JavaScript
4
star
21

html-tag-printer

A simple & small tool easily output HTML tags in php
PHP
4
star
22

brainfuck

A simple brainfuck interpreter
JavaScript
4
star
23

fractal-div

Fractals made out of divs
Haskell
4
star
24

tiny-template

A tiny template engine without using `eval` or `new Function`
JavaScript
3
star
25

cond

Lisp-like conditional statement for JSX
JavaScript
3
star
26

praxis

Practice makes perfect
JavaScript
3
star
27

whirling-rain

A Windows screensaver
C++
3
star
28

sveltekit-issues

JavaScript
2
star
29

browser-bugs

Browser bugs I found while experimenting
2
star
30

wallpapers

Picked wallpapers
2
star
31

postcss-polygon-shapes

Generate shapes using polygon()
JavaScript
2
star
32

wildcard

Solutions to wildcard programming challenge
JavaScript
1
star
33

seq

Generate strings from a sequence.
JavaScript
1
star
34

emmet-vim-snippets

emmet-vim snippets for personal usage
CSS
1
star