From 5b12597c27233f3aa0bef554589ae9b46add60e9 Mon Sep 17 00:00:00 2001 From: Will Temple Date: Thu, 4 Nov 2021 11:58:07 -0400 Subject: [PATCH] [dev-tool] Added support for deep imports in samples (#18511) Co-authored-by: Will Temple --- .../dev-tool/src/commands/samples/publish.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/common/tools/dev-tool/src/commands/samples/publish.ts b/common/tools/dev-tool/src/commands/samples/publish.ts index 9d99f5fc97f9..61ce7cd02819 100644 --- a/common/tools/dev-tool/src/commands/samples/publish.ts +++ b/common/tools/dev-tool/src/commands/samples/publish.ts @@ -122,7 +122,7 @@ function isDependency(moduleSpecifier: string): boolean { // This seems like a reasonable test for "is a relative path" as long as // absolute path imports are forbidden. - const isRelativePath = /^\.\.?\//.test(moduleSpecifier); + const isRelativePath = /^\.\.?[\/\\]/.test(moduleSpecifier); return !isRelativePath; } @@ -280,6 +280,21 @@ async function collect(i: AsyncIterableIterator): Promise { return out; } +/** + * Processes a segmented module path to return the first segment. This is useful for packages that have nested imports + * such as "dayjs/plugin/duration". + * + * @param specifier - the module specifier to resolve to a package name + * @returns a package name + */ +function resolveModule(specifier: string): string { + const parts = specifier.split("/", 2); + + // The first part could be a namespace, in which case we need to join them + if (parts.length > 1 && parts[0].startsWith("@")) return parts[0] + "/" + parts[1]; + else return parts[0]; +} + /** * Extracts the sample generation metainformation from the sample sources and * configuration in package.json. @@ -346,7 +361,7 @@ async function makeSampleGenerationInfo( .slice(-1)[0] .replace("\\", "/"), // This'll be good enough most of the time, but products like Azure Form Recognizer will have - // too adjust using the sample configuration. + // to adjust using the sample configuration. productName: sampleConfiguration.productName ?? fail(`The sample configuration does not specify a "productName".`), @@ -377,7 +392,7 @@ async function makeSampleGenerationInfo( return { dependencies: moduleInfos.reduce((prev, source) => { const current: Record = {}; - for (const dependency of source.importedModules) { + for (const dependency of source.importedModules.map(resolveModule)) { if (prev[dependency] === undefined) { const dependencyVersion = sampleConfiguration.dependencyOverrides?.[dependency] ??