Skip to content

Commit

Permalink
spectrum web components example
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Nov 23, 2024
1 parent 7135a50 commit c1f505b
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 61 deletions.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
"start": "pnpm run serve"
},
"dependencies": {
"@spectrum-web-components/action-menu": "^1.0.1",
"@spectrum-web-components/button": "^1.0.1",
"@spectrum-web-components/menu": "^1.0.1",
"@spectrum-web-components/shared": "^1.0.1",
"@spectrum-web-components/styles": "^1.0.1",
"@spectrum-web-components/theme": "^1.0.1",
"@spectrum-web-components/top-nav": "^1.0.1",
"lit": "^3.1.0"
},
"devDependencies": {
Expand Down
94 changes: 42 additions & 52 deletions patches/@greenwood__cli.patch
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ index 05226f66bf42fc0a07588d373b14932cbeb9ff7f..a86924565e964815769e1fc788bf5c6c
};
\ No newline at end of file
diff --git a/src/lib/walker-package-ranger.js b/src/lib/walker-package-ranger.js
index a92bb3d29496a453f1714e5cd32d1800c94844be..68fd30e718f5493b426b88bc8e6aa13753c2c2c0 100644
index a92bb3d29496a453f1714e5cd32d1800c94844be..e081405fed7ce326659f2dd806a6801b0b3d2e24 100644
--- a/src/lib/walker-package-ranger.js
+++ b/src/lib/walker-package-ranger.js
@@ -1,217 +1,120 @@
@@ -1,215 +1,111 @@
-/* eslint-disable max-depth,complexity */
-import * as acorn from 'acorn';
-import fs from 'fs';
Expand All @@ -60,32 +60,32 @@ index a92bb3d29496a453f1714e5cd32d1800c94844be..68fd30e718f5493b426b88bc8e6aa137
+const extensionFilters = ['map', 'd.ts'];

