JSPM

  • Created
  • Published
  • Downloads 374
  • Score
    100M100P100Q97956F
  • License MIT

Plugin for supporting codegen files.

Package Exports

  • esbuild-codegen-plugin
  • esbuild-codegen-plugin/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (esbuild-codegen-plugin) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

esbuild-codegen-plugin

A plugin for esbuild 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 esbuild-codegen-plugin --save-dev

Now in your esbuild configuration file you can do:

const { build } = require('esbuild');
const { codegenPlugin } = require('esbuild-codegen-plugin');

build({
  // ...
  plugins: [codegenPlugin()],
});

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 esbuild 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.