-
Notifications
You must be signed in to change notification settings - Fork 10
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
Rfc/issue 570 context and theme packs #669
Conversation
Development v PublishingSo a couple thoughts here around handling developing a theme pack vs. distributing a theme pack since the location and paths of files would be different when develop (e.g. src/some/path/) as opposed to where they will ultimately be published to (e.g. /path/to/node_modules/< package-name >/dist/etc). I think we can establish a couple ground rules here
In other words, it's probably a bit easier to bend Greenwood to make it easier for local development in a one-off fashion for a plugin author, rather than forcing them to have jump through a bunch of hoops or extra build steps when wanting to publish their templates. Ideally when everything looks good, then can just publish everything as is and it will "just work". 🤞 ™️ So here are my thoughts 👇 Templates LocationLeast hackiest approach I could think of right now is simply to have the theme pack plugin accept a secret Example // _greenwood.config.js_
const packageName = require('./package.json').name;
const myThemePackPlugin = require('./theme-pack-context-plugin');
const { spawnSync } = require('child_process');
const ls = spawnSync('npm', ['ls', packageName]);
module.exports = {
plugins: [
...myThemePackPlugin({
__isDevelopment: ls.stdout.toString().indexOf('(empty)') >= 0
}]
}; // in the theme pack plugin
const path = require('path');
module.exports = (options = {}) => [{
type: 'context',
name: 'my-theme-pack:context',
provider: (compilation) => {
const { context } = compilation;
const templateLocation = options.__isDevelopment // eslint-disable-line no-underscore-dangle
? path.join(process.cwd(), 'src/layouts')
: path.join(context.projectDirectory, 'node_modules/my-theme-pack/dist/layouts');
return {
templates: [
templateLocation
]
};
}
}]; Paths in TemplatesFor paths in templates, e.g. <!DOCTYPE html>
<html lang="en" prefix="og:http://ogp.me/ns#">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="/node_modules/my-theme-pack/dist/styles/theme.css"></link>
</head>
<body>
<h1>This is a custom app template from the custom layouts directory.</h1>
<page-outlet></page-outlet>
</body>
</html> This looks a little cleaner, we can just have plugin authors use a class MyThemePackDevelopmentResource extends ResourceInterface {
constructor(compilation, options) {
super(compilation, options);
this.extensions = ['*'];
}
async shouldResolve(url) {
return Promise.resolve(url.indexOf('/node_modules/my-theme-pack/') >= 0);
}
async resolve(url) {
return url.replace('/node_modules/my-theme-pack/dist/', path.join(process.cwd(), '/src/layout/'));
}
} With all that put together, and can pretty much keep the actual code the same, and still develop like a regular Greenwood "end user". (using (so in this case, that URL doesn't actually exist 😉 ) |
86a0db3
to
748a178
Compare
Related Issue
resolves #570
Summary of Changes
context
Plugin type. as anArray
getAppTemplate
andgetPageTemplate
in plugin-standard-htmltemplate
from front matterContext
plugin as well as a guide on Creating a Theme PackExample Usage
Then in a template called layouts/title.html you can "share" a template like so
Use it in frontmatter in your own workspace
Even combine it with a custom app template too, using layouts/app.html
Greenwood will automatically handle all the node resolution for that JS and CSS!!! 🏆 🙌
TODO
ResourceInterface
?ResourceInterface
, just point everything tonode_modules
and Greenwood will take care of the rest! Brings up another question though... 👇require.resolve
for finding the right node levels directory, a la leverage NodeJS resolve logic for looking up location of paths for node modules #557/node_modules/<package_name>
convention, and Greenwood usesrequire.resolve
correctly, then users can depend on Greenwood doing all the heavy lifting, which makes sense for such a fundamental mechanism like resolve node_modules.add a greenwood.config.js option for context for overriding templates and pages dir (nice to have, might make separate issue)- implemented!additional input formats (array vs. string)- not needed at this timeuse an interface?- doesn't seem warrantedhelpful utils for plugin authors?- defer to the discussion and until we have a little more experience building these - Improve the developer experience for theme pack / context plugins #682best practices / naming conventions for users- I think by virtue of having to have a unique package name on npm anyway, there would be automatic namespacing?Discussion
context
and link to [RFC] External Data (Graph) Sources #21 and maybe they can both be done together?