Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/issue 278 graphql plugin #487

Merged
merged 29 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6ea0e05
init commit of WIP with server and graphiql working
thescientist13 Mar 6, 2021
059b890
WIP client side implementation
thescientist13 Mar 6, 2021
21d6647
output message when processing a commonjs file
thescientist13 Mar 6, 2021
2b39aa1
debug statement
thescientist13 Mar 6, 2021
fb86d6a
custom graphql client
thescientist13 Mar 6, 2021
7ca8651
restore graphQL to shelf
thescientist13 Mar 7, 2021
d639daa
refactor shelf
thescientist13 Mar 7, 2021
faf7bba
rollup bundling working
thescientist13 Mar 7, 2021
c9a9090
graphql working in production build
thescientist13 Mar 7, 2021
6018887
upgrade backend apollo 3
thescientist13 Mar 7, 2021
8e46ddd
move and re-enable data unit tests
thescientist13 Mar 11, 2021
3426df8
align server start usage with develop command
thescientist13 Mar 13, 2021
49844de
fixed bug with dangling commas in es-modules-shims
thescientist13 Mar 13, 2021
51584c2
add new config query test case
thescientist13 Mar 13, 2021
d7b98bb
tests for menu query for navigation
thescientist13 Mar 13, 2021
7b9b5f7
clean up
thescientist13 Mar 13, 2021
021135b
add support for children query aliasing
thescientist13 Mar 13, 2021
b50f544
children query specs
thescientist13 Mar 13, 2021
fa09926
cleanup
thescientist13 Mar 13, 2021
15bb49b
graph query test cases and support
thescientist13 Mar 13, 2021
8adc39f
delete old cases
thescientist13 Mar 13, 2021
63628a4
console cleanup
thescientist13 Mar 13, 2021
c8bb1bd
fix label confusion and restore id
thescientist13 Mar 14, 2021
a2edc08
remove frontmatter
thescientist13 Mar 14, 2021
5b15169
refactoring and promisifying
thescientist13 Mar 14, 2021
9b6df9b
graph test with custom frontmatter
thescientist13 Mar 14, 2021
efbb1c9
restore docs
thescientist13 Mar 14, 2021
74b657a
remove example config query
thescientist13 Mar 14, 2021
6164d82
delete commented out code
thescientist13 Mar 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path');

