Skip to content

Commit

Permalink
538 refactor rollup and resource to use shared data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Mar 12, 2022
1 parent 2da6a61 commit 59d3fa2
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions packages/plugin-graphql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import { ResourceInterface } from '@greenwood/cli/src/lib/resource-interface.js'
import { ServerInterface } from '@greenwood/cli/src/lib/server-interface.js';
import rollupPluginAlias from '@rollup/plugin-alias';

const importMap = {
'@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'
};

class GraphQLResource extends ResourceInterface {
constructor(compilation, options = {}) {
super(compilation, options);
Expand Down Expand Up @@ -38,16 +48,7 @@ class GraphQLResource extends ResourceInterface {
async intercept(url, body) {
return new Promise(async (resolve, reject) => {
try {
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);
const newBody = mergeImportMap(body, importMap);

resolve({ body: newBody });
} catch (e) {
Expand Down Expand Up @@ -102,18 +103,18 @@ const greenwoodPluginGraphQL = (options = {}) => {
}, {
type: 'rollup',
name: 'plugin-graphql:rollup',
provider: () => [
rollupPluginAlias({
entries: [
{ find: '@greenwood/plugin-graphql/core/client', replacement: '@greenwood/plugin-graphql/src/core/client.js' },
{ find: '@greenwood/plugin-graphql/core/common', replacement: '@greenwood/plugin-graphql/src/core/common.js' },
{ find: '@greenwood/plugin-graphql/queries/menu', replacement: '@greenwood/plugin-graphql/src/queries/menu.gql' },
{ find: '@greenwood/plugin-graphql/queries/config', replacement: '@greenwood/plugin-graphql/src/queries/config.gql' },
{ find: '@greenwood/plugin-graphql/queries/children', replacement: '@greenwood/plugin-graphql/src/queries/children.gql' },
{ find: '@greenwood/plugin-graphql/queries/graph', replacement: '@greenwood/plugin-graphql/src/queries/graph.gql' }
]
})
]
provider: () => {
const aliasEntries = Object.keys(importMap).map(key => {
return {
find: key,
replacement: importMap[key].replace('/node_modules/', '')
};
});

return [
rollupPluginAlias({ entries: aliasEntries })
];
}
}];
};

Expand Down

0 comments on commit 59d3fa2

Please sign in to comment.