pax_global_header00006660000000000000000000000064147551614030014520gustar00rootroot0000000000000052 comment=24ec93554ba818db6a6223f3f52ffef0a9efd241 esm2umd-0.3.1/000077500000000000000000000000001475516140300130755ustar00rootroot00000000000000esm2umd-0.3.1/.gitattributes000066400000000000000000000000221475516140300157620ustar00rootroot00000000000000bin/* text eol=lf esm2umd-0.3.1/.github/000077500000000000000000000000001475516140300144355ustar00rootroot00000000000000esm2umd-0.3.1/.github/FUNDING.yml000066400000000000000000000000201475516140300162420ustar00rootroot00000000000000github: dcodeIO esm2umd-0.3.1/.github/workflows/000077500000000000000000000000001475516140300164725ustar00rootroot00000000000000esm2umd-0.3.1/.github/workflows/lint.yml000066400000000000000000000007161475516140300201670ustar00rootroot00000000000000name: Lint on: push: branches: - main pull_request: workflow_dispatch: jobs: test: name: Test runs-on: ubuntu-latest strategy: matrix: node_version: ["current"] steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node_version }} - name: Install dependencies run: npm ci --no-audit - name: Lint run: npm run lint esm2umd-0.3.1/.github/workflows/publish.yml000066400000000000000000000015461475516140300206710ustar00rootroot00000000000000name: Publish on: workflow_dispatch: jobs: publish: name: Publish if: github.repository == 'dcodeIO/esm2umd' runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: ref: main fetch-depth: 0 - uses: actions/setup-node@v4 with: node-version: current - name: Install dependencies run: npm ci - name: Build run: npm run build - name: Run tests run: npm test - name: Publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | VERSION=$(npx aspublish --version) if [ -z "$VERSION" ]; then echo "Changes do not trigger a release" else echo "Publishing new version: $VERSION" npx aspublish fi esm2umd-0.3.1/.github/workflows/stale.yml000066400000000000000000000021341475516140300203250ustar00rootroot00000000000000name: Stale on: schedule: - cron: "0 0 * * *" jobs: stale: runs-on: ubuntu-latest steps: - uses: actions/stale@v9 with: stale-issue-message: "This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!" stale-issue-label: "stale" stale-pr-message: "This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!" close-pr-message: "This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards." exempt-issue-labels: "bug,enhancement,compatibility" exempt-pr-labels: "breaking change" exempt-draft-pr: true exempt-all-milestones: true exempt-all-assignees: true days-before-issue-stale: 30 days-before-pr-stale: 60 days-before-close: 7 esm2umd-0.3.1/.github/workflows/test.yml000066400000000000000000000010071475516140300201720ustar00rootroot00000000000000name: Test on: push: branches: - main pull_request: workflow_dispatch: jobs: test: name: Test runs-on: ubuntu-latest strategy: matrix: node_version: ["current", "lts/*"] steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node_version }} - name: Install dependencies run: npm ci --no-audit - name: Build run: npm run build - name: Run tests run: npm test esm2umd-0.3.1/.gitignore000066400000000000000000000000471475516140300150660ustar00rootroot00000000000000node_modules/ npm-debug.* umd/index.js esm2umd-0.3.1/.prettierrc.js000066400000000000000000000000231475516140300156670ustar00rootroot00000000000000export default {}; esm2umd-0.3.1/README.md000066400000000000000000000051141475516140300143550ustar00rootroot00000000000000# esm2umd Transforms ESM to UMD, i.e. to use ESM by default with UMD as a legacy fallback. [![Build Status](https://img.shields.io/github/actions/workflow/status/dcodeIO/esm2umd/test.yml?branch=main&label=test&logo=github)](https://github.com/dcodeIO/esm2umd/actions/workflows/test.yml) [![Publish Status](https://img.shields.io/github/actions/workflow/status/dcodeIO/esm2umd/publish.yml?branch=main&label=publish&logo=github)](https://github.com/dcodeIO/esm2umd/actions/workflows/publish.yml) [![npm](https://img.shields.io/npm/v/esm2umd.svg?label=npm&color=007acc&logo=npm)](https://www.npmjs.com/package/esm2umd) ## Usage ``` $> npm install --save-dev esm2umd ``` ``` npx esm2umd MyModule esmFile.js > umdFile.js ``` `MyModule` is used as the name of the vanilla JS global. If the module has a `default` export, it is transformed to a whole-module export. ## API ```js import esm2umd from "esm2umd"; const esmCode = "..."; const umdCode = esm2umd("ModuleName", esmCode); ``` ## Examples Common outline of a hybrid module with legacy fallback, either exporting a class or a namespace: **package.json** ```json { "type": "module", "main": "./umd/index.js", "types": "./umd/index.d.ts", "exports": { ".": { "import": { "types": "./index.d.ts", "default": "./index.js" }, "require": { "types": "./umd/index.d.ts", "default": "./umd/index.js" } } }, "scripts": { "build": "npx esm2umd MyModule index.js > umd/index.js && cp types.d.ts umd/types.d.ts" } } ``` **umd/package.json** ```json { "type": "commonjs" } ``` **.gitignore** ``` umd/index.js umd/types.d.ts ``` ### Class export As used by [long.js](https://github.com/dcodeIO/long.js): **index.d.ts** ```ts import { MyClass } from "./types.js"; export default MyClass; ``` **types.d.ts** ```ts export declare class MyClass { // ... } ``` **umd/index.d.ts** ```ts import { MyClass } from "./types.js"; export = MyClass; export as namespace MyClass; ``` **umd/types.d.ts** Copy of types.d.ts. ### Namespace export As used by [bcrypt.js](https://github.com/dcodeIO/bcrypt.js): **index.d.ts** ```ts import * as myNamespace from "./types.js"; export * from "./types.js"; export default myNamespace; ``` **types.d.ts** ```ts export declare function myFunction(): void; // ... ``` **umd/index.d.ts** ```ts import * as myNamespace from "./types.js"; export = myNamespace; export as namespace myNamespace; ``` **umd/types.d.ts** Copy of types.d.ts. ## Building Building the UMD fallback: ``` $> npm run build ``` Running the [tests](./tests): ``` $> npm test ``` esm2umd-0.3.1/bin/000077500000000000000000000000001475516140300136455ustar00rootroot00000000000000esm2umd-0.3.1/bin/esm2umd.js000066400000000000000000000004361475516140300155620ustar00rootroot00000000000000#!/usr/bin/env node import fs from "fs"; import esm2umd from "../index.js"; if (process.argv.length < 4) { console.log("Usage: esm2umd ModuleName esmFile.js > umdFile.js"); process.exit(1); } process.stdout.write( esm2umd(process.argv[2], fs.readFileSync(process.argv[3])), ); esm2umd-0.3.1/index.d.ts000066400000000000000000000000761475516140300150010ustar00rootroot00000000000000import { esm2umd } from "./types.js"; export default esm2umd; esm2umd-0.3.1/index.js000066400000000000000000000163411475516140300145470ustar00rootroot00000000000000import { declare } from "@babel/helper-plugin-utils"; import { basename, extname } from "path"; import { isModule, rewriteModuleStatementsAndPrepareHeader, hasExports, isSideEffectImport, buildNamespaceInitStatements, ensureStatementsHoisted, wrapInterop, getModuleName, } from "@babel/helper-module-transforms"; import { types as t, template, transform } from "@babel/core"; const buildPrerequisiteAssignment = template(` GLOBAL_REFERENCE = GLOBAL_REFERENCE || {} `); const buildWrapper = template(` (function (global, factory) { function preferDefault(exports) { return exports.default || exports; } if (typeof define === "function" && define.amd) { define(AMD_ARGUMENTS, function(FORWARD_NAMES) { var exports = {}; factory(exports, FORWARD_NAMES); return preferDefault(exports); }); } else if (typeof exports === "object") { factory(COMMONJS_ARGUMENTS); if (typeof module === "object") module.exports = preferDefault(exports); } else { (function() { var exports = {}; factory(BROWSER_ARGUMENTS); GLOBAL_TO_ASSIGN; })(); } })( typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function(IMPORT_NAMES) { }) `); const transformModulesUmd = declare((api, options) => { api.assertVersion(7); const { globals, exactGlobals, loose, allowTopLevelThis, strict, strictMode, noInterop, } = options; /** * Build the assignment statements that initialize the UMD global. */ function buildBrowserInit( browserGlobals, exactGlobals, filename, moduleName, ) { const moduleNameOrBasename = moduleName ? moduleName.value : basename(filename, extname(filename)); let globalToAssign = t.memberExpression( t.identifier("global"), t.identifier(t.toIdentifier(moduleNameOrBasename)), ); let initAssignments = []; if (exactGlobals) { const globalName = browserGlobals[moduleNameOrBasename]; if (globalName) { initAssignments = []; const members = globalName.split("."); globalToAssign = members.slice(1).reduce( (accum, curr) => { initAssignments.push( buildPrerequisiteAssignment({ GLOBAL_REFERENCE: t.cloneNode(accum), }), ); return t.memberExpression(accum, t.identifier(curr)); }, t.memberExpression(t.identifier("global"), t.identifier(members[0])), ); } } initAssignments.push( t.expressionStatement( t.assignmentExpression( "=", globalToAssign, t.callExpression(t.identifier("preferDefault"), [ t.identifier("exports"), ]), ), ), ); return initAssignments; } /** * Build the member expression that reads from a global for a given source. */ function buildBrowserArg(browserGlobals, exactGlobals, source) { let memberExpression; if (exactGlobals) { const globalRef = browserGlobals[source]; if (globalRef) { memberExpression = globalRef .split(".") .reduce( (accum, curr) => t.memberExpression(accum, t.identifier(curr)), t.identifier("global"), ); } else { memberExpression = t.memberExpression( t.identifier("global"), t.identifier(t.toIdentifier(source)), ); } } else { const requireName = basename(source, extname(source)); const globalName = browserGlobals[requireName] || requireName; memberExpression = t.memberExpression( t.identifier("global"), t.identifier(t.toIdentifier(globalName)), ); } return memberExpression; } return { name: "transform-modules-umd", visitor: { Program: { exit(path) { if (!isModule(path)) return; const browserGlobals = globals || {}; let moduleName = getModuleName(this.file.opts, options); if (moduleName) moduleName = t.stringLiteral(moduleName); const { meta, headers } = rewriteModuleStatementsAndPrepareHeader( path, { loose, strict, strictMode, allowTopLevelThis, noInterop, }, ); const amdArgs = []; const commonjsArgs = []; const browserArgs = []; const importNames = []; const forwardNames = []; if (hasExports(meta)) { commonjsArgs.push(t.identifier("exports")); browserArgs.push(t.identifier("exports")); importNames.push(t.identifier(meta.exportName)); } for (const [source, metadata] of meta.source) { amdArgs.push(t.stringLiteral(source)); commonjsArgs.push( t.callExpression(t.identifier("require"), [ t.stringLiteral(source), ]), ); browserArgs.push( buildBrowserArg(browserGlobals, exactGlobals, source), ); importNames.push(t.identifier(metadata.name)); forwardNames.push(t.identifier(metadata.name)); if (!isSideEffectImport(metadata)) { const interop = wrapInterop( path, t.identifier(metadata.name), metadata.interop, ); if (interop) { const header = t.expressionStatement( t.assignmentExpression( "=", t.identifier(metadata.name), interop, ), ); header.loc = meta.loc; headers.push(header); } } headers.push( ...buildNamespaceInitStatements(meta, metadata, loose), ); } ensureStatementsHoisted(headers); path.unshiftContainer("body", headers); const { body, directives } = path.node; path.node.directives = []; path.node.body = []; const umdWrapper = path.pushContainer("body", [ buildWrapper({ // MODULE_NAME: moduleName, AMD_ARGUMENTS: t.arrayExpression(amdArgs), COMMONJS_ARGUMENTS: commonjsArgs, BROWSER_ARGUMENTS: browserArgs, IMPORT_NAMES: importNames, FORWARD_NAMES: forwardNames, GLOBAL_TO_ASSIGN: buildBrowserInit( browserGlobals, exactGlobals, this.filename || "unknown", moduleName, ), }), ])[0]; const umdFactory = umdWrapper .get("expression.arguments")[1] .get("body"); umdFactory.pushContainer("directives", directives); umdFactory.pushContainer("body", body); }, }, }, }; }); export default function esm2umd(moduleName, esmCode, options = {}) { return ( "// GENERATED FILE. DO NOT EDIT.\n" + transform(esmCode, { plugins: [[transformModulesUmd, options]], moduleId: moduleName, }).code.trim() ); } esm2umd-0.3.1/package-lock.json000066400000000000000000000505051475516140300163160ustar00rootroot00000000000000{ "name": "esm2umd", "version": "0.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "esm2umd", "version": "0.0.0", "license": "MIT", "dependencies": { "@babel/core": "^7.26.8", "@babel/helper-plugin-utils": "^7.26.5" }, "bin": { "esm2umd": "bin/esm2umd.js" }, "devDependencies": { "prettier": "^3.5.0", "requirejs": "^2.3.7", "typescript": "^5.7.3" } }, "node_modules/@ampproject/remapping": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz", "integrity": "sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==", "license": "Apache-2.0", "dependencies": { "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.24" }, "engines": { "node": ">=6.0.0" } }, "node_modules/@babel/code-frame": { "version": "7.26.2", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.26.2.tgz", "integrity": "sha512-RJlIHRueQgwWitWgF8OdFYGZX328Ax5BCemNGlqHfplnRT9ESi8JkFlvaVYbS+UubVY6dpv87Fs2u5M29iNFVQ==", "license": "MIT", "dependencies": { "@babel/helper-validator-identifier": "^7.25.9", "js-tokens": "^4.0.0", "picocolors": "^1.0.0" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/compat-data": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.26.8.tgz", "integrity": "sha512-oH5UPLMWR3L2wEFLnFJ1TZXqHufiTKAiLfqw5zkhS4dKXLJ10yVztfil/twG8EDTA4F/tvVNw9nOl4ZMslB8rQ==", "license": "MIT", "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/core": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.26.8.tgz", "integrity": "sha512-l+lkXCHS6tQEc5oUpK28xBOZ6+HwaH7YwoYQbLFiYb4nS2/l1tKnZEtEWkD0GuiYdvArf9qBS0XlQGXzPMsNqQ==", "license": "MIT", "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.26.2", "@babel/generator": "^7.26.8", "@babel/helper-compilation-targets": "^7.26.5", "@babel/helper-module-transforms": "^7.26.0", "@babel/helpers": "^7.26.7", "@babel/parser": "^7.26.8", "@babel/template": "^7.26.8", "@babel/traverse": "^7.26.8", "@babel/types": "^7.26.8", "@types/gensync": "^1.0.0", "convert-source-map": "^2.0.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", "json5": "^2.2.3", "semver": "^6.3.1" }, "engines": { "node": ">=6.9.0" }, "funding": { "type": "opencollective", "url": "https://opencollective.com/babel" } }, "node_modules/@babel/generator": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.26.8.tgz", "integrity": "sha512-ef383X5++iZHWAXX0SXQR6ZyQhw/0KtTkrTz61WXRhFM6dhpHulO/RJz79L8S6ugZHJkOOkUrUdxgdF2YiPFnA==", "license": "MIT", "dependencies": { "@babel/parser": "^7.26.8", "@babel/types": "^7.26.8", "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.25", "jsesc": "^3.0.2" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-compilation-targets": { "version": "7.26.5", "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.26.5.tgz", "integrity": "sha512-IXuyn5EkouFJscIDuFF5EsiSolseme1s0CZB+QxVugqJLYmKdxI1VfIBOst0SUu4rnk2Z7kqTwmoO1lp3HIfnA==", "license": "MIT", "dependencies": { "@babel/compat-data": "^7.26.5", "@babel/helper-validator-option": "^7.25.9", "browserslist": "^4.24.0", "lru-cache": "^5.1.1", "semver": "^6.3.1" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-module-imports": { "version": "7.25.9", "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.25.9.tgz", "integrity": "sha512-tnUA4RsrmflIM6W6RFTLFSXITtl0wKjgpnLgXyowocVPrbYrLUXSBXDgTs8BlbmIzIdlBySRQjINYs2BAkiLtw==", "license": "MIT", "dependencies": { "@babel/traverse": "^7.25.9", "@babel/types": "^7.25.9" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-module-transforms": { "version": "7.26.0", "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.26.0.tgz", "integrity": "sha512-xO+xu6B5K2czEnQye6BHA7DolFFmS3LB7stHZFaOLb1pAwO1HWLS8fXA+eh0A2yIvltPVmx3eNNDBJA2SLHXFw==", "license": "MIT", "dependencies": { "@babel/helper-module-imports": "^7.25.9", "@babel/helper-validator-identifier": "^7.25.9", "@babel/traverse": "^7.25.9" }, "engines": { "node": ">=6.9.0" }, "peerDependencies": { "@babel/core": "^7.0.0" } }, "node_modules/@babel/helper-plugin-utils": { "version": "7.26.5", "resolved": "https://registry.npmjs.org/@babel/helper-plugin-utils/-/helper-plugin-utils-7.26.5.tgz", "integrity": "sha512-RS+jZcRdZdRFzMyr+wcsaqOmld1/EqTghfaBGQQd/WnRdzdlvSZ//kF7U8VQTxf1ynZ4cjUcYgjVGx13ewNPMg==", "license": "MIT", "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-string-parser": { "version": "7.25.9", "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.25.9.tgz", "integrity": "sha512-4A/SCr/2KLd5jrtOMFzaKjVtAei3+2r/NChoBNoZ3EyP/+GlhoaEGoWOZUmFmoITP7zOJyHIMm+DYRd8o3PvHA==", "license": "MIT", "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-validator-identifier": { "version": "7.25.9", "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.25.9.tgz", "integrity": "sha512-Ed61U6XJc3CVRfkERJWDz4dJwKe7iLmmJsbOGu9wSloNSFttHV0I8g6UAgb7qnK5ly5bGLPd4oXZlxCdANBOWQ==", "license": "MIT", "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-validator-option": { "version": "7.25.9", "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.25.9.tgz", "integrity": "sha512-e/zv1co8pp55dNdEcCynfj9X7nyUKUXoUEwfXqaZt0omVOmDe9oOTdKStH4GmAw6zxMFs50ZayuMfHDKlO7Tfw==", "license": "MIT", "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helpers": { "version": "7.26.7", "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.26.7.tgz", "integrity": "sha512-8NHiL98vsi0mbPQmYAGWwfcFaOy4j2HY49fXJCfuDcdE7fMIsH9a7GdaeXpIBsbT7307WU8KCMp5pUVDNL4f9A==", "license": "MIT", "dependencies": { "@babel/template": "^7.25.9", "@babel/types": "^7.26.7" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/parser": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.26.8.tgz", "integrity": "sha512-TZIQ25pkSoaKEYYaHbbxkfL36GNsQ6iFiBbeuzAkLnXayKR1yP1zFe+NxuZWWsUyvt8icPU9CCq0sgWGXR1GEw==", "license": "MIT", "dependencies": { "@babel/types": "^7.26.8" }, "bin": { "parser": "bin/babel-parser.js" }, "engines": { "node": ">=6.0.0" } }, "node_modules/@babel/template": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.26.8.tgz", "integrity": "sha512-iNKaX3ZebKIsCvJ+0jd6embf+Aulaa3vNBqZ41kM7iTWjx5qzWKXGHiJUW3+nTpQ18SG11hdF8OAzKrpXkb96Q==", "license": "MIT", "dependencies": { "@babel/code-frame": "^7.26.2", "@babel/parser": "^7.26.8", "@babel/types": "^7.26.8" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/traverse": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.26.8.tgz", "integrity": "sha512-nic9tRkjYH0oB2dzr/JoGIm+4Q6SuYeLEiIiZDwBscRMYFJ+tMAz98fuel9ZnbXViA2I0HVSSRRK8DW5fjXStA==", "license": "MIT", "dependencies": { "@babel/code-frame": "^7.26.2", "@babel/generator": "^7.26.8", "@babel/parser": "^7.26.8", "@babel/template": "^7.26.8", "@babel/types": "^7.26.8", "debug": "^4.3.1", "globals": "^11.1.0" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/types": { "version": "7.26.8", "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.26.8.tgz", "integrity": "sha512-eUuWapzEGWFEpHFxgEaBG8e3n6S8L3MSu0oda755rOfabWPnh0Our1AozNFVUxGFIhbKgd1ksprsoDGMinTOTA==", "license": "MIT", "dependencies": { "@babel/helper-string-parser": "^7.25.9", "@babel/helper-validator-identifier": "^7.25.9" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.8", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.8.tgz", "integrity": "sha512-imAbBGkb+ebQyxKgzv5Hu2nmROxoDOXHh80evxdoXNOrvAnVx7zimzc1Oo5h9RlfV4vPXaE2iM5pOFbvOCClWA==", "license": "MIT", "dependencies": { "@jridgewell/set-array": "^1.2.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.24" }, "engines": { "node": ">=6.0.0" } }, "node_modules/@jridgewell/resolve-uri": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", "license": "MIT", "engines": { "node": ">=6.0.0" } }, "node_modules/@jridgewell/set-array": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.2.1.tgz", "integrity": "sha512-R8gLRTZeyp03ymzP/6Lil/28tGeGEzhx1q2k703KGWRAI1VdvPIXdG70VJc2pAMw3NA6JKL5hhFu1sJX0Mnn/A==", "license": "MIT", "engines": { "node": ">=6.0.0" } }, "node_modules/@jridgewell/sourcemap-codec": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.0.tgz", "integrity": "sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==", "license": "MIT" }, "node_modules/@jridgewell/trace-mapping": { "version": "0.3.25", "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.25.tgz", "integrity": "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==", "license": "MIT", "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "node_modules/@types/gensync": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/@types/gensync/-/gensync-1.0.4.tgz", "integrity": "sha512-C3YYeRQWp2fmq9OryX+FoDy8nXS6scQ7dPptD8LnFDAUNcKWJjXQKDNJD3HVm+kOUsXhTOkpi69vI4EuAr95bA==", "license": "MIT" }, "node_modules/browserslist": { "version": "4.24.4", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.24.4.tgz", "integrity": "sha512-KDi1Ny1gSePi1vm0q4oxSF8b4DR44GF4BbmS2YdhPLOEqd8pDviZOGH/GsmRwoWJ2+5Lr085X7naowMwKHDG1A==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/browserslist" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/browserslist" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "license": "MIT", "dependencies": { "caniuse-lite": "^1.0.30001688", "electron-to-chromium": "^1.5.73", "node-releases": "^2.0.19", "update-browserslist-db": "^1.1.1" }, "bin": { "browserslist": "cli.js" }, "engines": { "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, "node_modules/caniuse-lite": { "version": "1.0.30001699", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001699.tgz", "integrity": "sha512-b+uH5BakXZ9Do9iK+CkDmctUSEqZl+SP056vc5usa0PL+ev5OHw003rZXcnjNDv3L8P5j6rwT6C0BPKSikW08w==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/browserslist" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/caniuse-lite" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "license": "CC-BY-4.0" }, "node_modules/convert-source-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", "license": "MIT" }, "node_modules/debug": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.0.tgz", "integrity": "sha512-6WTZ/IxCY/T6BALoZHaE4ctp9xm+Z5kY/pzYaCHRFeyVhojxlrm+46y68HA6hr0TcwEssoxNiDEUJQjfPZ/RYA==", "license": "MIT", "dependencies": { "ms": "^2.1.3" }, "engines": { "node": ">=6.0" }, "peerDependenciesMeta": { "supports-color": { "optional": true } } }, "node_modules/electron-to-chromium": { "version": "1.5.96", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.96.tgz", "integrity": "sha512-8AJUW6dh75Fm/ny8+kZKJzI1pgoE8bKLZlzDU2W1ENd+DXKJrx7I7l9hb8UWR4ojlnb5OlixMt00QWiYJoVw1w==", "license": "ISC" }, "node_modules/escalade": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==", "license": "MIT", "engines": { "node": ">=6" } }, "node_modules/gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", "integrity": "sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==", "license": "MIT", "engines": { "node": ">=6.9.0" } }, "node_modules/globals": { "version": "11.12.0", "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", "license": "MIT", "engines": { "node": ">=4" } }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", "license": "MIT" }, "node_modules/jsesc": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz", "integrity": "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA==", "license": "MIT", "bin": { "jsesc": "bin/jsesc" }, "engines": { "node": ">=6" } }, "node_modules/json5": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "license": "MIT", "bin": { "json5": "lib/cli.js" }, "engines": { "node": ">=6" } }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", "license": "ISC", "dependencies": { "yallist": "^3.0.2" } }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "license": "MIT" }, "node_modules/node-releases": { "version": "2.0.19", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz", "integrity": "sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw==", "license": "MIT" }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", "license": "ISC" }, "node_modules/prettier": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.0.tgz", "integrity": "sha512-quyMrVt6svPS7CjQ9gKb3GLEX/rl3BCL2oa/QkNcXv4YNVBC9olt3s+H7ukto06q7B1Qz46PbrKLO34PR6vXcA==", "dev": true, "license": "MIT", "bin": { "prettier": "bin/prettier.cjs" }, "engines": { "node": ">=14" }, "funding": { "url": "https://github.com/prettier/prettier?sponsor=1" } }, "node_modules/requirejs": { "version": "2.3.7", "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.7.tgz", "integrity": "sha512-DouTG8T1WanGok6Qjg2SXuCMzszOo0eHeH9hDZ5Y4x8Je+9JB38HdTLT4/VA8OaUhBa0JPVHJ0pyBkM1z+pDsw==", "dev": true, "license": "MIT", "bin": { "r_js": "bin/r.js", "r.js": "bin/r.js" }, "engines": { "node": ">=0.4.0" } }, "node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "license": "ISC", "bin": { "semver": "bin/semver.js" } }, "node_modules/typescript": { "version": "5.7.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz", "integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==", "dev": true, "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { "node": ">=14.17" } }, "node_modules/update-browserslist-db": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.2.tgz", "integrity": "sha512-PPypAm5qvlD7XMZC3BujecnaOxwhrtoFR+Dqkk5Aa/6DssiH0ibKoketaj9w8LP7Bont1rYeoV5plxD7RTEPRg==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/browserslist" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/browserslist" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "license": "MIT", "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" }, "bin": { "update-browserslist-db": "cli.js" }, "peerDependencies": { "browserslist": ">= 4.21.0" } }, "node_modules/yallist": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==", "license": "ISC" } } } esm2umd-0.3.1/package.json000066400000000000000000000027641475516140300153740ustar00rootroot00000000000000{ "name": "esm2umd", "description": "Transforms ESM to UMD.", "version": "0.0.0", "license": "MIT", "type": "module", "main": "umd/index.js", "types": "umd/index.d.ts", "exports": { ".": { "import": { "types": "./index.d.ts", "default": "./index.js" }, "require": { "types": "./umd/index.d.ts", "default": "./umd/index.js" } } }, "bin": { "esm2umd": "bin/esm2umd.js" }, "repository": { "type": "git", "url": "git+https://github.com/dcodeIO/esm2umd.git" }, "dependencies": { "@babel/core": "^7.26.8", "@babel/helper-plugin-utils": "^7.26.5" }, "scripts": { "build": "node bin/esm2umd esm2umd index.js > umd/index.js && prettier --write umd/index.js", "lint": "prettier --check .", "format": "prettier --write .", "prepublishOnly": "npm run build", "test": "npm run test:unit && npm run test:typescript", "test:unit": "node tests", "test:typescript": "tsc --project tests/typescript/tsconfig.esnext.json && tsc --project tests/typescript/tsconfig.nodenext.json && tsc --project tests/typescript/tsconfig.commonjs.json && tsc --project tests/typescript/tsconfig.global.json" }, "files": [ "index.js", "index.d.ts", "umd/index.js", "umd/index.d.ts", "umd/package.json", "types.d.ts", "package.json", "README.md", "bin/esm2umd.js" ], "devDependencies": { "prettier": "^3.5.0", "requirejs": "^2.3.7", "typescript": "^5.7.3" } } esm2umd-0.3.1/tests/000077500000000000000000000000001475516140300142375ustar00rootroot00000000000000esm2umd-0.3.1/tests/index.js000066400000000000000000000041711475516140300157070ustar00rootroot00000000000000const assert = require("assert"); const util = require("util"); const path = require("path"); const fs = require("fs"); const vm = require("vm"); const tests = [ async function esm() { const index = await import("../index.js"); assert.equal(Object.prototype.toString.call(index), "[object Module]"); assert(typeof index === "object"); assert(typeof index.default === "function"); console.log(util.inspect(index, { showHidden: true })); }, async function commonjs() { const index = require("../umd/index.js"); assert.equal(Object.prototype.toString.call(index), "[object Function]"); assert(typeof index === "function"); console.log(util.inspect(index, { showHidden: true })); }, async function global() { const umdSource = await fs.promises.readFile( path.join(__dirname, "..", "umd", "index.js"), "utf8", ); const context = vm.createContext({ window: {}, helperPluginUtils: { declare: function () {} }, path: {}, helperModuleTransforms: {}, core: { template: function () {} }, }); context.window = context; vm.runInContext(umdSource, context); const index = context.esm2umd; assert.equal(Object.prototype.toString.call(index), "[object Function]"); assert(typeof index === "function"); console.log(util.inspect(index, { showHidden: true })); }, function amd() { return new Promise((resolve, reject) => { const requirejs = require("requirejs"); requirejs.config({ baseUrl: path.join(__dirname, "..", "umd"), paths: { esm2umd: "index", }, }); requirejs(["esm2umd"], function (index) { try { assert.equal( Object.prototype.toString.call(index), "[object Function]", ); assert(typeof index === "function"); console.log(util.inspect(index, { showHidden: true })); resolve(); } catch (err) { reject(err); } }); }); }, ]; function next() { if (!tests.length) return; const test = tests.shift(); console.log(test.name); test().then(next); } next(); esm2umd-0.3.1/tests/package.json000066400000000000000000000000311475516140300165170ustar00rootroot00000000000000{ "type": "commonjs" } esm2umd-0.3.1/tests/typescript/000077500000000000000000000000001475516140300164455ustar00rootroot00000000000000esm2umd-0.3.1/tests/typescript/package.json000066400000000000000000000000271475516140300207320ustar00rootroot00000000000000{ "type": "module" } esm2umd-0.3.1/tests/typescript/test-global.ts000066400000000000000000000001101475516140300212220ustar00rootroot00000000000000/// esm2umd("test", "test"); esm2umd-0.3.1/tests/typescript/test-import.ts000066400000000000000000000001311475516140300212770ustar00rootroot00000000000000import esm2umd from "../../index.js"; esm2umd("test", "test"); export default esm2umd; esm2umd-0.3.1/tests/typescript/test-require.ts000066400000000000000000000001351475516140300214450ustar00rootroot00000000000000import esm2umd = require("../../umd/index.js"); esm2umd("test", "test"); export = esm2umd; esm2umd-0.3.1/tests/typescript/tsconfig.base.json000066400000000000000000000001121475516140300220570ustar00rootroot00000000000000{ "compilerOptions": { "target": "ESNext", "noEmit": true } } esm2umd-0.3.1/tests/typescript/tsconfig.commonjs.json000066400000000000000000000002431475516140300227770ustar00rootroot00000000000000{ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "CommonJS", "moduleResolution": "node10" }, "include": ["./test-require.ts"] } esm2umd-0.3.1/tests/typescript/tsconfig.esnext.json000066400000000000000000000001761475516140300224650ustar00rootroot00000000000000{ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "ESNext" }, "include": ["./test-import.ts"] } esm2umd-0.3.1/tests/typescript/tsconfig.global.json000066400000000000000000000001741475516140300224150ustar00rootroot00000000000000{ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "None" }, "include": ["./test-global.ts"] } esm2umd-0.3.1/tests/typescript/tsconfig.json000066400000000000000000000000541475516140300211530ustar00rootroot00000000000000{ "extends": "./tsconfig.commonjs.json" } esm2umd-0.3.1/tests/typescript/tsconfig.nodenext.json000066400000000000000000000002441475516140300227770ustar00rootroot00000000000000{ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "NodeNext", "moduleResolution": "nodenext" }, "include": ["./test-import.ts"] } esm2umd-0.3.1/types.d.ts000066400000000000000000000003321475516140300150310ustar00rootroot00000000000000/** * Transforms the given ESM code to UMD. Uses `moduleName` as the name of the * vanilla JS global. */ export function esm2umd( moduleName: string, esmCode: string, options?: Record, ): string; esm2umd-0.3.1/umd/000077500000000000000000000000001475516140300136625ustar00rootroot00000000000000esm2umd-0.3.1/umd/index.d.ts000066400000000000000000000001261475516140300155620ustar00rootroot00000000000000import { esm2umd } from "../types.js"; export = esm2umd; export as namespace esm2umd; esm2umd-0.3.1/umd/package.json000066400000000000000000000000311475516140300161420ustar00rootroot00000000000000{ "type": "commonjs" }