Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-rest-pipeline] Add conditional exports #22804

Merged
merged 19 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ee27d04
Conditional export for core-rest-pipeline
jeremymeng Jun 13, 2022
1c0c4fb
Add an option to dev-tool bundle command for .cjs output extension
jeremymeng Aug 1, 2022
db7e7ef
import "*" => import "*.js"
jeremymeng Aug 1, 2022
aa393d5
bump mocha version to ^10.0.0
jeremymeng Aug 2, 2022
31632f4
- import * as => import
jeremymeng Aug 4, 2022
db5ed1f
Merge remote-tracking branch 'up/main' into core/cond-export
jeremymeng Aug 4, 2022
d9fe9ee
- Fix linter rule to allow `"main": "dist/index.cjs"`
jeremymeng Aug 4, 2022
d874bc1
revert back to awesome example.com
jeremymeng Aug 4, 2022
65524e6
Fix types export
jeremymeng Aug 4, 2022
76a44b5
Merge remote-tracking branch 'up/main' into core/cond-export
jeremymeng Aug 4, 2022
5231081
Bump minor version and update CHANGELOG
jeremymeng Aug 4, 2022
279160c
Merge remote-tracking branch 'up/main' into core/cond-export
jeremymeng Aug 5, 2022
00d9c25
Merge remote-tracking branch 'up/main' into core/cond-export
jeremymeng Aug 29, 2022
6bcb49c
Merge remote-tracking branch 'up/main' into core/cond-export
jeremymeng Oct 5, 2022
10f3fa6
Add copy of our types shim for CommonJS conditional export
jeremymeng Oct 6, 2022
5bb5b13
remove `output-cjs-ext` option
jeremymeng Oct 17, 2022
ccc1365
Merge remote-tracking branch 'upstream/main' into core/cond-export
jeremymeng Oct 17, 2022
57705cf
delete usage of removed dev-tool bundle option
jeremymeng Oct 20, 2022
df32ab1
Merge remote-tracking branch 'up/main' into core/cond-export
jeremymeng Oct 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion common/tools/dev-tool/src/commands/run/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const commandInfo = makeCommandInfo(
default: true,
description: "include a polyfill for Node.js builtin modules",
},
"output-cjs-ext": {
kind: "boolean",
default: false,
description: "use .cjs extension for bundle output. default is .js extension",
},
}
);