-const updateImportMap = (entry, entryPath) => {
-
- if (path.extname(entryPath) === '') {
- entryPath = `${entryPath}.js`;
- }
+function updateImportMap(key, value) {
+ importMap[key.replace('./', '')] = value.replace('./', '');
+}

- if (path.extname(entryPath) === '') {
- entryPath = `${entryPath}.js`;
- }
- // handle WIn v Unix-style path separators and force to /
- importMap[entry.replace(/\\/g, '/')] = entryPath.replace(/\\/g, '/');
-};
+// https://nodejs.org/api/packages.html#subpath-patterns
+async function walkExportPatterns(dependency, exp, resolvedRoot) {
+ if (exp.endsWith('*')) {
+ const dir = new URL(exp.replace('*', ''), resolvedRoot);
+ const files = await fs.readdir(dir);
+ // console.log({ dependency, exp, dir });

- // handle WIn v Unix-style path separators and force to /
- importMap[entry.replace(/\\/g, '/')] = entryPath.replace(/\\/g, '/');
-};
+ files
+ .filter((file) => {
+ let shouldNotFilter = true;

-// handle ESM paths that have varying levels of nesting, e.g. export * from '../../something.js'
-// https://github.com/ProjectEvergreen/greenwood/issues/820
-async function resolveRelativeSpecifier(specifier, modulePath, dependency) {
- const absoluteNodeModulesLocation = await getNodeModulesLocationForPackage(dependency);
-
+ files
+ .filter((file) => {
+ let shouldNotFilter = true;

- // handle WIn v Unix-style path separators and force to /
- return `${dependency}${path.join(path.dirname(modulePath), specifier).replace(/\\/g, '/').replace(absoluteNodeModulesLocation.replace(/\\/g, '/', ''), '')}`;
-}
Expand Down Expand Up @@ -137,9 +137,7 @@ index a92bb3d29496a453f1714e5cd32d1800c94844be..68fd30e718f5493b426b88bc8e6aa137
- if (!importMap[sourceValue]) {
- updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
- }
+async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
+ const { exports, module, main } = packageJson;

-
- await walkPackageJson(path.join(absoluteNodeModulesLocation, 'package.json'));
- } else if (isBarePath) {
- updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
Expand All @@ -148,7 +146,8 @@ index a92bb3d29496a453f1714e5cd32d1800c94844be..68fd30e718f5493b426b88bc8e6aa137
- sourceValue = !hasExtension
- ? `${sourceValue}.js`
- : sourceValue;
+ console.log('walkPackageForExports', { dependency, exports, module, main });
+async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
+ const { exports, module, main } = packageJson;

- if (fs.existsSync(path.join(absoluteNodeModulesLocation, sourceValue))) {
- const entry = `/node_modules/${await resolveRelativeSpecifier(sourceValue, modulePath, dependency)}`;
Expand Down Expand Up @@ -282,38 +281,16 @@ index a92bb3d29496a453f1714e5cd32d1800c94844be..68fd30e718f5493b426b88bc8e6aa137
- // use the dependency itself as an entry in the importMap
- if (entry === '.') {
- updateImportMap(dependency, `/node_modules/${path.join(dependency, packageExport)}`);
- }
- } else if (exportMapEntry.endsWith && (exportMapEntry.endsWith('.js') || exportMapEntry.endsWith('.mjs')) && exportMapEntry.indexOf('*') < 0) {
- // is probably a file, so _not_ an export array, package.json, or wildcard export
- packageExport = exportMapEntry;
- }
+ console.log({ packageJson });
+ try {
+ for (const dependency of Object.keys(packageJson.dependencies || {})) {
+ const resolved = resolveBareSpecifier(dependency);
+
+ console.log({ dependency, resolved });
+ if (resolved) {
+ console.log(resolved.split(`/node_modules/${dependency}/`)[0]);
+ const resolvedRoot = new URL(`./node_modules/${dependency}/`, `${resolved.split(`/node_modules/${dependency}/`)[0]}/`);
+ console.log({ resolvedRoot })
+ const resolvedPackageJson = (await import(new URL('./package.json', resolvedRoot), { with: { type: 'json' } })).default;
+
+ console.log({ resolvedPackageJson });
+ walkPackageForExports(dependency, resolvedPackageJson, resolvedRoot);
+
+ if (resolvedPackageJson.dependencies) {
+ for (const dependency in resolvedPackageJson.dependencies) {
+ const resolved = resolveBareSpecifier(dependency);
+
+ if (resolved) {
+ const resolvedRoot = new URL(`./node_modules/${dependency}/`, `${resolved.split(`/node_modules/${dependency}/`)[0]}/`);
+ const resolvedPackageJson = (await import(new URL('./package.json', resolvedRoot), { with: { type: 'json' } })).default;
+
+ walkPackageForExports(dependency, resolvedPackageJson, resolvedRoot);
+
+ await walkPackageJson(resolvedPackageJson);
}
- } else if (exportMapEntry.endsWith && (exportMapEntry.endsWith('.js') || exportMapEntry.endsWith('.mjs')) && exportMapEntry.indexOf('*') < 0) {
- // is probably a file, so _not_ an export array, package.json, or wildcard export
- packageExport = exportMapEntry;
}
-

- if (packageExport) {
- const packageExportLocation = path.resolve(absoluteNodeModulesLocation, packageExport);
-
Expand All @@ -330,29 +307,42 @@ index a92bb3d29496a453f1714e5cd32d1800c94844be..68fd30e718f5493b426b88bc8e6aa137
- }
- }
- }
-
+ if (resolved) {
+ const resolvedRoot = new URL(`./node_modules/${dependency}/`, `${resolved.split(`/node_modules/${dependency}/`)[0]}/`);
+ const resolvedPackageJson = (await import(new URL('./package.json', resolvedRoot), { with: { type: 'json' } })).default;

- await walkPackageJson(dependencyPackageJson);
- } else {
- const packageEntryPointPath = path.join(absoluteNodeModulesLocation, entry);
-
+ walkPackageForExports(dependency, resolvedPackageJson, resolvedRoot);

- // sometimes a main file is actually just an empty string... :/
- if (fs.existsSync(packageEntryPointPath)) {
- updateImportMap(dependency, `/node_modules/${path.join(dependency, entry)}`);
-
+ if (resolvedPackageJson.dependencies) {
+ for (const dependency in resolvedPackageJson.dependencies) {
+ const resolved = resolveBareSpecifier(dependency);

- await walkModule(packageEntryPointPath, dependency);
- await walkPackageJson(dependencyPackageJson);
+ if (resolved) {
+ const resolvedRoot = new URL(`./node_modules/${dependency}/`, `${resolved.split(`/node_modules/${dependency}/`)[0]}/`);
+ const resolvedPackageJson = (await import(new URL('./package.json', resolvedRoot), { with: { type: 'json' } })).default;
+
+ walkPackageForExports(dependency, resolvedPackageJson, resolvedRoot);
+
+ await walkPackageJson(resolvedPackageJson);
+ }
+ }
}
}
}
+ } catch (e) {
+ console.error('Error building up import map', e);
}

+ console.log({ importMap });
return importMap;
}

@@ -245,7 +148,6 @@ function mergeImportMap(html = '', map = {}, shouldShim = false) {
@@ -245,7 +141,6 @@ function mergeImportMap(html = '', map = {}, shouldShim = false) {
}

export {
Expand Down
Loading

0 comments on commit c1f505b

Please sign in to comment.