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

Create Block: Simplify the process of defining a config for templates #22235

Merged
merged 5 commits into from
May 13, 2020

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented May 8, 2020

Description

Related to efforts started by @fabiankaegy in #22175.

The goal here is to make it simplify the process of defining templates:

  • a template can define only overrides for default values provided by the tool for all prompts
  • there is no longer need to list all output template files, glob detects them based on the file path that is assumed as lib/templates/${ templateName } for predefined templates
  • wpScripts are enabled by default, the template author can disable that by setting to false

There was general refactoring performed to process the template and create full configuration first and use such an object to pass to all other helper functions. This way, it's going to be way much easier to handle external templates as explored in #22175.

How has this been tested?

npx wp-create-block fun-with-template
npx wp-create-block fun-with-template -t es5

Types of changes

Refactoring.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • [N/A] My code follows the accessibility standards.
  • My code has proper inline documentation.
  • [N/A] I've included developer documentation if appropriate.
  • [N/A] I've updated all React Native files affected by any refactorings/renamings in this PR.

@gziolo gziolo added [Type] Enhancement A suggestion for improvement. [Status] In Progress Tracking issues with work in progress [Package] Create Block /packages/create-block labels May 8, 2020
@gziolo gziolo self-assigned this May 8, 2020
@gziolo gziolo force-pushed the update/create-block-glob-templates branch from 9a2bf50 to 69da572 Compare May 8, 2020 18:10
@github-actions
Copy link

github-actions bot commented May 8, 2020

Size Change: +3.76 kB (0%)

Total Size: 828 kB

Filename Size Change
build/annotations/index.js 3.62 kB -1 B
build/api-fetch/index.js 4.08 kB -2 B (0%)
build/block-directory/index.js 6.59 kB -21 B (0%)
build/block-directory/style-rtl.css 764 B +4 B (0%)
build/block-directory/style.css 764 B +3 B (0%)
build/block-editor/index.js 104 kB +1.41 kB (1%)
build/block-editor/style-rtl.css 10.6 kB +257 B (2%)
build/block-editor/style.css 10.6 kB +253 B (2%)
build/block-library/index.js 115 kB -125 B (0%)
build/blocks/index.js 48.1 kB -4 B (0%)
build/components/index.js 181 kB +828 B (0%)
build/compose/index.js 6.66 kB -1 B
build/core-data/index.js 11.4 kB -10 B (0%)
build/data-controls/index.js 1.29 kB +2 B (0%)
build/data/index.js 8.42 kB -22 B (0%)
build/edit-navigation/index.js 5.59 kB +1.19 kB (21%) 🚨
build/edit-post/index.js 28 kB +5 B (0%)
build/edit-site/index.js 12.1 kB -4 B (0%)
build/edit-widgets/index.js 8.37 kB +2 B (0%)
build/editor/index.js 44.3 kB -8 B (0%)
build/element/index.js 4.65 kB -1 B
build/format-library/index.js 7.63 kB -1 B
build/hooks/index.js 2.13 kB -6 B (0%)
build/keyboard-shortcuts/index.js 2.51 kB -1 B
build/list-reusable-blocks/index.js 3.13 kB +4 B (0%)
build/media-utils/index.js 5.29 kB +4 B (0%)
build/notices/index.js 1.79 kB +1 B
build/nux/index.js 3.4 kB -1 B
build/plugins/index.js 2.56 kB -1 B
build/rich-text/index.js 14.8 kB -1 B
build/server-side-render/index.js 2.68 kB +2 B (0%)
build/viewport/index.js 1.84 kB +1 B
build/wordcount/index.js 1.17 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-library/editor-rtl.css 7.12 kB 0 B
build/block-library/editor.css 7.12 kB 0 B
build/block-library/style-rtl.css 7.38 kB 0 B
build/block-library/style.css 7.38 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/components/style-rtl.css 17 kB 0 B
build/components/style.css 16.9 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/style-rtl.css 618 B 0 B
build/edit-navigation/style.css 617 B 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/style-rtl.css 5.22 kB 0 B
build/edit-site/style.css 5.22 kB 0 B
build/edit-widgets/style-rtl.css 4.69 kB 0 B
build/edit-widgets/style.css 4.69 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/escape-html/index.js 734 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/warning/index.js 1.14 kB 0 B