Expand Down Expand Up @@ -72,7 +77,7 @@ export default leafCommand(commandInfo, async (options) => {
const bundle = await rollup.rollup(baseConfig);

await bundle.write({
file: "dist/index.js",
file: `dist/index.${options["output-cjs-ext"] ? "cjs" : "js"}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using an option, could we just read the main path from package.json?

Suggested change
file: `dist/index.${options["output-cjs-ext"] ? "cjs" : "js"}`,
file: info.packageJson.main

It may be best to defensively check that this field is set and starts with dist just to make sure we don't blow up someone's machine from a nasty config.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point! I changed to use the main path. We already have the linter to ensure the main path so I just did a minimal check for non-falsy values.

format: "cjs",
sourcemap: true,
exports: "named",
Expand Down
32 changes: 23 additions & 9 deletions sdk/core/core-rest-pipeline/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
"version": "1.9.1",
"description": "Isomorphic client library for making HTTP requests in node.js and browser.",
"sdk-type": "client",
"main": "dist/index.js",
"main": "dist/index.cjs",
"module": "dist-esm/src/index.js",
"type": "module",
"export": {
".": {
"types": "./types/src/index.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this still be something like

Suggested change
"types": "./types/src/index.js",
"types": "./types/latest/src/index.d.ts",

Though really shouldn't this match the main types entry so core-rest-pipeline.shims.d.ts ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from Matt's PR. Will fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpodwysocki the "types" export in notification hub package.json seem incorrect?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They resolve just fine in TypeScript as is. I got the advice from @DanielRosenwasser and it works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpodwysocki does TS not need this to resolve to a d.ts then? I guess I'm confused how the resolution works, lmk if you can point me at some docs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually nest conditions arbitrarily deep, and use arrays of fallbacks, rather than just simple strings! They're quite complicated! Anyways, it would look something like

"exports": {
  ".": {
    "import": {
        "types": "./types/index.d.ts",
        "default": "./dist/index.js"
    },
    "require": {
        "types": "./types/index.d.cts",
        "default": "./dist/index.cjs"
    }
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! 😮

Thanks for taking the time to explain, hopefully once we find the right config we can tool it such that humans don't have to think too hard about this stuff. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be two .d.ts files. One for the esm module, one for the cjs module. The esm module can (and probably should) just re-export stuff from the cjs one if it helps.

@weswigham thanks for the detail information! For our packages we used a rollup d.ts for the default types field. From my testing it worked when both require and import use the same d.ts file. Is it necessary to have two separate files?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, from the linked discussion microsoft/TypeScript#50466 we would need two separate files so that the correct mode is applied.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this PR to have two copies of the same shim type file, d.ts for import, and d.cts for require. I tested the package on two projects, an ESM one with "type": "module", and a CJS one without "type": "module". Result of VS Code IntelliSense/Go to Definition work the same as before so I assume this should be good.

"require": "./dist/index.cjs",
"import": "./dist-esm/src/index.js"
}
},
"browser": {
"./dist-esm/src/defaultHttpClient.js": "./dist-esm/src/defaultHttpClient.browser.js",
"./dist-esm/src/policies/decompressResponsePolicy.js": "./dist-esm/src/policies/decompressResponsePolicy.browser.js",
Expand All @@ -14,7 +22,7 @@
"./dist-esm/src/util/userAgentPlatform.js": "./dist-esm/src/util/userAgentPlatform.browser.js"
},
"react-native": {
"./dist/index.js": "./dist-esm/src/index.js",
"./dist/index.cjs": "./dist-esm/src/index.js",
"./dist-esm/src/defaultHttpClient.js": "./dist-esm/src/defaultHttpClient.native.js",
"./dist-esm/src/util/userAgentPlatform.js": "./dist-esm/src/util/userAgentPlatform.native.js"
},
Expand All @@ -29,9 +37,9 @@
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:samples": "echo Obsolete",
"build:test": "tsc -p . && dev-tool run bundle",
"build:test": "tsc -p . && dev-tool run bundle --output-cjs-ext=true",
"build:types": "downlevel-dts types/latest/ types/3.1/",
"build": "npm run clean && tsc -p . && dev-tool run bundle && api-extractor run --local && npm run build:types",
"build": "npm run clean && tsc -p . && dev-tool run bundle --output-cjs-ext=true && api-extractor run --local && npm run build:types",
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-* temp types *.tgz *.log",
"execute:samples": "echo skipped",
Expand All @@ -45,15 +53,17 @@
"pack": "npm pack 2>&1",
"test:browser": "npm run clean && npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
"test:node": "npm run clean && tsc -p . && npm run unit-test:node && npm run integration-test:node",
"test": "npm run clean && tsc -p . && npm run unit-test:node && dev-tool run bundle && npm run unit-test:browser && npm run integration-test",
"unit-test:browser": "karma start --single-run",
"unit-test:node": "mocha -r esm -r ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace --exclude \"test/**/browser/*.spec.ts\" \"test/**/*.spec.ts\"",
"test": "npm run clean && tsc -p . && npm run unit-test:node && dev-tool run bundle --output-cjs-ext=true && npm run unit-test:browser && npm run integration-test",
"unit-test:browser": "karma start karma.conf.cjs --single-run",
"unit-test:node": "mocha --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace --exclude \"test/**/browser/*.spec.ts\" \"test/**/*.spec.ts\"",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
},
"files": [
"dist/",
"dist-esm/src/",
"types/3.1/src/",
"types/3.1/core-rest-pipeline.d.ts",
"types/latest/src/",
"types/latest/core-rest-pipeline.d.ts",
"core-rest-pipeline.shims.d.ts",
"core-rest-pipeline.shims-3_1.d.ts",
Expand Down Expand Up @@ -125,15 +135,19 @@
"karma-sourcemap-loader": "^0.3.8",
"karma": "^6.3.0",
"mocha-junit-reporter": "^2.0.0",
"mocha": "^7.1.1",
"mocha": "^10.0.0",
"prettier": "^2.5.1",
"puppeteer": "^14.0.0",
"rimraf": "^3.0.0",
"sinon": "^9.0.2",
"source-map-support": "^0.5.9",
"typescript": "~4.6.0",
"ts-node": "^10.0.0",
"typescript": "~4.7.0",
"util": "^0.12.1"
},
"mocha": {
"loader": "ts-node/esm"
},
"//sampleConfiguration": {
"skipFolder": true,
"disableDocsMs": true,
Expand Down
26 changes: 13 additions & 13 deletions sdk/core/core-rest-pipeline/src/createPipelineFromOptions.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { LogPolicyOptions, logPolicy } from "./policies/logPolicy";
import { Pipeline, createEmptyPipeline } from "./pipeline";
import { PipelineRetryOptions, TlsSettings } from "./interfaces";
import { RedirectPolicyOptions, redirectPolicy } from "./policies/redirectPolicy";
import { UserAgentPolicyOptions, userAgentPolicy } from "./policies/userAgentPolicy";
import { LogPolicyOptions, logPolicy } from "./policies/logPolicy.js";
import { Pipeline, createEmptyPipeline } from "./pipeline.js";
import { PipelineRetryOptions, TlsSettings } from "./interfaces.js";
import { RedirectPolicyOptions, redirectPolicy } from "./policies/redirectPolicy.js";
import { UserAgentPolicyOptions, userAgentPolicy } from "./policies/userAgentPolicy.js";

import { ProxySettings } from ".";
import { decompressResponsePolicy } from "./policies/decompressResponsePolicy";
import { defaultRetryPolicy } from "./policies/defaultRetryPolicy";
import { formDataPolicy } from "./policies/formDataPolicy";
import { ProxySettings } from "./interfaces.js";
import { decompressResponsePolicy } from "./policies/decompressResponsePolicy.js";
import { defaultRetryPolicy } from "./policies/defaultRetryPolicy.js";
import { formDataPolicy } from "./policies/formDataPolicy.js";
import { isNode } from "@azure/core-util";
import { proxyPolicy } from "./policies/proxyPolicy";
import { setClientRequestIdPolicy } from "./policies/setClientRequestIdPolicy";
import { tlsPolicy } from "./policies/tlsPolicy";
import { tracingPolicy } from "./policies/tracingPolicy";
import { proxyPolicy } from "./policies/proxyPolicy.js";
import { setClientRequestIdPolicy } from "./policies/setClientRequestIdPolicy.js";
import { tlsPolicy } from "./policies/tlsPolicy.js";
import { tracingPolicy } from "./policies/tracingPolicy.js";

/**
* Defines options that are used to configure the HTTP pipeline for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpClient } from "./interfaces";
import { createFetchHttpClient } from "./fetchHttpClient";
import { HttpClient } from "./interfaces.js";
import { createFetchHttpClient } from "./fetchHttpClient.js";

/**
* Create the correct HttpClient for the current environment.
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/core-rest-pipeline/src/defaultHttpClient.native.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpClient } from "./interfaces";
import { createXhrHttpClient } from "./xhrHttpClient";
import { HttpClient } from "./interfaces.js";
import { createXhrHttpClient } from "./xhrHttpClient.js";

/**
* Create the correct HttpClient for the current environment.
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/core-rest-pipeline/src/defaultHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpClient } from "./interfaces";
import { createNodeHttpClient } from "./nodeHttpClient";
import { HttpClient } from "./interfaces.js";
import { createNodeHttpClient } from "./nodeHttpClient.js";

/**
* Create the correct HttpClient for the current environment.
Expand Down
6 changes: 3 additions & 3 deletions sdk/core/core-rest-pipeline/src/fetchHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
PipelineRequest,
PipelineResponse,
TransferProgressEvent,
} from "./interfaces";
import { RestError } from "./restError";
import { createHttpHeaders } from "./httpHeaders";
} from "./interfaces.js";
import { RestError } from "./restError.js";
import { createHttpHeaders } from "./httpHeaders.js";

/**
* Checks if the body is a NodeReadable stream which is not supported in Browsers
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-rest-pipeline/src/httpHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpHeaders, RawHttpHeaders, RawHttpHeadersInput } from "./interfaces";
import { HttpHeaders, RawHttpHeaders, RawHttpHeadersInput } from "./interfaces.js";

interface HeaderEntry {
name: string;
Expand Down
56 changes: 32 additions & 24 deletions sdk/core/core-rest-pipeline/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,78 @@ export {
SendRequest,
TlsSettings,
TransferProgressEvent,
} from "./interfaces";
} from "./interfaces.js";
export {
AddPolicyOptions as AddPipelineOptions,
PipelinePhase,
PipelinePolicy,
Pipeline,
createEmptyPipeline,
} from "./pipeline";
} from "./pipeline.js";
export {
createPipelineFromOptions,
InternalPipelineOptions,
PipelineOptions,
} from "./createPipelineFromOptions";
export { createDefaultHttpClient } from "./defaultHttpClient";
export { createHttpHeaders } from "./httpHeaders";
export { createPipelineRequest, PipelineRequestOptions } from "./pipelineRequest";
export { RestError, RestErrorOptions, isRestError } from "./restError";
} from "./createPipelineFromOptions.js";
export { createDefaultHttpClient } from "./defaultHttpClient.js";
export { createHttpHeaders } from "./httpHeaders.js";
export { createPipelineRequest, PipelineRequestOptions } from "./pipelineRequest.js";
export { RestError, RestErrorOptions, isRestError } from "./restError.js";
export {
decompressResponsePolicy,
decompressResponsePolicyName,
} from "./policies/decompressResponsePolicy";
} from "./policies/decompressResponsePolicy.js";
export {
exponentialRetryPolicy,
ExponentialRetryPolicyOptions,
exponentialRetryPolicyName,
} from "./policies/exponentialRetryPolicy";
} from "./policies/exponentialRetryPolicy.js";
export {
setClientRequestIdPolicy,
setClientRequestIdPolicyName,
} from "./policies/setClientRequestIdPolicy";
export { logPolicy, logPolicyName, LogPolicyOptions } from "./policies/logPolicy";
export { proxyPolicy, proxyPolicyName, getDefaultProxySettings } from "./policies/proxyPolicy";
} from "./policies/setClientRequestIdPolicy.js";
export { logPolicy, logPolicyName, LogPolicyOptions } from "./policies/logPolicy.js";
export { proxyPolicy, proxyPolicyName, getDefaultProxySettings } from "./policies/proxyPolicy.js";
export {
redirectPolicy,
redirectPolicyName,
RedirectPolicyOptions,
} from "./policies/redirectPolicy";
} from "./policies/redirectPolicy.js";
export {
systemErrorRetryPolicy,
SystemErrorRetryPolicyOptions,
systemErrorRetryPolicyName,
} from "./policies/systemErrorRetryPolicy";
} from "./policies/systemErrorRetryPolicy.js";
export {
throttlingRetryPolicy,
throttlingRetryPolicyName,
ThrottlingRetryPolicyOptions,
} from "./policies/throttlingRetryPolicy";
export { retryPolicy, RetryPolicyOptions } from "./policies/retryPolicy";
export { RetryStrategy, RetryInformation, RetryModifiers } from "./retryStrategies/retryStrategy";
export { tracingPolicy, tracingPolicyName, TracingPolicyOptions } from "./policies/tracingPolicy";
export { defaultRetryPolicy, DefaultRetryPolicyOptions } from "./policies/defaultRetryPolicy";
} from "./policies/throttlingRetryPolicy.js";
export { retryPolicy, RetryPolicyOptions } from "./policies/retryPolicy.js";
export {
RetryStrategy,
RetryInformation,
RetryModifiers,
} from "./retryStrategies/retryStrategy.js";
export {
tracingPolicy,
tracingPolicyName,
TracingPolicyOptions,
} from "./policies/tracingPolicy.js";
export { defaultRetryPolicy, DefaultRetryPolicyOptions } from "./policies/defaultRetryPolicy.js";
export {
userAgentPolicy,
userAgentPolicyName,
UserAgentPolicyOptions,
} from "./policies/userAgentPolicy";
export { tlsPolicy, tlsPolicyName } from "./policies/tlsPolicy";
export { formDataPolicy, formDataPolicyName } from "./policies/formDataPolicy";
} from "./policies/userAgentPolicy.js";
export { tlsPolicy, tlsPolicyName } from "./policies/tlsPolicy.js";
export { formDataPolicy, formDataPolicyName } from "./policies/formDataPolicy.js";
export {
bearerTokenAuthenticationPolicy,
BearerTokenAuthenticationPolicyOptions,
bearerTokenAuthenticationPolicyName,
ChallengeCallbacks,
AuthorizeRequestOptions,
AuthorizeRequestOnChallengeOptions,
} from "./policies/bearerTokenAuthenticationPolicy";
export { ndJsonPolicy, ndJsonPolicyName } from "./policies/ndJsonPolicy";
} from "./policies/bearerTokenAuthenticationPolicy.js";
export { ndJsonPolicy, ndJsonPolicyName } from "./policies/ndJsonPolicy.js";
14 changes: 7 additions & 7 deletions sdk/core/core-rest-pipeline/src/nodeHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as http from "http";
import * as https from "https";
import * as zlib from "zlib";
import http from "http";
import https from "https";
import zlib from "zlib";
import { Transform } from "stream";
import { AbortController, AbortError } from "@azure/abort-controller";
import {
Expand All @@ -14,11 +14,11 @@ import {
RequestBodyType,
TlsSettings,
TransferProgressEvent,
} from "./interfaces";
import { createHttpHeaders } from "./httpHeaders";
import { RestError } from "./restError";
} from "./interfaces.js";
import { createHttpHeaders } from "./httpHeaders.js";
import { RestError } from "./restError.js";
import { IncomingMessage } from "http";
import { logger } from "./log";
import { logger } from "./log.js";

const DEFAULT_TLS_SETTINGS = {};

Expand Down
Loading