Skip to content

Commit

Permalink
support multiple custom page formats at once
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Aug 10, 2024
1 parent 390d7a1 commit 69724eb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
20 changes: 13 additions & 7 deletions packages/cli/src/lifecycles/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,28 @@ const generateGraph = async (compilation) => {
pages = nextPages.pages;
apiRoutes = nextPages.apiRoutes;
} else {
const req = new Request(filenameUrl, { headers: { 'Accept': 'text/html' } });
const extension = `.${filenameUrl.pathname.split('.').pop()}`;
const isCustom = customPageFormatPlugins[0] && customPageFormatPlugins[0].shouldServe && await customPageFormatPlugins[0].shouldServe(filenameUrl, req)
? customPageFormatPlugins[0].servePage
: null;
const relativePagePath = filenameUrl.pathname.replace(pagesDir.pathname, '/');
const relativeWorkspacePath = directory.pathname.replace(projectDirectory.pathname, '');
const isApiRoute = relativePagePath.startsWith('/api');
const req = isApiRoute
? new Request(filenameUrl)
: new Request(filenameUrl, { headers: { 'Accept': 'text/html' } });
let isCustom = null;

for (const plugin of customPageFormatPlugins) {
if (plugin.shouldServe && await plugin.shouldServe(filenameUrl, req)) {
isCustom = plugin.servePage;
break;
}
}

const isStatic = isCustom === 'static' || extension === '.md' || extension === '.html';
const isDynamic = isCustom === 'dynamic' || extension === '.js';
const isApiRoute = relativePagePath.startsWith('/api');
const isPage = isStatic || isDynamic;

if (isApiRoute) {
const req = new Request(filenameUrl);
const extension = filenameUrl.pathname.split('.').pop();
const isCustom = customPageFormatPlugins[0] && customPageFormatPlugins[0].shouldServe && await customPageFormatPlugins[0].shouldServe(filenameUrl, req);

if (extension !== 'js' && !isCustom) {
console.warn(`${filenameUrl} is not a supported API file extension, skipping...`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ class FooResource {
}
}

class BarResource {
constructor(compilation, options) {
this.compilation = compilation;
this.options = options;
this.servePage = options.servePage;

this.extensions = ['bar'];
this.contentType = 'text/javascript';
}

async shouldServe(url) {
return url.pathname.split('.').pop() === this.extensions[0] && this.servePage;
}

async serve(url) {
let body = await fs.readFile(url, 'utf-8');

body = body.replace(/interface (.*){(.*)}/s, '');

return new Response(body, {
headers: new Headers({
'Content-Type': this.contentType
})
});
}
}

const greenwoodPluginFooResource = (options = {}) => {
return [{
type: 'resource',
Expand All @@ -35,10 +62,21 @@ const greenwoodPluginFooResource = (options = {}) => {
}];
};

const greenwoodPluginBarResource = (options = {}) => {
return [{
type: 'resource',
name: 'plugin-import-bar:resource',
provider: (compilation) => new BarResource(compilation, options)
}];
};

export default {
plugins: [
greenwoodPluginFooResource({
servePage: 'static'
}),
greenwoodPluginBarResource({
servePage: 'dynamic'
})
]
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
* // see complete implementation in the greenwood.config.js file used for this spec
* }
*
* class BarResource extends ResourceInterface {
* // see complete implementation in the greenwood.config.js file used for this spec
* }
*
* {
* plugins: [{
* type: 'resource',
* name: 'plugin-foo',
* provider: (compilation, options) => new FooResource(compilation, options)
* },{
* type: 'resource',
* name: 'plugin-bar',
* provider: (compilation, options) => new BarResource(compilation, options)
* }]
* }
*
Expand Down Expand Up @@ -48,7 +56,7 @@ describe('Build Greenwood With: ', function() {
this.context = {
publicDir: path.join(outputPath, 'public')
};
runner = new Runner();
runner = new Runner(false, true);
});

describe(LABEL, function() {
Expand Down Expand Up @@ -89,24 +97,25 @@ describe('Build Greenwood With: ', function() {
});
});

// TODO not sure why this was disabled, but should enable this test case
xdescribe('Custom Format Dynamic Contact Page', function() {
let aboutPage;
describe('Custom Format Dynamic Contact Page (exported with prerendering)', function() {
let contactPage;

before(async function() {
aboutPage = await glob.promise(path.join(this.context.publicDir, 'contact.html'));
contactPage = await glob.promise(path.join(this.context.publicDir, 'contact/index.html'));
});

it('should have the expected JavaScript equivalent file in the output directory', function() {
expect(aboutPage).to.have.lengthOf(1);
it('should have the expected pre-rendered HTML file in the output directory', function() {
expect(contactPage).to.have.lengthOf(1);
});

it('should have expected text from from my-other-custom-file.foo in the script output file', function() {
const contents = fs.readFileSync(aboutPage[0], 'utf-8');
it('should have expected text from the output HTML file', function() {
const contents = fs.readFileSync(contactPage[0], 'utf-8');

expect(contents).to.contain('Welcome to our About page!');
expect(contents).to.contain('Welcome to our Contact page!');
});
});

// TODO test an API route with a dynamic format plugin
});

after(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export default class ContactPage extends HTMLElement {
<h1>Welcome to our Contact page!</h1>
`;
}
}
}

export const prerender = true;

0 comments on commit 69724eb

Please sign in to comment.