1.1.1 • Published 2 months ago

vite-plugin-codegen v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

vite-plugin-codegen

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.

1.1.1

2 months ago

1.1.0

3 months ago

1.1.0-beta.6679

3 months ago

1.1.0-beta.6681

3 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.3

6 months ago

1.0.3-beta.6241

6 months ago

1.0.1-beta.6214

6 months ago

1.0.0

10 months ago

1.0.0-beta.5749

10 months ago

1.0.0-beta.5750

10 months ago

0.15.2

10 months ago

0.15.2-beta.5714

11 months ago

0.15.1

1 year ago

0.15.0

1 year ago

0.15.0-beta.4640

2 years ago

0.15.0-beta.4516

2 years ago

0.15.0-beta.4513

2 years ago