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

Rfc/issue 185 resources refactor #466

Merged
merged 24 commits into from
Jan 23, 2021

Conversation

thescientist13
Copy link
Member

@thescientist13 thescientist13 commented Jan 19, 2021

Related Issue

resolves #185 - a new take on an existing design as started in #450

Summary of Changes

  1. Introduced the concept of a Resource plugin, and the ResourceInterface base class (f.k.a Transforms) with various lifecycles.
  2. Built in "standard" resource plugins for Greenwood for like JS, CSS, HTML / md, etc. (note: given the tight coupling and shared logic, merged .md and .html back into one)
  3. Refactored Koa middleware logic a bit (and more options possible using Koa Cascading middleware)
  4. There is a TypeScript like example for a .foo resource you can see on the home page
  5. Restored / refactored Google Analytics and Polyfills plugins
  6. Added tests and documentation

I know there is a lot of ground to cover here in this PR, so will go through a full demo / walk through during our next weekly meeting.

Notes / Thoughts

  • For GraphQL, we should now have the ability to handle @greenwood/data and return JS for .gql
    • will likely just need to expose a server's plugin API now
  • For TS - .foo should provide a basic POC

TODOs

  • check to see if this new resolve logic now solves / supports resolve (deeply) nested relative template paths to expected workspace path #435 (for its own PR) - left a comment, will need to give Rollup a little help here
  • move HTML serialize teardown stuff into optimize hook in standard HTML plugin
  • Need at least one example of intercept - importMap?
  • remove example .foo code from the website - you can still the same functionality in the test cases for plugins
  • fix unfinished comment in docs - Note: you can always
  • More issue / Trello tracking
    • do we need to add more font / icon types?
    • refactor devServer to use standard plugins now?
    • refactoring out parts of StandardHtmlResource plugin like importMap, liveServer into their own?
    • should Greenwood standard plugins be pushed to plugins array?

@thescientist13 thescientist13 added the RFC Proposal and changes to workflows, architecture, APIs, etc label Jan 19, 2021
@thescientist13 thescientist13 added CLI Plugins Greenwood Plugins P0 Critical issue that should get addressed ASAP labels Jan 19, 2021
@thescientist13 thescientist13 self-assigned this Jan 20, 2021
Copy link
Member

@hutchgrant hutchgrant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had got used to calling these transforms plugins ¯\_(ツ)_/¯

That modification to rollup config is perfect, I was thinking people were going to have to include their own configuration with their plugins or something. The additional intercept and optimize capability is a good idea as well.

My only gripe is with no pre-transform(resource) or default override capability. This could be addressed at a later time.

edit: also I'm not sure if I'm in agreement with the combined markdown and html resource. When I wrote #450 and attempted to decouple them, I recall it being slightly difficult because they both needed to make use of app/page templates.

}

// handle custom user file extensions
const customResources = compilation.config.plugins.filter((plugin) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Was curious how we were going to implement this in rollup. This is exactly whats needed.

}

// eslint-disable-next-line no-unused-vars
async intercept(contents, headers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea +1

// ex: remove es shim <script>, convert .ts -> .js and update path references
// this is only an _index.html_ file, BYOA (Bring Your Own AST)
// eslint-disable-next-line no-unused-vars
shouldOptimize(contents, url) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect for post-serialize plugin functionality

htmlModified = htmlModified.replace(/<script src="\/node_modules\/@webcomponents\/webcomponentsjs\/webcomponents-bundle.js"><\/script>/, '');
htmlModified = htmlModified.replace(/<script type="importmap-shim">.*?<\/script>/s, '');
htmlModified = htmlModified.replace(/<script defer="" src="\/node_modules\/es-module-shims\/dist\/es-module-shims.js"><\/script>/, '');
htmlModified = htmlModified.replace(/<script type="module-shim"/g, '<script type="module"');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 agree with this refactor.

@thescientist13
Copy link
Member Author

My only gripe is with no pre-transform(resource) or default override capability. This could be addressed at a later time.

Yeah, and I think once there are some clear use cases for that, I think it will make it easier to understand what a good design for that could look like. It could be just as easy as running shouldServe before and after maybe?

  // then handle serving urls
  app.use(async (ctx, next) => {
    // run user plugins

    // run greenwood plugins

    // run user plugins again?
  });

Or perhaps cascading middleware is better for this, but certainly solving in its own PR will allow us to drill down into the design and come up with something good once we have that clear use case.


edit: also I'm not sure if I'm in agreement with the combined markdown and html resource. When I wrote #450 and attempted to decouple them, I recall it being slightly difficult because they both needed to make use of app/page templates.

Maybe I misunderstand your comment, but given my experience is the same as yours (decoupling meant more logic spread out in multiple places, specifically shouldServe type logic and thus more bugs), I also found keeping them as one actually makes our code less susceptible to errors as a result and easier to reason about (or at the very least you only have to be looking in one place). So not sure how that makes a case for splitting them up?

@thescientist13 thescientist13 merged commit 48f77f6 into release/0.10.0 Jan 23, 2021
@thescientist13 thescientist13 deleted the rfc/issue-185-resources-refactor branch January 23, 2021 17:02
@thescientist13 thescientist13 mentioned this pull request Jan 23, 2021
12 tasks
thescientist13 added a commit that referenced this pull request Apr 3, 2021
* initial transforms refactor using Resources approach

* POC of a FooResource

* make resource resolve async and resource support for css js and json

* support standard font and image resources

* integrate markdown resource handling

* WIP resource lifecycle refactor

* refactor all plugins

* comments and code cleanup

* delete old transforms

* filename change for consistency

* custom file extension integration with rollup

* fix existing test cases to restore expected outcomes post refactor

* fix .ico resolution by using url

* made spec for a foo resource plugin

* delete old plugin specs, rename existing specs, added plugin name spec

* refactor and restore google analytics plugin

* refactor and restore polyfills plugin

* plugin documentation refactoring

* add examples and rename

* refactor serialize teardown into standard html optimize

* remove unfinished sentance from docs

* refactor import map from standard html plugin to node resolve plugin intercept

* fix linting

* remove example foo code
thescientist13 added a commit that referenced this pull request Apr 3, 2021
* initial transforms refactor using Resources approach

* POC of a FooResource

* make resource resolve async and resource support for css js and json

* support standard font and image resources

* integrate markdown resource handling

* WIP resource lifecycle refactor

* refactor all plugins

* comments and code cleanup

* delete old transforms

* filename change for consistency

* custom file extension integration with rollup

* fix existing test cases to restore expected outcomes post refactor

* fix .ico resolution by using url

* made spec for a foo resource plugin

* delete old plugin specs, rename existing specs, added plugin name spec

* refactor and restore google analytics plugin

* refactor and restore polyfills plugin

* plugin documentation refactoring

* add examples and rename

* refactor serialize teardown into standard html optimize

* remove unfinished sentance from docs

* refactor import map from standard html plugin to node resolve plugin intercept

* fix linting

* remove example foo code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI P0 Critical issue that should get addressed ASAP Plugins Greenwood Plugins RFC Proposal and changes to workflows, architecture, APIs, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

determine API / usage for plugins [RFC] Plugins: Transforms
2 participants