@ms-cloudpack/esm-stub-utilities
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -5,3 +5,24 @@ { | ||
{ | ||
"date": "Wed, 02 Nov 2022 08:10:59 GMT", | ||
"date": "Tue, 15 Nov 2022 08:12:36 GMT", | ||
"tag": "@ms-cloudpack/esm-stub-utilities_v0.4.0", | ||
"version": "0.4.0", | ||
"comments": { | ||
"minor": [ | ||
{ | ||
"author": "dzearing@microsoft.com", | ||
"package": "@ms-cloudpack/esm-stub-utilities", | ||
"commit": "a71972309b615d683a5a3bac7a85c981fb6e89af", | ||
"comment": "When we emit esm stubs, we now try to use import * for object exports, and import for function exports." | ||
}, | ||
{ | ||
"author": "beachball", | ||
"package": "@ms-cloudpack/esm-stub-utilities", | ||
"comment": "Bump @ms-cloudpack/path-utilities to v1.1.1", | ||
"commit": "a71972309b615d683a5a3bac7a85c981fb6e89af" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"date": "Wed, 02 Nov 2022 08:11:20 GMT", | ||
"tag": "@ms-cloudpack/esm-stub-utilities_v0.3.4", | ||
@@ -8,0 +29,0 @@ "version": "0.3.4", |
# Change Log - @ms-cloudpack/esm-stub-utilities | ||
This log was last generated on Wed, 02 Nov 2022 08:10:59 GMT and should not be manually modified. | ||
This log was last generated on Tue, 15 Nov 2022 08:12:36 GMT and should not be manually modified. | ||
<!-- Start content --> | ||
## 0.4.0 | ||
Tue, 15 Nov 2022 08:12:36 GMT | ||
### Minor changes | ||
- When we emit esm stubs, we now try to use import * for object exports, and import for function exports. (dzearing@microsoft.com) | ||
- Bump @ms-cloudpack/path-utilities to v1.1.1 | ||
## 0.3.4 | ||
Wed, 02 Nov 2022 08:10:59 GMT | ||
Wed, 02 Nov 2022 08:11:20 GMT | ||
@@ -11,0 +20,0 @@ ### Patches |
import { slash } from '@ms-cloudpack/path-string-parsing'; | ||
import path from 'path'; | ||
import { initializeBrowserEnvironment } from './initializeBrowserEnvironment.js'; | ||
import { forbiddenExportNames } from './forbiddenExportNames.js'; | ||
import { tryRequire } from './tryRequire.cjs'; | ||
import { forbiddenExportNames } from './forbiddenExportNames.js'; | ||
/** | ||
@@ -20,39 +20,34 @@ * Generates an ESM stub for CommonJS modules. | ||
if (packageExport !== undefined) { | ||
let isDefaultExported = false; | ||
const isExportObject = typeof packageExport === 'object'; | ||
const isExportFunction = typeof packageExport === 'function'; | ||
if (isExportObject || isExportFunction) { | ||
if (isExportFunction || (isExportObject && !Array.isArray(packageExport))) { | ||
// Make sure to filter keywords. | ||
const namedExports = Object.keys(packageExport).filter((name) => forbiddenExportNames.indexOf(name) < 0); | ||
if (namedExports.length || isExportFunction) { | ||
result.push(`import packageExport from "${relativePath}";`); | ||
} | ||
if (namedExports.length) { | ||
result.push(`const {`); | ||
for (const namedExport of namedExports) { | ||
if (namedExport === 'default') { | ||
isDefaultExported = true; | ||
result.push(` default: __moduleDefault,`); | ||
} | ||
else { | ||
result.push(` ${namedExport},`); | ||
} | ||
const hasDefaultExport = 'default' in packageExport; | ||
if (namedExports.length || hasDefaultExport) { | ||
result.push(`import ${isExportFunction ? '' : '* as '}packageExport from "${relativePath}";`); | ||
if (namedExports.length) { | ||
result.push(`const { ${namedExports.join(', ')} } = packageExport;`); | ||
} | ||
result.push(`} = packageExport;`, ``); | ||
if (isDefaultExported) { | ||
result.push(`export {`, ...namedExports.filter((n) => n !== 'default').map((n) => `${n},`), `__moduleDefault as default,`, `}`); | ||
if (hasDefaultExport) { | ||
result.push(`const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default;`); | ||
result.push(`export default defaultExport;`); | ||
} | ||
else { | ||
result.push(`export {`, ...namedExports.map((n) => ` ${n},`), ` packageExport as default,`, `}`); | ||
result.push(`export default packageExport;`); | ||
} | ||
if (namedExports.length) { | ||
result.push(`export { ${namedExports.join(', ')} }`); | ||
} | ||
} | ||
else if (isExportObject) { | ||
else { | ||
// No named exports... | ||
// Object with no members? Must be a polyfill. Just import it and don't export anything. | ||
result.push(`import "${relativePath}";`); | ||
if (isExportFunction) { | ||
result.push(`export { default } from "${relativePath}";`); | ||
} | ||
else { | ||
result.push(`import "${relativePath}";`); | ||
} | ||
} | ||
else { | ||
// Must be a function. Export it as default. | ||
result.push(`export default packageExport;`); | ||
} | ||
} | ||
@@ -68,3 +63,3 @@ else if (typeof packageExport === 'boolean' || typeof packageExport === 'number') { | ||
else { | ||
// not an object or function | ||
// not an object or function (array or undefined) | ||
result.push(`export default packageExport;`); | ||
@@ -71,0 +66,0 @@ } |
@@ -8,48 +8,70 @@ import { describe, it, expect } from '@jest/globals'; | ||
const stubPath = path.join(testScenariosPath, 'esm-stub.js'); | ||
// Remove absolute import paths from the generated stub. | ||
const makeStubSafe = (stub) => { | ||
return stub.replace(/from "[a-zA-Z0-9./:-]+";/g, 'from "---omitted for safety---"'); | ||
}; | ||
describe('createESMStub', () => { | ||
it('should create an ESM stub for CommonJS object export without a default', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'object-export.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from "./object-export.cjs"; | ||
const { | ||
a, | ||
b, | ||
c, | ||
d, | ||
} = packageExport; | ||
export { | ||
a, | ||
b, | ||
c, | ||
d, | ||
packageExport as default, | ||
}" | ||
it('should handle sorted-btree', async () => { | ||
const testScenarioPath = path.join(testScenariosPath, 'b-tree.cjs'); | ||
const testScenarioStubPath = path.join(testScenariosPath, 'b-tree.stub.mjs'); | ||
const stub = await createESMStub(testScenarioPath, testScenarioStubPath); | ||
expect(makeStubSafe(stub)).toMatchInlineSnapshot(` | ||
"import * as packageExport from "---omitted for safety---" | ||
const { defaultComparator, simpleComparator, asSet, EmptyBTree } = packageExport; | ||
const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default; | ||
export default defaultExport; | ||
export { defaultComparator, simpleComparator, asSet, EmptyBTree }" | ||
`); | ||
}); | ||
it('should handle yallist', async () => { | ||
const testScenarioPath = path.join(testScenariosPath, 'yallist.cjs'); | ||
const testScenarioStubPath = path.join(testScenariosPath, 'yallist.stub.mjs'); | ||
const stub = await createESMStub(testScenarioPath, testScenarioStubPath); | ||
expect(makeStubSafe(stub)).toMatchInlineSnapshot(` | ||
"import packageExport from "---omitted for safety---" | ||
const { Node, create } = packageExport; | ||
export default packageExport; | ||
export { Node, create }" | ||
`); | ||
}); | ||
it('should handle events', async () => { | ||
const testScenarioPath = path.join(testScenariosPath, 'events.cjs'); | ||
const testScenarioStubPath = path.join(testScenariosPath, 'events.stub.mjs'); | ||
const stub = await createESMStub(testScenarioPath, testScenarioStubPath); | ||
expect(makeStubSafe(stub)).toMatchInlineSnapshot(` | ||
"import packageExport from "---omitted for safety---" | ||
const { once, EventEmitter, defaultMaxListeners, init, listenerCount } = packageExport; | ||
export default packageExport; | ||
export { once, EventEmitter, defaultMaxListeners, init, listenerCount }" | ||
`); | ||
}); | ||
it('should handle object-assign', async () => { | ||
const testScenarioPath = path.join(testScenariosPath, 'object-assign.cjs'); | ||
const testScenarioStubPath = path.join(testScenariosPath, 'object-assign.stub.mjs'); | ||
const stub = await createESMStub(testScenarioPath, testScenarioStubPath); | ||
expect(makeStubSafe(stub)).toMatchInlineSnapshot(`"export { default } from "---omitted for safety---""`); | ||
}); | ||
it(`should handle react-lifecycles-compat`, async () => { | ||
const testScenarioPath = path.join(testScenariosPath, 'react-lifecycles-compat.cjs'); | ||
const testScenarioStubPath = path.join(testScenariosPath, 'react-lifecycles-compat.stub.mjs'); | ||
const stub = await createESMStub(testScenarioPath, testScenarioStubPath); | ||
expect(makeStubSafe(stub)).toMatchInlineSnapshot(` | ||
"import * as packageExport from "---omitted for safety---" | ||
const { polyfill } = packageExport; | ||
export default packageExport; | ||
export { polyfill }" | ||
`); | ||
}); | ||
it('should create an ESM stub for CommonJS object export with a named default', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'object-export-with-default.cjs'), stubPath)) | ||
.toMatchInlineSnapshot(` | ||
"import packageExport from "./object-export-with-default.cjs"; | ||
const { | ||
default: __moduleDefault, | ||
a, | ||
b, | ||
c, | ||
d, | ||
} = packageExport; | ||
export { | ||
a, | ||
b, | ||
c, | ||
d, | ||
__moduleDefault as default, | ||
}" | ||
"import * as packageExport from "./object-export-with-default.cjs"; | ||
const { a, b, c, d } = packageExport; | ||
const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default; | ||
export default defaultExport; | ||
export { a, b, c, d }" | ||
`); | ||
}); | ||
it('should create an ESM stub for CommonJS function export', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'function-export.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from "./function-export.cjs"; | ||
export default packageExport;" | ||
`); | ||
expect(await createESMStub(path.join(testScenariosPath, 'function-export.cjs'), stubPath)).toMatchInlineSnapshot(`"export { default } from "./function-export.cjs";"`); | ||
}); | ||
@@ -66,99 +88,16 @@ it('should create an ESM stub for CommonJS string export', async () => { | ||
it('can snapshot react', async () => { | ||
expect(await createESMStub('react', stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from "./../react"; | ||
const { | ||
Fragment, | ||
StrictMode, | ||
Profiler, | ||
Suspense, | ||
Children, | ||
Component, | ||
PureComponent, | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, | ||
cloneElement, | ||
createContext, | ||
createElement, | ||
createFactory, | ||
createRef, | ||
forwardRef, | ||
isValidElement, | ||
lazy, | ||
memo, | ||
useCallback, | ||
useContext, | ||
useDebugValue, | ||
useEffect, | ||
useImperativeHandle, | ||
useLayoutEffect, | ||
useMemo, | ||
useReducer, | ||
useRef, | ||
useState, | ||
version, | ||
} = packageExport; | ||
export { | ||
Fragment, | ||
StrictMode, | ||
Profiler, | ||
Suspense, | ||
Children, | ||
Component, | ||
PureComponent, | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, | ||
cloneElement, | ||
createContext, | ||
createElement, | ||
createFactory, | ||
createRef, | ||
forwardRef, | ||
isValidElement, | ||
lazy, | ||
memo, | ||
useCallback, | ||
useContext, | ||
useDebugValue, | ||
useEffect, | ||
useImperativeHandle, | ||
useLayoutEffect, | ||
useMemo, | ||
useReducer, | ||
useRef, | ||
useState, | ||
version, | ||
packageExport as default, | ||
}" | ||
expect(await createESMStub(path.join(testScenariosPath, 'react.development.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import * as packageExport from "./react.development.cjs"; | ||
const { Fragment, StrictMode, Profiler, Suspense, Children, Component, PureComponent, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, useCallback, useContext, useDebugValue, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, version } = packageExport; | ||
export default packageExport; | ||
export { Fragment, StrictMode, Profiler, Suspense, Children, Component, PureComponent, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, useCallback, useContext, useDebugValue, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, version }" | ||
`); | ||
}); | ||
it('can snapshot react-dom', async () => { | ||
expect(await createESMStub('react-dom', stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from "./../react-dom"; | ||
const { | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, | ||
createPortal, | ||
findDOMNode, | ||
flushSync, | ||
hydrate, | ||
render, | ||
unmountComponentAtNode, | ||
unstable_batchedUpdates, | ||
unstable_createPortal, | ||
unstable_renderSubtreeIntoContainer, | ||
version, | ||
} = packageExport; | ||
export { | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, | ||
createPortal, | ||
findDOMNode, | ||
flushSync, | ||
hydrate, | ||
render, | ||
unmountComponentAtNode, | ||
unstable_batchedUpdates, | ||
unstable_createPortal, | ||
unstable_renderSubtreeIntoContainer, | ||
version, | ||
packageExport as default, | ||
}" | ||
expect(await createESMStub(path.join(testScenariosPath, 'react-dom.development.cjs'), stubPath)) | ||
.toMatchInlineSnapshot(` | ||
"import * as packageExport from "./react-dom.development.cjs"; | ||
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, createPortal, findDOMNode, flushSync, hydrate, render, unmountComponentAtNode, unstable_batchedUpdates, unstable_createPortal, unstable_renderSubtreeIntoContainer, version } = packageExport; | ||
export default packageExport; | ||
export { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, createPortal, findDOMNode, flushSync, hydrate, render, unmountComponentAtNode, unstable_batchedUpdates, unstable_createPortal, unstable_renderSubtreeIntoContainer, version }" | ||
`); | ||
@@ -168,11 +107,6 @@ }); | ||
expect(await createESMStub(path.join(testScenariosPath, 'browser-sensitive.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from "./browser-sensitive.cjs"; | ||
const { | ||
getDiv, | ||
} = packageExport; | ||
export { | ||
getDiv, | ||
packageExport as default, | ||
}" | ||
"import * as packageExport from "./browser-sensitive.cjs"; | ||
const { getDiv } = packageExport; | ||
export default packageExport; | ||
export { getDiv }" | ||
`); | ||
@@ -184,12 +118,7 @@ }); | ||
.toMatchInlineSnapshot(` | ||
"import packageExport from "./export-with-keyword.cjs"; | ||
const { | ||
default: __moduleDefault, | ||
a, | ||
} = packageExport; | ||
export { | ||
a, | ||
__moduleDefault as default, | ||
}" | ||
"import * as packageExport from "./export-with-keyword.cjs"; | ||
const { a } = packageExport; | ||
const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default; | ||
export default defaultExport; | ||
export { a }" | ||
`); | ||
@@ -200,17 +129,18 @@ }); | ||
"import packageExport from "./node-fetch.cjs"; | ||
const { | ||
default: __moduleDefault, | ||
Headers, | ||
Request, | ||
Response, | ||
} = packageExport; | ||
export { | ||
Headers, | ||
Request, | ||
Response, | ||
__moduleDefault as default, | ||
}" | ||
const { Headers, Request, Response } = packageExport; | ||
const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default; | ||
export default defaultExport; | ||
export { Headers, Request, Response }" | ||
`); | ||
}); | ||
it('can generate a stub for diff-sequences', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'diff-sequences.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import * as packageExport from "./diff-sequences.cjs"; | ||
const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default; | ||
export default defaultExport;" | ||
`); | ||
}); | ||
it('can generate a stub for isomorphic-ws', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'isomorphic-ws.cjs'), stubPath)).toMatchInlineSnapshot(`"export { default } from "./isomorphic-ws.cjs";"`); | ||
}); | ||
it('can generate a stub from something that returns a boolean', async () => { | ||
@@ -217,0 +147,0 @@ expect(await createESMStub(path.join(testScenariosPath, 'boolean-return.cjs'), stubPath)).toMatchInlineSnapshot(`"export default true;"`); |
@@ -19,2 +19,3 @@ /** | ||
'debugger', | ||
'default', | ||
'delete', | ||
@@ -21,0 +22,0 @@ 'do', |
@@ -31,2 +31,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
globalThis.navigator = dom.window.navigator; | ||
// Simulate WebSocket existence. (Needed for isomorphic-ws) | ||
globalThis.WebSocket = globalThis.window.WebSocket = function () { | ||
/* noop */ | ||
}; | ||
// eslint-disable-next-line etc/no-deprecated | ||
@@ -33,0 +37,0 @@ const createElement = dom.window.document.createElement.bind(dom.window.document); |
@@ -8,5 +8,5 @@ // This file is read by tools that parse documentation comments conforming to the TSDoc standard. | ||
"packageName": "@microsoft/api-extractor", | ||
"packageVersion": "7.33.5" | ||
"packageVersion": "7.33.6" | ||
} | ||
] | ||
} |
{ | ||
"name": "@ms-cloudpack/esm-stub-utilities", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "Generates ESM stubs for CommonJS entry files.", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"@ms-cloudpack/path-utilities": "^1.1.0", | ||
"@ms-cloudpack/path-utilities": "^1.1.1", | ||
"@ms-cloudpack/path-string-parsing": "^1.0.0", | ||
@@ -19,0 +19,0 @@ "atob": "^2.1.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
48833
753