New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

astro-auto-import

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-auto-import

Auto-import components in Astro projects

  • 0.4.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by4.35%
Maintainers
1
Weekly downloads
 
Created
Source

astro-auto-import

This an Astro integration that allows you to auto-import components or other modules and access them in MDX files without importing them.

Installation

npm i astro-auto-import

If you aren’t already using MDX, you’ll need to add it too:

npx astro add mdx

Usage

Import the integration and add it to your astro.config.* file:

import { defineConfig } from 'astro/config';
import AutoImport from 'astro-auto-import';
import mdx from '@astrojs/mdx';

export default defineConfig({
  integrations: [
    AutoImport({
      imports: [
        // Import a component’s default export
        // generates:
        // import A from './src/components/A.astro';
        './src/components/A.astro',

        {
          // Explicitly alias a default export
          // generates:
          // import { default as B } from './src/components/B.astro';
          './src/components/B.astro': [['default', 'B']],

          // Import a module’s named exports
          // generates:
          // import { Tweet, YouTube } from 'astro-embed';
          'astro-embed': ['Tweet', 'YouTube'],

          // Import all named exports from a module as a namespace
          // generates:
          // import * as Components from './src/components';
          './src/components': 'Components',
        },
      ],
    }),

    // Make sure the MDX integration is included AFTER astro-auto-import
    mdx(),
  ],
});

Options

imports

Type: (string | Record<string, (string | [string, string])[]>)[]

An array of items that configure what files are imported and how.

Default exports

For Astro components or other files that have a default export, the easiest option is to provide their path and they will be imported with a name based on the file name.

imports: ['./src/components/A.astro', './src/components/react/ReactComponent.tsx'];

The above config will import A and ReactComponent respectively, so they could be used as <A /> or <ReactComponent />.

Equivalent to
import A from './src/components/A.astro';
import ReactComponent from './src/components/react/ReactComponent.tsx';
Named exports

For script modules or component frameworks that can use named exports, you can pass an object mapping the module to the names you want to import.

imports: [
  {
    'astro-embed': ['Twitter', 'YouTube'],
  },
];

This config will import both the Twitter and YouTube components from the astro-embed package.

Equivalent to
import { Twitter, YouTube } from 'astro-embed';
Import aliasing

In some cases you may want to alias a default or named export to a different name either for convenience or to avoid conflicts. In this case instead of passing strings, you can pass a tuple of ['original-name', 'alias'].

imports: [
  {
    './src/components/B.astro': [['default', 'RenamedB']],
  },
];

This config will import the Astro component in src/components/B.astro but make it available as <RenamedB />.

Equivalent to
import { default as RenamedB } from './src/components/B.astro';
Namespace import

If you want to import all the named exports in a file to a namespace, you can pass a string to set the namespace to import to:

imports: [
  {
    './src/components/index': 'Components',
  },
];

This config would import all the components in an index file, making them available as <Components.A />, <Components.B /> etc.

Equivalent to
import * as Components from './src/components/index';

License

MIT

Keywords

FAQs

Package last updated on 10 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc