Volt.JS.CommonJS (Volt v0.22.1)

Copy Markdown View Source

Detects local CommonJS/UMD modules and converts them to ES modules for the dev server.

Production builds hand CommonJS files to the bundler, which wraps them itself. The dev server serves each file as a native ES module, so a CommonJS file such as Phoenix's vendored topbar.js needs the same interop before a browser can import topbar from "../vendor/topbar".

Summary

Functions

Return whether the module at path is CommonJS/UMD rather than an ES module.

Bundle the CommonJS file at path into an ES module whose default export is module.exports. Modules it requires are bundled along with it.

Functions

commonjs?(source, path)

@spec commonjs?(String.t(), String.t()) :: boolean()

Return whether the module at path is CommonJS/UMD rather than an ES module.

.cjs and .cts files are CommonJS by extension. A .js file is CommonJS when it has no import or export declarations and uses module.exports, exports, or require.

to_esm(path, opts \\ [])

@spec to_esm(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Bundle the CommonJS file at path into an ES module whose default export is module.exports. Modules it requires are bundled along with it.

Options

  • :modules — directories used to resolve bare require specifiers
  • :module_types — bundler module type overrides