The `umd` package, currently at version 3.0.3, provides a specialized utility for wrapping JavaScript modules with Universal Module Definition (UMD) boilerplate. It is primarily designed for integration into automated build systems, enabling developers to convert their proprietary or standard modules into a format compatible with a wide array of JavaScript environments, including Asynchronous Module Definition (AMD) loaders, CommonJS environments (like Node.js), and traditional browser global script tags. The library supports two primary input formats: a 'return style' module, where the module's export is the direct result of a `return` statement, and CommonJS-style modules which require an explicit `commonJS: true` option. A key differentiator is its synchronous operation and its focus on preventing naming conflicts between multiple UMD modules on the same page. The package offers functions to generate the UMD prelude, postlude, or to wrap an entire module source string. While UMD was crucial for cross-environment compatibility, its necessity has somewhat diminished with the widespread adoption of native ECMAScript Modules (ESM) in modern environments.
npm install umdVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use the `umd` package programmatically to wrap both 'return style' and CommonJS modules, outputting the result to files and console. Requires Node.js to run.
For CommonJS source, use `umd(name, source, { commonJS: true })`. For 'return style', ensure your code concludes with `return yourExportedValue;`.Ensure module names adhere to `/[a-zA-Z0-9$_]+/` patterns, typically camelCase (e.g., `myAwesomeModule`), to avoid unintended transformations.
Consider if ESM or modern bundlers are more appropriate for your module distribution, especially for new projects or targeting modern environments. Use UMD primarily for legacy support.
Ensure that for CommonJS-style source, the `commonJS` option is set: `umd('your-module', yourCjsSource, { commonJS: true });`Modify your module source to explicitly `return` the value you wish to export, for example: `function MyFunc() {...} return MyFunc;`Be aware of the automatic camelCasing and character stripping. If you need a specific global name, ensure your input `name` parameter adheres to JavaScript variable naming conventions (e.g., 'myCoolModule' instead of 'my-cool-module') or inspect the generated UMD output to confirm the exact name.
No dependency data recorded yet.