-
Notifications
You must be signed in to change notification settings - Fork 660
[rush-sdk]: add named export support for CommonJS compatibility #5539
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@microsoft/rush", | ||
| "comment": "Chore: add named exports to support for `@rushstack/rush-sdk` used via ESM named import.", | ||
| "type": "none" | ||
| } | ||
| ], | ||
| "packageName": "@microsoft/rush" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,10 @@ | |
| "name": "axios", | ||
| "allowedCategories": [ "libraries" ] | ||
| }, | ||
| { | ||
| "name": "cjs-module-lexer", | ||
| "allowedCategories": [ "libraries" ] | ||
| }, | ||
|
Comment on lines
+61
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should go in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Emmm. It's generated automatically. Do you mean that I need to manually move the config into
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. |
||
| { | ||
| "name": "dependency-path", | ||
| "allowedCategories": [ "libraries" ] | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
| { | ||
| "pnpmShrinkwrapHash": "32f13ef1f15898a4f614bf9897cc1d74d8fdf2dd", | ||
| "pnpmShrinkwrapHash": "402417ff6a3ef549c064c379cb9793ec4b4d64af", | ||
| "preferredVersionsHash": "550b4cee0bef4e97db6c6aad726df5149d20e7d9", | ||
| "packageJsonInjectedDependenciesHash": "cb59d652ae8cf04249e1fa557d15d2958128a5e8" | ||
| "packageJsonInjectedDependenciesHash": "248fe4df023dec4d802dbb3f8d3f90842fd458ed" | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
| { | ||
| "pnpmShrinkwrapHash": "02e03149d5bf0b6a4e8dd2df668ed5350b0506c5", | ||
| "pnpmShrinkwrapHash": "f91ada7a0ec37139e2f0cff3fb869d1f514ee628", | ||
| "preferredVersionsHash": "a9b67c38568259823f9cfb8270b31bf6d8470b27" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ | |
|
|
||
| import * as path from 'node:path'; | ||
|
|
||
| import { FileSystem, Import, Path } from '@rushstack/node-core-library'; | ||
| import { initSync, parse } from 'cjs-module-lexer'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're already generating types with named exports and ESM modules, so we shouldn't need to try to infer the names from the CJS.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does your suggestion involve reading
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take a look at https://www.npmjs.com/package/es-module-lexer and analyzing the ESM modules we already generate in rush-lib.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for using |
||
|
|
||
| import { Encoding, FileSystem, Import, Path } from '@rushstack/node-core-library'; | ||
|
|
||
| function generateLibFilesRecursively(options: { | ||
| parentSourcePath: string; | ||
|
|
@@ -14,6 +16,10 @@ function generateLibFilesRecursively(options: { | |
| for (const folderItem of FileSystem.readFolderItems(options.parentSourcePath)) { | ||
| const sourcePath: string = path.join(options.parentSourcePath, folderItem.name); | ||
| const targetPath: string = path.join(options.parentTargetPath, folderItem.name); | ||
| const commonjsPath: string = path.join( | ||
| options.parentSourcePath.replace('/rush-lib/lib', '/rush-lib/lib-commonjs'), | ||
| folderItem.name | ||
| ); | ||
|
|
||
| if (folderItem.isDirectory()) { | ||
| // create destination folder | ||
|
|
@@ -36,11 +42,17 @@ function generateLibFilesRecursively(options: { | |
| const shimPathLiteral: string = JSON.stringify(Path.convertToSlashes(shimPath)); | ||
| const srcImportPathLiteral: string = JSON.stringify(srcImportPath); | ||
|
|
||
| const sourceCode: string = FileSystem.readFile(commonjsPath, { encoding: Encoding.Utf8 }); | ||
| const exportedNames: string[] = extractNamedExports(sourceCode); | ||
| const namedExportsPlaceholder: string = exportedNames.length | ||
| ? `${exportedNames.map((name) => `exports.${name}`).join(' = ')} = undefined;\n\n` | ||
| : ''; | ||
|
|
||
| FileSystem.writeFile( | ||
| targetPath, | ||
| // Example: | ||
| // module.exports = require("../../../lib-shim/index")._rushSdk_loadInternalModule("logic/policy/GitEmailPolicy"); | ||
| `module.exports = require(${shimPathLiteral})._rushSdk_loadInternalModule(${srcImportPathLiteral});` | ||
| `${namedExportsPlaceholder}module.exports = require(${shimPathLiteral})._rushSdk_loadInternalModule(${srcImportPathLiteral});` | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -58,6 +70,7 @@ export async function runAsync(): Promise<void> { | |
| const stubsTargetPath: string = path.resolve(__dirname, '../lib'); | ||
| // eslint-disable-next-line no-console | ||
| console.log('generate-stubs: Generating stub files under: ' + stubsTargetPath); | ||
| initSync(); | ||
| generateLibFilesRecursively({ | ||
| parentSourcePath: path.join(rushLibFolder, 'lib'), | ||
| parentTargetPath: stubsTargetPath, | ||
|
|
@@ -67,3 +80,8 @@ export async function runAsync(): Promise<void> { | |
| // eslint-disable-next-line no-console | ||
| console.log('generate-stubs: Completed successfully.'); | ||
| } | ||
|
|
||
| export function extractNamedExports(source: string): string[] { | ||
| const { exports, reexports } = parse(source); | ||
| return [...exports, ...reexports].filter((d) => d !== '__esModule'); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import { Executable } from '@rushstack/node-core-library'; | ||
|
|
||
| describe('@rushstack/rush-sdk named exports check', () => { | ||
| it('Should import named exports correctly (lib-shim)', () => { | ||
| const result = Executable.spawnSync('node', [ | ||
| '-e', | ||
| ` | ||
| const { RushConfiguration } = await import('@rushstack/rush-sdk'); | ||
| console.log(typeof RushConfiguration.loadFromConfigurationFile); | ||
| ` | ||
| ]); | ||
| expect(result.stdout.trim()).toEqual('function'); | ||
| expect(result.status).toBe(0); | ||
| }); | ||
|
|
||
| it('Should import named exports correctly (lib)', () => { | ||
| const result = Executable.spawnSync('node', [ | ||
| '-e', | ||
| ` | ||
| const { RushConfiguration } = await import('@rushstack/rush-sdk/lib/api/RushConfiguration'); | ||
| console.log(typeof RushConfiguration.loadFromConfigurationFile); | ||
| ` | ||
| ]); | ||
| expect(result.stdout.trim()).toEqual('function'); | ||
| expect(result.status).toBe(0); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.