@ms-cloudpack/esm-stub-utilities
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -5,3 +5,18 @@ { | ||
{ | ||
"date": "Sat, 06 Aug 2022 08:15:24 GMT", | ||
"date": "Fri, 12 Aug 2022 08:12:22 GMT", | ||
"tag": "@ms-cloudpack/esm-stub-utilities_v0.2.1", | ||
"version": "0.2.1", | ||
"comments": { | ||
"patch": [ | ||
{ | ||
"author": "dzearing@microsoft.com", | ||
"package": "@ms-cloudpack/esm-stub-utilities", | ||
"commit": "533958ff437ec4556ac825da167e9ad54193f8f4", | ||
"comment": "Fixing rollup to bundle only supported entries. Also fixing the esm stub creation to asynchronously wait for web environment initialization to complete." | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"date": "Sat, 06 Aug 2022 08:15:40 GMT", | ||
"tag": "@ms-cloudpack/esm-stub-utilities_v0.2.0", | ||
@@ -8,0 +23,0 @@ "version": "0.2.0", |
# Change Log - @ms-cloudpack/esm-stub-utilities | ||
This log was last generated on Sat, 06 Aug 2022 08:15:24 GMT and should not be manually modified. | ||
This log was last generated on Fri, 12 Aug 2022 08:12:22 GMT and should not be manually modified. | ||
<!-- Start content --> | ||
## 0.2.1 | ||
Fri, 12 Aug 2022 08:12:22 GMT | ||
### Patches | ||
- Fixing rollup to bundle only supported entries. Also fixing the esm stub creation to asynchronously wait for web environment initialization to complete. (dzearing@microsoft.com) | ||
## 0.2.0 | ||
Sat, 06 Aug 2022 08:15:24 GMT | ||
Sat, 06 Aug 2022 08:15:40 GMT | ||
@@ -11,0 +19,0 @@ ### Minor changes |
@@ -8,2 +8,2 @@ /** | ||
*/ | ||
export declare function createESMStub(entryPath: string, stubPath: string): string; | ||
export declare function createESMStub(entryPath: string, stubPath: string): Promise<string>; |
@@ -13,7 +13,7 @@ import { slash } from '@ms-cloudpack/path-utilities'; | ||
*/ | ||
export function createESMStub(entryPath, stubPath) { | ||
export async function createESMStub(entryPath, stubPath) { | ||
const result = []; | ||
const relativePath = './' + slash(path.relative(path.dirname(stubPath), entryPath)); | ||
// Try and require the entry file. | ||
initializeBrowserEnvironment(); | ||
await initializeBrowserEnvironment(); | ||
const packageExport = tryRequire(entryPath); | ||
@@ -20,0 +20,0 @@ if (packageExport) { |
@@ -8,4 +8,4 @@ import path from 'path'; | ||
describe('createESMStub', () => { | ||
it('should create an ESM stub for CommonJS object export without a default', () => { | ||
expect(createESMStub(path.join(testScenariosPath, 'object-export.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
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\\"; | ||
@@ -28,4 +28,4 @@ const { | ||
}); | ||
it('should create an ESM stub for CommonJS object export with a named default', () => { | ||
expect(createESMStub(path.join(testScenariosPath, 'object-export-with-default.cjs'), stubPath)) | ||
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(` | ||
@@ -50,4 +50,4 @@ "import packageExport from \\"./object-export-with-default.cjs\\"; | ||
}); | ||
it('should create an ESM stub for CommonJS function export', () => { | ||
expect(createESMStub(path.join(testScenariosPath, 'function-export.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
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\\"; | ||
@@ -57,13 +57,13 @@ export default packageExport;" | ||
}); | ||
it('should create an ESM stub for CommonJS string export', () => { | ||
expect(createESMStub(path.join(testScenariosPath, 'string-export.cjs'), stubPath)).toMatchInlineSnapshot(`"export default packageExport;"`); | ||
it('should create an ESM stub for CommonJS string export', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'string-export.cjs'), stubPath)).toMatchInlineSnapshot(`"export default packageExport;"`); | ||
}); | ||
it("should throw when the code can't be parsed", () => { | ||
expect(() => createESMStub(path.join(testScenariosPath, 'throws-error.cjs'), stubPath)).toThrow(); | ||
it("should throw when the code can't be parsed", async () => { | ||
expect(() => createESMStub(path.join(testScenariosPath, 'throws-error.cjs'), stubPath)).rejects.toBeCalled(); | ||
}); | ||
it('can handle a CommonJS module with no exports', () => { | ||
expect(createESMStub(path.join(testScenariosPath, 'no-exports.cjs'), stubPath)).toMatchInlineSnapshot(`"import \\"./no-exports.cjs\\";"`); | ||
it('can handle a CommonJS module with no exports', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'no-exports.cjs'), stubPath)).toMatchInlineSnapshot(`"import \\"./no-exports.cjs\\";"`); | ||
}); | ||
it('can snapshot react', () => { | ||
expect(createESMStub('react', stubPath)).toMatchInlineSnapshot(` | ||
it('can snapshot react', async () => { | ||
expect(await createESMStub('react', stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from \\"./../react\\"; | ||
@@ -134,4 +134,4 @@ const { | ||
}); | ||
it('can snapshot react-dom', () => { | ||
expect(createESMStub('react-dom', stubPath)).toMatchInlineSnapshot(` | ||
it('can snapshot react-dom', async () => { | ||
expect(await createESMStub('react-dom', stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from \\"./../react-dom\\"; | ||
@@ -168,4 +168,4 @@ const { | ||
}); | ||
it('can generate a stub for a browser-sensitive CommonJS module', () => { | ||
expect(createESMStub(path.join(testScenariosPath, 'browser-sensitive.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
it('can generate a stub for a browser-sensitive CommonJS module', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'browser-sensitive.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from \\"./browser-sensitive.cjs\\"; | ||
@@ -182,5 +182,6 @@ const { | ||
}); | ||
it('can ignore JavaScript keywords in named exports', () => { | ||
it('can ignore JavaScript keywords in named exports', async () => { | ||
// Should be missing "delete" in the named exports. | ||
expect(createESMStub(path.join(testScenariosPath, 'export-with-keyword.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
expect(await createESMStub(path.join(testScenariosPath, 'export-with-keyword.cjs'), stubPath)) | ||
.toMatchInlineSnapshot(` | ||
"import packageExport from \\"./export-with-keyword.cjs\\"; | ||
@@ -198,3 +199,21 @@ const { | ||
}); | ||
it('can generate a stub for node-fetch', async () => { | ||
expect(await createESMStub(path.join(testScenariosPath, 'node-fetch.cjs'), stubPath)).toMatchInlineSnapshot(` | ||
"import packageExport from \\"./node-fetch.cjs\\"; | ||
const { | ||
default: __moduleDefault, | ||
Headers, | ||
Request, | ||
Response, | ||
} = packageExport; | ||
export { | ||
Headers, | ||
Request, | ||
Response, | ||
__moduleDefault as default, | ||
}" | ||
`); | ||
}); | ||
}); | ||
//# sourceMappingURL=createESMStub.test.js.map |
@@ -0,1 +1,2 @@ | ||
let hasInitialized = false; | ||
/** | ||
@@ -5,44 +6,50 @@ * Initializes a browser environment (e.g. by setting up the global `window` in a node environment.) | ||
export async function initializeBrowserEnvironment() { | ||
if (typeof window === 'undefined' && typeof global !== 'undefined') { | ||
const { JSDOM } = await import('jsdom'); | ||
const btoa = await import('btoa'); | ||
const atob = await import('atob'); | ||
await import('jsdom-global'); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import('regenerator-runtime'); | ||
const dom = new JSDOM(); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const globalThis = global; | ||
globalThis.window = dom.window; | ||
globalThis.window.requestAnimationFrame = dom.window.setTimeout; | ||
globalThis.window.cancelAnimationFrame = dom.window.clearTimeout; | ||
globalThis.btoa = btoa; | ||
globalThis.atob = atob; | ||
globalThis.self = dom.window; | ||
globalThis.document = dom.window.document; | ||
globalThis.navigator = dom.window.navigator; | ||
// eslint-disable-next-line etc/no-deprecated | ||
const createElement = dom.window.document.createElement.bind(dom.window.document); | ||
// Mock out canvas. | ||
globalThis.document.createElement = | ||
globalThis.window.createElement = | ||
globalThis.createElement = | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(tagName, ...args) => { | ||
if (tagName === 'canvas') { | ||
return { | ||
getContext: () => ({ | ||
fillRect: () => { | ||
/* no-op */ | ||
}, | ||
}), | ||
measureText: () => ({}), | ||
}; | ||
} | ||
if (!hasInitialized) { | ||
hasInitialized = true; | ||
if (typeof window === 'undefined' && typeof global !== 'undefined') { | ||
const { JSDOM } = await import('jsdom'); | ||
const btoa = await import('btoa'); | ||
const atob = await import('atob'); | ||
await import('jsdom-global'); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import('regenerator-runtime'); | ||
const dom = new JSDOM(); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const globalThis = global; | ||
globalThis.window = dom.window; | ||
globalThis.window.requestAnimationFrame = dom.window.setTimeout; | ||
globalThis.window.cancelAnimationFrame = dom.window.clearTimeout; | ||
globalThis.btoa = btoa; | ||
globalThis.atob = atob; | ||
globalThis.self = dom.window; | ||
globalThis.document = dom.window.document; | ||
globalThis.navigator = dom.window.navigator; | ||
// eslint-disable-next-line etc/no-deprecated | ||
const createElement = dom.window.document.createElement.bind(dom.window.document); | ||
// Mock out canvas. | ||
globalThis.document.createElement = | ||
globalThis.window.createElement = | ||
globalThis.createElement = | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return createElement.apply(dom.window.document, [tagName, ...args]); | ||
}; | ||
(tagName, ...args) => { | ||
if (tagName === 'canvas') { | ||
return { | ||
getContext: () => ({ | ||
fillRect: () => { | ||
/* no-op */ | ||
}, | ||
}), | ||
measureText: () => ({}), | ||
}; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return createElement.apply(dom.window.document, [tagName, ...args]); | ||
}; | ||
globalThis.window.fetch = async () => { | ||
/* no-op */ | ||
}; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=initializeBrowserEnvironment.js.map |
import fs from 'fs'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
import { createESMStub } from './createESMStub.js'; | ||
import { slash } from '@ms-cloudpack/path-utilities'; | ||
const { writeFile, realpath } = fs.promises; | ||
import { findPackageRoot, slash } from '@ms-cloudpack/path-utilities'; | ||
const { writeFile, mkdir } = fs.promises; | ||
/** | ||
@@ -14,5 +13,12 @@ * Generates an ESM stub for CommonJS modules. | ||
// Ensure stub path is provided. | ||
stubPath ?? (stubPath = path.join(await realpath(os.tmpdir()), `esm-stub-${Date.now()}.js`)); | ||
if (!stubPath) { | ||
const rootPath = findPackageRoot(entryPath); | ||
const extension = path.extname(entryPath); | ||
const stubFilename = path.basename(entryPath, extension) + '-stub.js'; | ||
const stubFolderPath = path.join(rootPath, 'node_modules/.cache/cloudpack-stubs/'); | ||
await mkdir(stubFolderPath, { recursive: true }); | ||
stubPath = path.join(stubFolderPath, stubFilename); | ||
} | ||
// If we have a package export, we can generate an ESM stub. | ||
const esmStub = createESMStub(entryPath, stubPath); | ||
const esmStub = await createESMStub(entryPath, stubPath); | ||
// Write the stub to disk. | ||
@@ -19,0 +25,0 @@ await writeFile(stubPath, esmStub); |
{ | ||
"name": "@ms-cloudpack/esm-stub-utilities", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Generates ESM stubs for CommonJS entry files.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
34940
613