New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

vite-plugin-codegen

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-codegen

A plugin for Vite (v4 and higher) to generate useful modules at bundle-time.

latest
Source
npmnpm
Version
2.1.0
Version published
Weekly downloads
3.3K
-31.89%
Maintainers
1
Weekly downloads
 
Created
Source

vite-plugin-codegen

npm GitHub tag GitHub issues

A plugin for vite to allow bundle-time asset generation. This can be useful to work efficiently with established conventions and reduce duplication and boilerplate code.

It follows pretty much the parcel-plugin-codegen implementation.

Usage

Install the plugin:

npm i vite-plugin-codegen --save-dev

Now in your vite configuration file you can do:

import codegen from 'vite-plugin-codegen';

export default {
  // ...
  plugins: [codegen()],
};

At this point you can reference a .codegen file in any file, e.g., in a TypeScript asset

import generatedModule from './my.codegen';

Create a .codegen file with the structure:

module.exports = function() {
  return `export default function() {}`;
};

Async Generation

You can also use promises in your code generation. As an example, if your .codegen file looks similar to this:

module.exports = function() {
  return callSomeApi().then(result => `export default function() { return ${JSON.stringify(result)}; }`);
};

The new asset will be created asynchronously. Furthermore, you can obviously use require or import directly in your generated code. Since the asset will be run through vite like any other asset, you can use this mechanism to include files from a directory without referencing them explicitly:

module.exports = function() {
  return `
    import { lazy } from 'react';
    export default lazy(() => import(${JSON.stringify(filePath)}));
  `;
};

License

This plugin is released using the MIT license. For more information see the LICENSE file.

Keywords

vite

FAQs

Package last updated on 26 Dec 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