esbuild import assertions plugin

raw JSON →
1.0.1 verified Fri May 01 auth: no javascript

A plugin for esbuild that enables import assertion syntax (e.g. `import json from './foo.json' assert { type: 'json' }`) in your builds. Current stable version is 1.0.1. This plugin adds the ability to use the TC39 proposal for import assertions, which esbuild does not natively support. It works only with synchronous imports and requires `bundle: true`. Key differentiator: it fills a gap for users who need import assertions without switching to a different bundler like Webpack or Rollup.

error Error: The package "esbuild-plugin-import-assertions" is not a CommonJS module, use dynamic import() instead.
cause Attempting to require the package in a CommonJS environment without using dynamic import.
fix
Use import() or change the project to ESM (type: 'module' in package.json).
error TypeError: importAssertPlugin is not a function
cause Importing the default export instead of the named export 'importAssertPlugin'.
fix
Use named import: import { importAssertPlugin } from 'esbuild-plugin-import-assertions';
error Error: Import assertions are not supported with dynamic imports
cause Using dynamic import() with import assertions, which the plugin does not support.
fix
Convert to static import statements.
gotcha Asynchronous imports are not supported. Only static import statements with assertions will be handled.
fix Rewrite dynamic import() calls with assertions as static imports if possible, or use an alternative approach.
gotcha The plugin requires that esbuild's 'bundle' option is set to true. Without it, the plugin will not transform imports.
fix Set 'bundle: true' in the esbuild options.
npm install esbuild-plugin-import-assertions
yarn add esbuild-plugin-import-assertions
pnpm add esbuild-plugin-import-assertions

Demonstrates basic esbuild build with the import assertions plugin, requiring bundle: true.

import { importAssertPlugin } from 'esbuild-plugin-import-assertions';
import { build } from 'esbuild';

build({
  entryPoints: ['./app.ts'],
  bundle: true,
  outfile: './lib/out.js',
  plugins: [importAssertPlugin],
  target: ['chrome100']
}).catch(() => process.exit(1));