module.exports = {
// TODO spec: path.join(__dirname, 'packages/**/test/**/**/**/*.spec.js'),
spec: path.join(__dirname, 'packages/**/test/cases/**/**/*.spec.js'),
spec: path.join(__dirname, 'packages/**/test/**/**/**/*.spec.js'),
timeout: 30000
};
4 changes: 4 additions & 0 deletions greenwood.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const path = require('path');
// const pluginImportCommonjs = require('./packages/plugin-import-commonjs/src/index');
const pluginGoogleAnalytics = require('./packages/plugin-google-analytics/src/index');
const pluginGraphQL = require('./packages/plugin-graphql/src/index');
const pluginImportCss = require('./packages/plugin-import-css/src/index');
const pluginPolyfills = require('./packages/plugin-polyfills/src/index');
const pluginPostCss = require('./packages/plugin-postcss/src/index');
Expand All @@ -23,9 +25,11 @@ module.exports = {
{ name: 'google-site-verification', content: '4rYd8k5aFD0jDnN0CCFgUXNe4eakLP4NnA18mNnK5P0' }
],
plugins: [
// ...pluginImportCommonjs(),
pluginGoogleAnalytics({
analyticsId: 'UA-147204327-1'
}),
...pluginGraphQL(),
pluginPolyfills(),
pluginPostCss(),
...pluginImportCss()
Expand Down
44 changes: 33 additions & 11 deletions packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const bundleCompilation = require('../lifecycles/bundle');
const copyAssets = require('../lifecycles/copy');
const { devServer } = require('../lifecycles/serve');
const fs = require('fs');
const generateCompilation = require('../lifecycles/compile');
const serializeCompilation = require('../lifecycles/serialize');
const { devServer } = require('../lifecycles/serve');
const { ServerInterface } = require('../lib/server-interface');

module.exports = runProductionBuild = async () => {

Expand All @@ -14,17 +15,38 @@ module.exports = runProductionBuild = async () => {
const port = compilation.config.devServer.port;
const outputDir = compilation.context.outputDir;

devServer(compilation).listen(port);

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}

await serializeCompilation(compilation);
await bundleCompilation(compilation);
await copyAssets(compilation);
devServer(compilation).listen(port, async () => {
console.info(`Started local development server at localhost:${port}`);

// custom user server plugins
const servers = [...compilation.config.plugins.filter((plugin) => {
return plugin.type === 'server';
}).map((plugin) => {
const provider = plugin.provider(compilation);

if (!(provider instanceof ServerInterface)) {
console.warn(`WARNING: ${plugin.name}'s provider is not an instance of ServerInterface.`);
}

return provider;
})];

await Promise.all(servers.map(async (server) => {
server.start();

return Promise.resolve(server);
}));

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}

await serializeCompilation(compilation);
await bundleCompilation(compilation);
await copyAssets(compilation);

resolve();
resolve();
});
} catch (err) {
reject(err);
}
Expand Down
17 changes: 9 additions & 8 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,17 @@ function greenwoodHtmlPlugin(compilation) {
case '.html':
return Promise.resolve('');
default:
customResources.filter((resource) => {
const shouldServe = Promise.resolve(resource.shouldServe(id));
const resourceHandler = (await Promise.all(customResources.map(async (resource) => {
const shouldServe = await resource.shouldServe(id);

if (shouldServe) {
return resource;
}
});
return shouldServe
? resource
: null;
}))).filter(resource => resource);

if (customResources.length) {
const response = await customResources[0].serve(id);
// TODO should reduce here instead
if (resourceHandler.length) {
const response = await resourceHandler[0].serve(id);

return Promise.resolve(response.body);
}
Expand Down
46 changes: 0 additions & 46 deletions packages/cli/src/data/common.js

This file was deleted.

10 changes: 0 additions & 10 deletions packages/cli/src/data/queries/children.gql

This file was deleted.

10 changes: 0 additions & 10 deletions packages/cli/src/data/queries/graph.gql

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let defaultConfig = {
devServer: {
port: 1984
},
// TODO optimization: 'spa',
optimization: '',
title: 'My App',
meta: [],
plugins: [],
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/lifecycles/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ module.exports = generateGraph = async (compilation) => {
const relativeWorkspacePath = directory.replace(process.cwd(), '').replace('/', '');
const template = attributes.template || 'page';
const title = attributes.title || compilation.config.title || '';
const label = attributes.label || filename.split('/')[filename.split('/').length - 1].replace('.md', '').replace('.html', '');
const id = attributes.label || filename.split('/')[filename.split('/').length - 1].replace('.md', '').replace('.html', '');
const label = id.split('-')
.map((idPart) => {
return `${idPart.charAt(0).toUpperCase()}${idPart.substring(1)}`;
}).join(' ');
let route = relativePagePath.replace('.md', '').replace('.html', '');

/*
Expand All @@ -37,7 +41,7 @@ module.exports = generateGraph = async (compilation) => {
*/
if (relativePagePath.lastIndexOf('/') > 0) {
// https://github.com/ProjectEvergreen/greenwood/issues/455
route = label === 'index' || route.replace('/index', '') === `/${label}`
route = id === 'index' || route.replace('/index', '') === `/${id}`
? route.replace('index', '')
: `${route}/`;
} else {
Expand Down Expand Up @@ -91,7 +95,8 @@ module.exports = generateGraph = async (compilation) => {
*----------------------
* data: custom page frontmatter
* filename: name of the file
* label: text representation of the filename
* id: filename without the extension
* label: "pretty" text representation of the filename
* path: path to the file relative to the workspace
* route: URL route for a given page on outputFilePath
* template: page template to use as a base for a generated component
Expand All @@ -100,6 +105,7 @@ module.exports = generateGraph = async (compilation) => {
pages.push({
data: customData || {},
filename,
id,
label,
path: route === '/' || relativePagePath.lastIndexOf('/') === 0
? `${relativeWorkspacePath}${filename}`
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function getProdServer(compilation) {
}

if (url.endsWith('.json')) {
const contents = await fs.promises.readFile(path.join(outputDir, 'graph.json'), 'utf-8');
const contents = await fs.promises.readFile(path.join(outputDir, url), 'utf-8');

ctx.set('Content-Type', 'application/json');
ctx.body = JSON.parse(contents);
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,14 @@ class NodeModulesResource extends ResourceInterface {
async serve(url) {
return new Promise(async(resolve, reject) => {
try {
const fullUrl = path.extname(url) === '' ? `${url}.js` : url;
const fullUrl = path.extname(url) === ''
? `${url}.js`
: url;
// const fullUrl = path.extname(url) === ''
// ? fs.existsSync(`${url}.mjs`) // test for .mjs first
// ? `${url}.mjs`
// : `${url}.js`
// : url;
const body = await fs.promises.readFile(fullUrl);

resolve({
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/plugins/server/plugin-livereload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const livereload = require('livereload');
const path = require('path');
const { ResourceInterface } = require('../../lib/resource-interface');
const { ServerInterface } = require('../../lib/server-interface');

Expand All @@ -25,8 +24,10 @@ class LiveReloadServer extends ServerInterface {

class LiveReloadResource extends ResourceInterface {

async shouldIntercept(url) {
return Promise.resolve(path.extname(url) === '' && process.env.__GWD_COMMAND__ === 'develop'); // eslint-disable-line no-underscore-dangle
async shouldIntercept(url, body, headers) {
const { accept } = headers.request;

return Promise.resolve(accept && accept.indexOf('text/html') >= 0 && process.env.__GWD_COMMAND__ === 'develop'); // eslint-disable-line no-underscore-dangle
}

async intercept(url, body) {
Expand Down
Loading