@ms-cloudpack/esm-stub-utilities 0.15.30 → 0.15.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,6 +18,29 @@ const esmStub = await writeESMStubsInWorker({
18
18
  });
19
19
  ```
20
20
 
21
+ ## What is an ESM stub?
22
+
23
+ "ESM stubs" are a workaround for issues presented by CommonJS modules for Cloudpack's ESM library mode approach. The biggest problem is that CJS has many possible ways to declare exports, some of which are nontrivial or even impossible to find through static analysis. This isn't a problem in a monolithic bundle, but for an ESM bundle in library mode, all the export names must be declared in the bundle output so they can be imported by name in consuming packages. Some bundlers may attempt to convert common patterns to named exports, but others will just provide a default export.
24
+
25
+ The utilities in this package are used to run the package code, detect the actual exported names, and create an ES module format stub file with the proper exports (which can then be used as the bundler entry point). For example:
26
+
27
+ ```js
28
+ // Original file: index.js
29
+ module.exports.default = { value: 'I am the default export' };
30
+ // Example of a pattern that can't be statically analyzed (suppose it returns "a")
31
+ module.exports[computePropertyName()] = 'why';
32
+ module.exports.b = 'b';
33
+
34
+ // Generated stub file: index-stub.mjs
35
+ import moduleExport from './index.js';
36
+ const { a, b } = moduleExport;
37
+ const defaultExport = moduleExport?.default?.default ?? moduleExport?.default;
38
+ export default defaultExport;
39
+ export { a, b };
40
+ ```
41
+
42
+ Currently, our primary approach for detecting export names is to run the code in a worker thread, with a browser-like environment provided for packages that need it. This has multiple significant downsides and we're actively considering other options ([tracking issue](https://github.com/microsoft/cloudpack/issues/2548)), but running the code is the only way to approach 100% accuracy.
43
+
21
44
  ## Special considerations
22
45
 
23
46
  When evaluating named entries in the `exports` of the cjs file, the library is loaded in the Node process. A few libraries are used to simulate the browser environment in order for the script to load. (e.g. some libraries will reference `window` on load, and therefore must be run in an environment that accommodates this.)
@@ -23,8 +23,8 @@ export async function initBrowserEnvironment() {
23
23
  // this requires a workaround when adding a fake script tag at the bottom of this file.)
24
24
  enableJavaScriptEvaluation: false,
25
25
  // Disable the warning in case any of the node_modules code uses eval-like constructs.
26
- // https://github.com/capricorn86/happy-dom/wiki/Code-Generation-From-Strings-Warning
27
- suppressCodeGenerationFromStringsWarning: true,
26
+ // https://github.com/capricorn86/happy-dom/wiki/JavaScript-Evaluation-Warning
27
+ suppressInsecureJavaScriptEnvironmentWarning: true,
28
28
  fetch: {
29
29
  interceptor: {
30
30
  // Mock requests to not fail returning a response.
@@ -1 +1 @@
1
- {"version":3,"file":"initBrowserEnvironment.js","sourceRoot":"","sources":["../../src/worker/initBrowserEnvironment.js"],"names":[],"mappings":"AAAA,EAAE;AACF,+EAA+E;AAC/E,qDAAqD;AACrD,EAAE;AAEF,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAElE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB;IAC1C,MAAM,CAAC,WAAW,KAAK,gBAAgB,CAAC,CAAC,WAAW,CAAC,CAAC;IACtD,MAAM,CAAC,WAAW,KAAK,IAAI,CAAC,WAAW,CAAC;IACxC,MAAM,CAAC,WAAW,KAAK,gBAAgB,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAE3D,MAAM,GAAG,GAAG,kBAAkB,CAAC;IAC/B,iBAAiB,CAAC,QAAQ,CAAC;QACzB,GAAG;QACH,QAAQ,EAAE;YACR,4FAA4F;YAC5F,0FAA0F;YAC1F,2FAA2F;YAC3F,4FAA4F;YAC5F,wFAAwF;YACxF,0BAA0B,EAAE,KAAK;YACjC,sFAAsF;YACtF,qFAAqF;YACrF,wCAAwC,EAAE,IAAI;YAC9C,KAAK,EAAE;gBACL,WAAW,EAAE;oBACX,kDAAkD;oBAClD,kBAAkB,CAAC,EAAE,MAAM,EAAE;wBAC3B,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,sB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AgF;IAChF,OAAO,CAAC,GAAG,CAAC,YAAY,GAAG,GAAG,CAAC;IAE/B,mFAAmF;IACnF,gBAAgB,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,GAAG,IAAI,CAAC;IAE1C,8EAA8E;IAC9E,MAAM,SAAS,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IAC1C,SAAS,CAAC,GAAG,GAAG,GAAG,GAAG,UAAU,CAAC;IACjC,oEAAoE;IACpE,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC;IACnC,OAAO,CAAC,KAAK,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;IACzB,6DAA6D;IAC7D,MAAM,aAAa,GAAG,SAAS,CAAC,aAAa,CAAC;IAC9C,SAAS,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;IACrC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IACrC,OAAO,CAAC,KAAK,GAAG,YAAY,CAAC;IAC7B,SAAS,CAAC,aAAa,GAAG,aAAa,CAAC;AAC1C,CAAC","sourcesContent":["//\n// This file is JS because it's also used in a test worker within this package.\n// See verifyStub/loadStubWorker.js for more details.\n//\n\nimport { performance } from 'perf_hooks';\nimport util from 'util';\nimport { GlobalRegistrator } from '@happy-dom/global-registrator';\n\n/**\n * Initialize a fake browser environment.\n */\nexport async function initBrowserEnvironment() {\n global.performance ??= /** @type {*} */ (performance);\n global.TextEncoder ??= util.TextEncoder;\n global.TextDecoder ??= /** @type {*} */ (util.TextDecoder);\n\n const url = 'http://localhost';\n GlobalRegistrator.register({\n url,\n settings: {\n // As of happy-dom v20, JS script tag evaluation is disabled by default to prevent arbitrary\n // code execution from eval strings embedded in a webpage. This is not really relevant for\n // our use case of running node_modules code in a browser-like environment (loaded directly\n // not with script tags), but it shouldn't hurt to leave it disabled. (The only exception is\n // this requires a workaround when adding a fake script tag at the bottom of this file.)\n enableJavaScriptEvaluation: false,\n // Disable the warning in case any of the node_modules code uses eval-like constructs.\n // https://github.com/capricorn86/happy-dom/wiki/Code-Generation-From-Strings-Warning\n suppressCodeGenerationFromStringsWarning: true,\n fetch: {\n interceptor: {\n // Mock requests to not fail returning a response.\n beforeAsyncRequest({ window }) {\n return Promise.resolve(new window.Response('async fetch response'));\n },\n beforeSyncRequest({ window }) {\n return {\n status: 0,\n statusText: '',\n ok: true,\n url: '',\n redirected: false,\n headers: new window.Headers(),\n body: Buffer.from(''),\n };\n },\n },\n },\n },\n });\n\n await import('regenerator-runtime');\n\n // Ensure assigning to and reading from `window.foo` globals works as expected by copying all the\n // properties from the jsdom `window` to node's `global`, then assigning `global` to `window`.\n Object.entries(Object.getOwnPropertyDescriptors(window)).forEach(([k, desc]) => {\n // Skip storage to avoid errors \"localStorage is not available for opaque origins\"\n // (these errors could in theory be resolved by passing a URL to jsdom-global setup, but that\n // triggers some weird issues with whatwg-url using TextDecoder in a way that works in other\n // environments but throws in the sandbox)\n if (!(k in global) && !['localStorage', 'sessionStorage'].includes(k)) {\n Object.defineProperty(global, k, desc);\n }\n });\n /** @type {*} */ (global).window ??= global;\n /** @type {*} */ (global).self ??= global;\n /** @type {*} */ (document).window ??= global.window;\n\n // Don't allow setting timeouts or intervals\n global.setTimeout = /** @type {*} */ (() => 0);\n global.setInterval = /** @type {*} */ (() => 0);\n global.setImmediate = /** @type {*} */ (global.setTimeout);\n global.requestAnimationFrame = global.setTimeout;\n global.cancelAnimationFrame = global.clearTimeout;\n // Also don't allow process.nextTick in case of Node code\n process.nextTick = () => {};\n\n global.btoa = (str) => Buffer.from(str, 'binary').toString('base64');\n global.atob = (str) => Buffer.from(str, 'base64').toString('binary');\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n URL.createObjectURL ??= () => 'blob:http://fake';\n // eslint-disable-next-line @typescript-eslint/unbound-method\n URL.revokeObjectURL ??= () => {};\n\n // Simulate WebSocket existence. (Needed for isomorphic-ws)\n global.WebSocket = /** @type {*} */ (class WebSocket {});\n\n // Fake MessageChannel so scheduler won't hang the process by opening a channel.\n global.MessageChannel = class MessageChannel {\n port1 = /** @type {*} */ ({});\n port2 = /** @type {*} */ ({});\n };\n global.BroadcastChannel = /** @type {*} */ (class BroadcastChannel {});\n\n // Prevent adding event listeners.\n global.addEventListener = document.addEventListener = () => {};\n global.removeEventListener = document.removeEventListener = () => {};\n global.dispatchEvent = document.dispatchEvent = () => false;\n\n // Mock out canvas.\n // eslint-disable-next-line etc/no-deprecated\n const createElement = window.document.createElement.bind(window.document);\n const createElementMock = (/** @type {string} */ tagName, /** @type {ElementCreationOptions} */ options) => {\n if (tagName === 'canvas') {\n return /** @type {*} */ ({\n getContext: () => ({ fillRect: () => {} }),\n measureText: () => ({}),\n });\n }\n\n return createElement(tagName, options);\n };\n // eslint-disable-next-line etc/no-deprecated\n global.document.createElement =\n /** @type {*} */ (window).createElement =\n /** @type {*} */ (global).createElement =\n createElementMock;\n\n global.Path2D ??= /** @type {*} */ (class Path2D {});\n\n global.fetch = /** @type {*} */ (async () => {});\n global.XMLHttpRequest = /** @type {*} */ (class XMLHttpRequest {});\n\n // prevent 'debug' package from accessing process.stderr\n // eslint-disable-next-line no-restricted-properties -- setting a property is ok\n process.env.DEBUG_COLORS = '0';\n\n // Some packages like readable-stream@2.3.7 read process.browser to avoid node code\n /** @type {*} */ (process).browser = true;\n\n // Add a script tag so that webpack automatic publicPath detection won't throw\n const scriptTag = createElement('script');\n scriptTag.src = `${url}/fake.js`;\n // Workaround for having script tag evaluation disabled in happy-dom\n const consoleError = console.error;\n console.error = () => {};\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const dispatchEvent = scriptTag.dispatchEvent;\n scriptTag.dispatchEvent = () => true;\n document.body.appendChild(scriptTag);\n console.error = consoleError;\n scriptTag.dispatchEvent = dispatchEvent;\n}\n"]}
1
+ {"version":3,"file":"initBrowserEnvironment.js","sourceRoot":"","sources":["../../src/worker/initBrowserEnvironment.js"],"names":[],"mappings":"AAAA,EAAE;AACF,+EAA+E;AA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AAC;IACzB,6DAA6D;IAC7D,MAAM,aAAa,GAAG,SAAS,CAAC,aAAa,CAAC;IAC9C,SAAS,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;IACrC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IACrC,OAAO,CAAC,KAAK,GAAG,YAAY,CAAC;IAC7B,SAAS,CAAC,aAAa,GAAG,aAAa,CAAC;AAC1C,CAAC","sourcesContent":["//\n// This file is JS because it's also used in a test worker within this package.\n// See verifyStub/loadStubWorker.js for more details.\n//\n\nimport { performance } from 'perf_hooks';\nimport util from 'util';\nimport { GlobalRegistrator } from '@happy-dom/global-registrator';\n\n/**\n * Initialize a fake browser environment.\n */\nexport async function initBrowserEnvironment() {\n global.performance ??= /** @type {*} */ (performance);\n global.TextEncoder ??= util.TextEncoder;\n global.TextDecoder ??= /** @type {*} */ (util.TextDecoder);\n\n const url = 'http://localhost';\n GlobalRegistrator.register({\n url,\n settings: {\n // As of happy-dom v20, JS script tag evaluation is disabled by default to prevent arbitrary\n // code execution from eval strings embedded in a webpage. This is not really relevant for\n // our use case of running node_modules code in a browser-like environment (loaded directly\n // not with script tags), but it shouldn't hurt to leave it disabled. (The only exception is\n // this requires a workaround when adding a fake script tag at the bottom of this file.)\n enableJavaScriptEvaluation: false,\n // Disable the warning in case any of the node_modules code uses eval-like constructs.\n // https://github.com/capricorn86/happy-dom/wiki/JavaScript-Evaluation-Warning\n suppressInsecureJavaScriptEnvironmentWarning: true,\n fetch: {\n interceptor: {\n // Mock requests to not fail returning a response.\n beforeAsyncRequest({ window }) {\n return Promise.resolve(new window.Response('async fetch response'));\n },\n beforeSyncRequest({ window }) {\n return {\n status: 0,\n statusText: '',\n ok: true,\n url: '',\n redirected: false,\n headers: new window.Headers(),\n body: Buffer.from(''),\n };\n },\n },\n },\n },\n });\n\n await import('regenerator-runtime');\n\n // Ensure assigning to and reading from `window.foo` globals works as expected by copying all the\n // properties from the jsdom `window` to node's `global`, then assigning `global` to `window`.\n Object.entries(Object.getOwnPropertyDescriptors(window)).forEach(([k, desc]) => {\n // Skip storage to avoid errors \"localStorage is not available for opaque origins\"\n // (these errors could in theory be resolved by passing a URL to jsdom-global setup, but that\n // triggers some weird issues with whatwg-url using TextDecoder in a way that works in other\n // environments but throws in the sandbox)\n if (!(k in global) && !['localStorage', 'sessionStorage'].includes(k)) {\n Object.defineProperty(global, k, desc);\n }\n });\n /** @type {*} */ (global).window ??= global;\n /** @type {*} */ (global).self ??= global;\n /** @type {*} */ (document).window ??= global.window;\n\n // Don't allow setting timeouts or intervals\n global.setTimeout = /** @type {*} */ (() => 0);\n global.setInterval = /** @type {*} */ (() => 0);\n global.setImmediate = /** @type {*} */ (global.setTimeout);\n global.requestAnimationFrame = global.setTimeout;\n global.cancelAnimationFrame = global.clearTimeout;\n // Also don't allow process.nextTick in case of Node code\n process.nextTick = () => {};\n\n global.btoa = (str) => Buffer.from(str, 'binary').toString('base64');\n global.atob = (str) => Buffer.from(str, 'base64').toString('binary');\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n URL.createObjectURL ??= () => 'blob:http://fake';\n // eslint-disable-next-line @typescript-eslint/unbound-method\n URL.revokeObjectURL ??= () => {};\n\n // Simulate WebSocket existence. (Needed for isomorphic-ws)\n global.WebSocket = /** @type {*} */ (class WebSocket {});\n\n // Fake MessageChannel so scheduler won't hang the process by opening a channel.\n global.MessageChannel = class MessageChannel {\n port1 = /** @type {*} */ ({});\n port2 = /** @type {*} */ ({});\n };\n global.BroadcastChannel = /** @type {*} */ (class BroadcastChannel {});\n\n // Prevent adding event listeners.\n global.addEventListener = document.addEventListener = () => {};\n global.removeEventListener = document.removeEventListener = () => {};\n global.dispatchEvent = document.dispatchEvent = () => false;\n\n // Mock out canvas.\n // eslint-disable-next-line etc/no-deprecated\n const createElement = window.document.createElement.bind(window.document);\n const createElementMock = (/** @type {string} */ tagName, /** @type {ElementCreationOptions} */ options) => {\n if (tagName === 'canvas') {\n return /** @type {*} */ ({\n getContext: () => ({ fillRect: () => {} }),\n measureText: () => ({}),\n });\n }\n\n return createElement(tagName, options);\n };\n // eslint-disable-next-line etc/no-deprecated\n global.document.createElement =\n /** @type {*} */ (window).createElement =\n /** @type {*} */ (global).createElement =\n createElementMock;\n\n global.Path2D ??= /** @type {*} */ (class Path2D {});\n\n global.fetch = /** @type {*} */ (async () => {});\n global.XMLHttpRequest = /** @type {*} */ (class XMLHttpRequest {});\n\n // prevent 'debug' package from accessing process.stderr\n // eslint-disable-next-line no-restricted-properties -- setting a property is ok\n process.env.DEBUG_COLORS = '0';\n\n // Some packages like readable-stream@2.3.7 read process.browser to avoid node code\n /** @type {*} */ (process).browser = true;\n\n // Add a script tag so that webpack automatic publicPath detection won't throw\n const scriptTag = createElement('script');\n scriptTag.src = `${url}/fake.js`;\n // Workaround for having script tag evaluation disabled in happy-dom\n const consoleError = console.error;\n console.error = () => {};\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const dispatchEvent = scriptTag.dispatchEvent;\n scriptTag.dispatchEvent = () => true;\n document.body.appendChild(scriptTag);\n console.error = consoleError;\n scriptTag.dispatchEvent = dispatchEvent;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/esm-stub-utilities",
3
- "version": "0.15.30",
3
+ "version": "0.15.32",
4
4
  "description": "Generates ESM stubs for CommonJS entry files.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,8 +13,18 @@
13
13
  "import": "./lib/index.js"
14
14
  }
15
15
  },
16
+ "scripts": {
17
+ "api": "cloudpack-scripts api",
18
+ "build:watch": "cloudpack-scripts build-watch",
19
+ "build": "cloudpack-scripts build",
20
+ "lint:update": "cloudpack-scripts lint-update",
21
+ "lint": "cloudpack-scripts lint",
22
+ "test:update": "cloudpack-scripts test-update",
23
+ "test:watch": "cloudpack-scripts test-watch",
24
+ "test": "cloudpack-scripts test"
25
+ },
16
26
  "dependencies": {
17
- "@happy-dom/global-registrator": "^20.0.0",
27
+ "@happy-dom/global-registrator": "^20.0.2",
18
28
  "@ms-cloudpack/common-types": "^0.32.2",
19
29
  "@ms-cloudpack/environment": "^0.1.1",
20
30
  "@ms-cloudpack/json-utilities": "^0.1.11",
@@ -30,16 +40,6 @@
30
40
  "@ms-cloudpack/test-utilities": "^0.5.0",
31
41
  "@types/regenerator-runtime": "^0.13.1"
32
42
  },
33
- "scripts": {
34
- "api": "cloudpack-scripts api",
35
- "build:watch": "cloudpack-scripts build-watch",
36
- "build": "cloudpack-scripts build",
37
- "lint:update": "cloudpack-scripts lint-update",
38
- "lint": "cloudpack-scripts lint",
39
- "test:update": "cloudpack-scripts test-update",
40
- "test:watch": "cloudpack-scripts test-watch",
41
- "test": "cloudpack-scripts test"
42
- },
43
43
  "files": [
44
44
  "lib/**/!(*.test.*)"
45
45
  ]