Skip to content

Commit

Permalink
create import map merging function and apply to graphql plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Mar 12, 2022
1 parent b63bce4 commit 2da6a61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
26 changes: 20 additions & 6 deletions packages/cli/src/lib/walker-package-ranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function resolveRelativeSpecifier(specifier, modulePath, dependency) {
return `${dependency}${path.join(path.dirname(modulePath), specifier).replace(/\\/g, '/').replace(absoluteNodeModulesLocation.replace(/\\/g, '/', ''), '')}`;
}

const getPackageEntryPath = async (packageJson) => {
async function getPackageEntryPath(packageJson) {
let entry = packageJson.exports
? Object.keys(packageJson.exports) // first favor export maps first
: packageJson.module // next favor ESM entry points
Expand All @@ -41,9 +41,9 @@ const getPackageEntryPath = async (packageJson) => {
}

return entry;
};
}

const walkModule = async (module, dependency) => {
async function walkModule(module, dependency) {
const moduleContents = fs.readFileSync(modulePath, 'utf-8');

walk.simple(acorn.parse(moduleContents, {
Expand Down Expand Up @@ -107,9 +107,9 @@ const walkModule = async (module, dependency) => {
}
}
});
};
}

const walkPackageJson = async (packageJson = {}) => {
async function walkPackageJson(packageJson = {}) {
// while walking a package.json we need to find its entry point, e.g. index.js
// and then walk that for import / export statements
// and walk its package.json for its dependencies
Expand Down Expand Up @@ -213,9 +213,23 @@ const walkPackageJson = async (packageJson = {}) => {
}

return importMap;
};
}

function mergeImportMap(html = '', map = {}) {
// es-modules-shims breaks on dangling commas in an importMap :/
const danglingComma = html.indexOf('"imports": {}') > 0 ? '' : ',';
const importMap = JSON.stringify(map).replace('}', '').replace('{', '');

const merged = html.replace('"imports": {', `
"imports": {
${importMap}${danglingComma}
`);

return merged;
}

export {
mergeImportMap,
walkPackageJson,
walkModule
};
27 changes: 12 additions & 15 deletions packages/plugin-graphql/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import { graphqlServer } from './core/server.js';
import { mergeImportMap } from '@greenwood/cli/src/lib/walker-package-ranger.js';
import path from 'path';
import { ResourceInterface } from '@greenwood/cli/src/lib/resource-interface.js';
import { ServerInterface } from '@greenwood/cli/src/lib/server-interface.js';
Expand Down Expand Up @@ -37,22 +38,18 @@ class GraphQLResource extends ResourceInterface {
async intercept(url, body) {
return new Promise(async (resolve, reject) => {
try {
// es-modules-shims breaks on dangling commas in an importMap :/
const danglingComma = body.indexOf('"imports": {}') > 0
? ''
: ',';
const shimmedBody = body.replace('"imports": {', `
"imports": {
"@greenwood/cli/src/lib/hashing-utils.js": "/node_modules/@greenwood/cli/src/lib/hashing-utils.js",
"@greenwood/plugin-graphql/core/client": "/node_modules/@greenwood/plugin-graphql/src/core/client.js",
"@greenwood/plugin-graphql/core/common": "/node_modules/@greenwood/plugin-graphql/src/core/common.js",
"@greenwood/plugin-graphql/queries/children": "/node_modules/@greenwood/plugin-graphql/src/queries/children.gql",
"@greenwood/plugin-graphql/queries/config": "/node_modules/@greenwood/plugin-graphql/src/queries/config.gql",
"@greenwood/plugin-graphql/queries/graph": "/node_modules/@greenwood/plugin-graphql/src/queries/graph.gql",
"@greenwood/plugin-graphql/queries/menu": "/node_modules/@greenwood/plugin-graphql/src/queries/menu.gql"${danglingComma}
`);
const map = {
'@greenwood/cli/src/lib/hashing-utils.js': '/node_modules/@greenwood/cli/src/lib/hashing-utils.js',
'@greenwood/plugin-graphql/core/client': '/node_modules/@greenwood/plugin-graphql/src/core/client.js',
'@greenwood/plugin-graphql/core/common': '/node_modules/@greenwood/plugin-graphql/src/core/common.js',
'@greenwood/plugin-graphql/queries/children': '/node_modules/@greenwood/plugin-graphql/src/queries/children.gql',
'@greenwood/plugin-graphql/queries/config': '/node_modules/@greenwood/plugin-graphql/src/queries/config.gql',
'@greenwood/plugin-graphql/queries/graph': '/node_modules/@greenwood/plugin-graphql/src/queries/graph.gql',
'@greenwood/plugin-graphql/queries/menu': '/node_modules/@greenwood/plugin-graphql/src/queries/menu.gql'
};
const newBody = mergeImportMap(body, map);

resolve({ body: shimmedBody });
resolve({ body: newBody });
} catch (e) {
reject(e);
}
Expand Down

0 comments on commit 2da6a61

Please sign in to comment.