compressed-size-action

@gziolo gziolo changed the title Create Block: Use glob to find all template files Create Block: Simplify the process of defining a config for templates May 8, 2020
@gziolo gziolo marked this pull request as ready for review May 11, 2020 10:48
@gziolo gziolo force-pushed the update/create-block-glob-templates branch from 89d83ad to f020b6b Compare May 11, 2020 11:12
@gziolo
Copy link
Member Author

gziolo commented May 11, 2020

@fabiankaegy – I refactored code to make it possible to contain the logic that downloads the npm template in getBlockTemplate function.

@gziolo gziolo removed the [Status] In Progress Tracking issues with work in progress label May 11, 2020
@gziolo gziolo requested a review from aduth May 11, 2020 11:19
@gziolo gziolo force-pushed the update/create-block-glob-templates branch from a4631c8 to 0565d15 Compare May 11, 2020 16:26
packages/create-block/lib/templates.js Outdated Show resolved Hide resolved
packages/create-block/lib/templates.js Outdated Show resolved Hide resolved
'.mustache',
''
);
const outputTemplate = await readFile(
Copy link
Member

Choose a reason for hiding this comment

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

Probably outside the scope of what you're trying to do here, since as far as I know it's not fundamentally changing anything that doesn't already exist, but: I wonder if we should be lazier here. Rather than eagerly loading every template for every template option, at least only loading the template files for what's relevant given the user input.

It could be a matter of the template string being a function to call when needed, or something more "clever" (not necessarily a good thing) like Object.defineProperty getters, or only getOutputTemplates for templates relevant by input.

The last might also be good to avoid fast-glob if we know we won't need the template files.

Copy link
Member Author

@gziolo gziolo May 13, 2020

Choose a reason for hiding this comment

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

It could be a matter of the template string being a function to call when needed

The last might also be good to avoid fast-glob if we know we won't need the template files.

To clarify, it only loads all templates for a block template selected when calling npm init @wordpress/block. By default, it will only grab files from lib/templates/esnext folder. At the moment, we use all loaded templates to scaffold the project for the block.

Rather than eagerly loading every template for every template option, at least only loading the template files for what's relevant given the user input.

Once we have some conditions that make some templates options, it will make sense to defer loading templates and using a technique like " template string being a function" 👍

Did I miss something?

Copy link
Member

Choose a reason for hiding this comment

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

To clarify, it only loads all templates for a block template selected when calling npm init @wordpress/block. By default, it will only grab files from lib/templates/esnext folder. At the moment, we use all loaded templates to scaffold the project for the block.

That's good, and largely addresses the concern I had. It was a misinterpretation on my part.

I was thinking about it a little more later in the day yesterday, still misled by the prior concern, but technically still valid: Ideally we try to avoid holding file content in memory longer than necessary. It's something which Node streams excel at, though at a more simple level it could just be a matter of: Only ever having one file loaded in memory at a time, generate the output, move on to the next (rely on garbage collection).

It could have been more of a problem if my previous impression of the implementation was correct, which it's not. Now it may be overkill 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Only ever having one file loaded in memory at a time, generate the output, move on to the next (rely on garbage collection).

We had that before and I liked it more. Here I prematurely assumed that template downloaded from npm would be load into memory and removed instantaneously. Let's see how the final implementation looks like. If we could make it work with npm install my-template --no-save then we could list absolute paths to the individual file instead and that would solve both issues assuming that npm cleans up the node_modules folder when using npm init :)

@gziolo gziolo merged commit f13c1d2 into master May 13, 2020
@gziolo gziolo deleted the update/create-block-glob-templates branch May 13, 2020 08:12
@github-actions github-actions bot added this to the Gutenberg 8.2 milestone May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Create Block /packages/create-block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants