Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b12597

Browse files
[dev-tool] Added support for deep imports in samples (Azure#18511)
Co-authored-by: Will Temple <[email protected]>
1 parent be431bc commit 5b12597

File tree

1 file changed

+18
-3
lines changed
  • common/tools/dev-tool/src/commands/samples

1 file changed

+18
-3
lines changed

common/tools/dev-tool/src/commands/samples/publish.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function isDependency(moduleSpecifier: string): boolean {
122122

123123
// This seems like a reasonable test for "is a relative path" as long as
124124
// absolute path imports are forbidden.
125-
const isRelativePath = /^\.\.?\//.test(moduleSpecifier);
125+
const isRelativePath = /^\.\.?[\/\\]/.test(moduleSpecifier);
126126
return !isRelativePath;
127127
}
128128

@@ -280,6 +280,21 @@ async function collect<T>(i: AsyncIterableIterator<T>): Promise<T[]> {
280280
return out;
281281
}
282282

283+
/**
284+
* Processes a segmented module path to return the first segment. This is useful for packages that have nested imports
285+
* such as "dayjs/plugin/duration".
286+
*
287+
* @param specifier - the module specifier to resolve to a package name
288+
* @returns a package name
289+
*/
290+
function resolveModule(specifier: string): string {
291+
const parts = specifier.split("/", 2);
292+
293+
// The first part could be a namespace, in which case we need to join them
294+
if (parts.length > 1 && parts[0].startsWith("@")) return parts[0] + "/" + parts[1];
295+
else return parts[0];
296+
}
297+
283298
/**
284299
* Extracts the sample generation metainformation from the sample sources and
285300
* configuration in package.json.
@@ -346,7 +361,7 @@ async function makeSampleGenerationInfo(
346361
.slice(-1)[0]
347362
.replace("\\", "/"),
348363
// This'll be good enough most of the time, but products like Azure Form Recognizer will have
349-
// too adjust using the sample configuration.
364+
// to adjust using the sample configuration.
350365
productName:
351366
sampleConfiguration.productName ??
352367
fail(`The sample configuration does not specify a "productName".`),
@@ -377,7 +392,7 @@ async function makeSampleGenerationInfo(
377392
return {
378393
dependencies: moduleInfos.reduce((prev, source) => {
379394
const current: Record<string, string> = {};
380-
for (const dependency of source.importedModules) {
395+
for (const dependency of source.importedModules.map(resolveModule)) {
381396
if (prev[dependency] === undefined) {
382397
const dependencyVersion =
383398
sampleConfiguration.dependencyOverrides?.[dependency] ??

0 commit comments

Comments
 (0)