Coder Social home page Coder Social logo

harlan-zw / vue-cli-plugin-import-components Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 4.0 529 KB

πŸ”Œ Automatically import components in your Vue CLI apps.

License: MIT License

JavaScript 5.37% Vue 1.00% TypeScript 93.36% Shell 0.26%
vue-cli components

vue-cli-plugin-import-components's Introduction

vue-cli-plugin-import-components

Automatically import components in your Vue CLI app with tree shaking, supporting Vue 2 and 3.

Why and How?

Checkout my article about why this feature exists, how it works and the issues.

ℹ️ Need something more powerful? Consider using unplugin-vue-components

Features

  • πŸ§™ Vue 2 and 3 support with full tree shaking
  • ⚑ Lazy load your components
  • πŸ”₯ Hot Module Reloading ready
  • πŸ“ Written in Typescript

Setup

Install using Vue CLI. (Vue CLI 4+ is recommended)

vue add import-components

Usage

Add components to your components/ folder.

| components/
---| ComponentFoo.vue
---| ComponentBar.vue

Use them in any .vue as you would normally. Access your components with either PascalCase or kebab-case.

<template>
<div>
  <ComponentFoo />
  <component-bar />
</div>
</template>

Remove imports and components from the script section.

Async Component Loading

You can load your components async by prefixing your component name with Lazy or lazy-, depending on your syntax.

<template>
<div>
  <!-- ComponentFoo will be loaded in async -->
  <LazyComponentFoo />
  <!-- ComponentBar will be loaded sync -->
  <ComponentBar />
</div>
</template>

Configuration

You can change the behaviour of the plugin by modifying the options in ./vue.config.js.

// vue.config.js
module.exports = {
  pluginOptions: {
    components: {
      ...
    }
  }
}

Options

All options are optional.

path - String

The path used for scanning to find components. Note: It should be relative to your project root.

Default: ./src/components

pattern - String

Regex to find the files within the path. Note: If you omit the pattern it will use the configured extensions

Default: **/*.{${extensions.join(',')},}

ignore - String[]

Regex to ignore files within the path.

Default: [ '**/*.stories.js' ],

mapComponent - (component : Component) => Component | false

A function which you can use to filter out paths you don't want to be scanned.

For example, if we wanted to access your automatically components only when they're prefixed them with auto, you could use the below code.

// vue.config.js
module.exports = {
  pluginOptions: {
    components: {
      // prefix all automatically imported components with an auto prefix
      mapComponent (component) {
        component.pascalCase = 'Auto' + upperFirst(component.pascalCase)
        component.kebabName = 'auto-' + component.pascalCase
        return component
      }
    }
  }
}

extensions - String[]

When scanning the path for components, which files should be considered components.

Default: ['.js', '.vue', '.ts']

Limitations

Static Imports Only

Only components that are statically defined within your template will work.

<template>
  <component :is="dynamicComponent"/>
</template>

Using folders as namespaces

It is assumed you are using the Vue conventions for naming your components. The below would not work without manually mapping the components.

| components/
---| Foo.vue
------| Namespace/Foo.vue

It would create a conflict with two components called Foo.vue. You should name your component files with the namespace. i.e NamespaceFoo.vue.

Javascript Components

You may need to refresh your browser when you are updating them. The hot module reloading seems to be a little buggy sometimes.

It's recommended that you stick with .vue SFC components.

License

MIT

vue-cli-plugin-import-components's People

Contributors

amlinux avatar harlan-zw avatar lights0123 avatar verstoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vue-cli-plugin-import-components's Issues

Can this be used in webpack?

I have noticed that for vue 2 you use runtime/installComponents but not for vue 3. Was wondering why. Also is this only for vue cli or can I use it with webpack too?

Thank you!

Not working with script setup

Hello @loonpwn , I use vue-cli-plugin-import-components on Vue.js 3, It works nice.

I try to use script setup, but It is not working.

my script setup code here.

<script setup>
import { ref } from "vue"
export const text = ref('')
</script>

Can you test this with your plugin?

Conflicts with globally defined components

When you have globally defined components, for example in your main.js:

import Vue from 'vue';
import { ValidationObserver } from 'vee-validate';

Vue.component('ValidationObserver', ValidationObserver);

And you use one of these in combination with an auto-loaded component from the plugin in the same component:

<template>
  <ValidationObserver>
    <FiltersForm>
      <!-- ... -->
    </FiltersForm>
  </ValidationObserver>
</template>

Vue warns about custom element ValidationObserver not being available. Removing FiltersForm or disabling the plugin and specifying the components explicitly works.

If I check vm.$options.component of the component above, I only get FiltersForm. It’s like something override globally defined component, or prevent them from being found when parsing the template.

Import error: component is named incorrectly

Hello! First of all, thanks for your work and especially for such a detailed article.

What is the problem?

Import does not work. I installed your plugin on a clean project and on the first run (npm run serve) I immediately got an error:

This dependency was not found:
* @componentsHelloWorld.vue in ./src/App.vue, ./src/views/Home.vue

image

How to reproduce the problem

In a clean project created via @vue/cli, there is a HelloWorld.vue component in the components folder.
If you use it in any other component, the corresponding error will appear.
<HelloWorld msg="Welcome to Your Vue.js App" />

Project settings

Vue 3
@vue/cli 4.5.10

Missing support for vue-property-decorator

If a component is defined with vue-property-decorator, auto-import injection doesn't work.

Repro:

import {Vue, Component} from 'vue-property-decorator'

@Component
export default class PropertyDecoratorComponent extends Vue {
}

It fails in runtime with error messages that component-a, component-b, component-c are not declared.

Root cause: vue-property-decorator stores imported components not in script.components, but rather in script.options.components.

Conflict with vuetify-loader

Thank you for looking at my issue! Now that I’m using version 1.1 (relates to #9), there’s now a conflict with vuetify-loader:

 error  in ./src/App.vue

Module parse failed: Identifier 'installComponents' has already been declared (28:7)
File was processed with these loaders:
 * ./node_modules/vuetify-loader/lib/loader.js
 * ./node_modules/cache-loader/dist/cjs.js
 * ./node_modules/vue-cli-plugin-import-components/dist/loader.js
 * ./node_modules/vue-loader/lib/index.js
 * ./node_modules/eslint-loader/index.js
You may need an additional loader to handle the result of these loaders.
|
| /* vuetify-loader */
> import installComponents from "!../node_modules/vuetify-loader/lib/runtime/installComponents.js"
| import { VApp } from 'vuetify/lib/components/VApp';

I think you can easily replicate this environment in a Vue CLI app by doing vue add vuetify.

Support for monorepo

Hi there, I am wondering if it would be possible to use vue-cli-plugin-import-components to automatically resolve components provided by another package (as in a monorepo), specified by:

// /my-monorepo-project/package-a/vue.config.js

module.exports = {
  pluginOptions: {
    components: {
      path: 'package-b/components',
    },
  },
}

This currently throws an error:
no such file or directory, mkdir '/Users/my-user/repos/my-monorepo-project/package-a/package-b/components'

Cheers

Multiple paths for component directories

Clear and concise description of the problem

Hello. Back again to say thanks for a great plugin! And this time with a small request.

In a certain type of architecture (including mine) there are several folders with unique components, and at different directory levels. The path option only accepts a string, is it possible to remake the plugin so that the option has additional support for an array so that you can pass multiple paths to the desired component directories?

Suggested solution

Alternative

No response

Additional context

No response

Validations

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.