Skip to content

Gutenpack: Break up the Jetpack preset's view bundle into separate, per-block ones. - #28066

Merged
enejb merged 16 commits into
masterfrom
add/tiled-galleries-to-create-sepeate-bundle
Oct 30, 2018
Merged

enejb merged 16 commits into
masterfrom
add/tiled-galleries-to-create-sepeate-bundle

Conversation

@enejb

@enejb enejb commented Oct 24, 2018 •

Copy link
Copy Markdown
Member

Changes proposed in this Pull Request

  • make it so that we can build tile-galleries as a separate bundle.

Testing instructions

run the usual command that builds the bundle.
npm run sdk gutenberg client/gutenberg/extensions/presets/jetpack -- --output-dir="../jetpack/_inc/blocks"

it should produce the following files.

editor.js
editor.css
editor.rtl.css
tiled-gallery/view.js
tiled-gallery/view.css
tiled-gallery/view.rtl.css

@matticbot

Copy link
Copy Markdown
Contributor

@enejb
enejb requested review from ockham and tyxla and removed request for ockham October 24, 2018 20:30
@enejb enejb self-assigned this Oct 24, 2018
@enejb
enejb requested review from ockham, simison and sirreal October 24, 2018 21:56
@enejb enejb added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. [Project] SDK labels Oct 24, 2018
@lezama
lezama requested a review from a team October 24, 2018 22:42
@enejb
enejb force-pushed the add/tiled-galleries-to-create-sepeate-bundle branch from 2c1e318 to 9a4abb0 Compare October 25, 2018 18:47
import 'gutenberg/extensions/markdown/editor';
//import 'gutenberg/extensions/publicize/editor';
import 'gutenberg/extensions/related-posts/editor';
import 'gutenberg/extensions/tiled-gallery/editor';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This preset file is imported in Calypso:

import 'gutenberg/extensions/presets/jetpack/editor.js';

You might want to either keep it (and perhaps rename) or import each extension separately in client/gutenberg/editor/edit-post/editor.js.

Comment thread bin/sdk/gutenberg.js Resolved
Comment thread bin/sdk/gutenberg.js Outdated
const presetBlocks = require( presetPath );

// Helps split up each block into its own folder view script
viewBlocksPoints = reduce( presetBlocks, (obj, block) => ( { [ block + '/view'] : [path.join( inputDir, '../../'+ block +'/view.js' )] } ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is this reduce supposed to do? obj is never used, so the whole call will return the returned value for the last block?

Maybe you want this?

fromPairs( map(
  blocks,
  block => [ block + '/view', path.join( ..., block, 'view.js' ) ]
) );

@simison
simison requested a review from a team October 26, 2018 19:00
@@ -0,0 +1 @@
[ "markdown", "related-posts","tiled-gallery" ]

@simison simison Oct 26, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remember to rebase, publicize extension is now enabled in Jetpack preset as well.

Nit; that missing space. :-)

Comment thread bin/sdk/gutenberg.js Outdated
editorScripts = presetBlocks.map( block => path.join( inputDir, '../../'+ block +'/editor.js' ) ).filter( fs.existsSync );
viewScripts = presetBlocks.map( block => path.join( inputDir, '../../'+ block +'/view.js' ) ).filter( fs.existsSync );
// Combines all the different blocks into one Edit script
editorScript = utilScripts.concat( editorScripts ).concat( viewScripts );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that view scripts in client/gutenberg/extensions/presets/jetpack/view.js depend on utils/public-path.js as well.

@enejb
enejb force-pushed the add/tiled-galleries-to-create-sepeate-bundle branch from 633d660 to 602d5f2 Compare October 26, 2018 20:44
@ockham

ockham commented Oct 27, 2018 •

Copy link
Copy Markdown
Contributor

To provide a bit of rationale here (mostly discussed with @enejb and @lezama on a video call and screenshare):

The goal here is to break up the Jetpack preset's view bundle into separate, per-block ones, so they can be conditionally loaded by Jetpack depending on the corresponding modules' activation status -- from PHP, i.e. through wp_enqueue_script().

To reach this goal, I suggested using separate webpack entry points for each block's `view.

We're now tasked with producing the list of those files from a given preset configuration.

We discussed that our current preset format -- Javascript files that simply import individual blocks -- isn't particularly suited to this task: We would essentially need to parse JavaScript -- and even if we settled for a very basic, RegEx-based parser, those presets might also import util files that we don't want to create separate entry points for.

We also observed that our current presets configuration is somewhat redundant -- both a preset's editor.js, and its view.js essentially import each of the preset's blocks' editor.js and view.js files, respectively.

The solution that we came up with is what started this PR: Use a JSON file that only contains an array of block directory names. Iterate over this list to create both the list of view file entry points, and dynamically create an editor.js file in essentially the same format that we have now.

There are two tradeoffs AFAICS:

  • Previously, with presets just being Javascript files that imported blocks, the SDK didn't care if it was dealing with a preset, or an individual block -- the build process would work regardless. With JSON-based presets, we need build different logic for presets (=== this PR).
  • We're introducing yet more conventions here, namely:
    • Block directories are expected to be at the same file system hierarchy level as the presets/ directory. (This requirement could probably be removed by changing our preset JSONs from arrays to objects, whose keys are block names, and whose values are paths. IMO: Convention over configuration, and YAGNI.)
    • Shared util files are expected to be in the preset's util/ subdirectory.

@simison simison added the [Pri] High Address as soon as possible after BLOCKER issues label Oct 29, 2018
@simison
simison requested review from a team October 29, 2018 09:52
@simison

simison commented Oct 29, 2018

Copy link
Copy Markdown
Member

Noting that while the title mentions tiled gallery, the solution is built for all the blocks. The tiled gallery just happens to be the only one with view+editor side scripts and styles right now.

@lezama lezama changed the title Gutenpack: seperate the tiled-gallery into its own view bundle Gutenpack: Break up the Jetpack preset's view bundle into separate, per-block ones. Oct 29, 2018

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems to be testing well, nice work 💯

I've left some comments, mostly regarding code style - nothing major.

Also, I've noticed that I don't get any view scripts/styles in the root build directory - is that because tiled galleries is the only use case we have right now?

Comment thread bin/sdk/gutenberg.js Resolved
Comment thread bin/sdk/gutenberg.js Resolved
Comment thread bin/sdk/gutenberg.js Resolved
Comment thread bin/sdk/gutenberg.js Resolved
Comment thread bin/sdk/gutenberg.js Resolved
Comment thread docs/sdk.md Outdated
They can be found in `client/gutenberg/extensions/presets` directory.

To create a new preset, create a new folder in that directory and add an `index.json` file.
The file should be an array an array of the extensions folder names that you want to bundle together.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We seem to have written an array twice.

Comment thread bin/sdk/gutenberg.js Outdated
if ( fs.existsSync( presetPath ) ) {
const presetBlocks = require( presetPath );
// Find all the utils scripts
utilScripts = fs.readdirSync( utilsPath ).map( utilFile => path.join( utilsPath, utilFile ) ).filter( fs.existsSync );

@simison simison Oct 29, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that in #28088 we introduced gutenberg/extensions/presets/jetpack/utils/i18n which is imported from each block separately, instead just once in the preset file. Remember to rebase this PR to see how it affects things.

Should we refactor utility-files by moving them to /shared and import them in each block that depends on them? That way it'll be easier to build each block separately with the SDK without relying on preset importing things such as jetpack category for us.

The purpose of the preset would thus be only to define combination of extensions; not new functionality per-se.

See also #27836 which adds /shared utility folder.

@simison simison Oct 29, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ohh funny, looks like I commented almost identical thing in resolved convo above this line 😆 https://github.com/Automattic/wp-calypso/pull/28066/files#r228319972 — sorry I missed that.

Introduction of utils/i18n changes the situation again quite a bit, though.

Comment thread bin/sdk/gutenberg.js Outdated
} );

editorScripts = presetBlocks.map( block => path.join( inputDir, '../../'+ block +'/editor.js' ) ).filter( fs.existsSync );
viewScripts = presetBlocks.map( block => path.join( inputDir, '../../'+ block +'/view.js' ) ).filter( fs.existsSync );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that utilScripts, editorScripts, viewScripts and editorScript seem to be missing const

@simison

simison commented Oct 29, 2018 •

Copy link
Copy Markdown
Member

Many of the styling issues seem to be coming from the fact that /bin/ is ignored by eslint. :-(

You might want to run npx eslint bin/sdk/gutenberg.js --no-ignore manually to make sure all is good.

@simison

simison commented Oct 29, 2018 •

Copy link
Copy Markdown
Member

By running:

npm run sdk -- gutenberg client/gutenberg/extensions/presets/jetpack/ --output-dir=~/jetpack/_inc/blocks/

I'm getting:

_inc/blocks/
├── editor.css
├── editor.js
├── editor.rtl.css
└── tiled-gallery
    ├── view.css
    ├── view.js
    └── view.rtl.css

While current Jetpack master expects also view.js in the root: the PR description doesn't mention Automattic/jetpack#10405 but I'm assuming these both should be merged at the same time?

Heads up @oskosk that Jurassic Ninja might need master branch of Jetpack for each build for a while again, because asset paths have changed.

@simison simison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Building the o2 preset seems to be broken right now:

npm run sdk -- gutenberg client/gutenberg/extensions/presets/o2 --output-dir=~/test

As well as building individual blocks:

npm run sdk -- gutenberg client/gutenberg/extensions/tiled-gallery --output-dir=~/test

Building via bin script seems to work well — to test, type npm link in Calypso folder and then from Jetpack folder run:

calypso-sdk gutenberg ~/wp-calypso/client/gutenberg/extensions/presets/jetpack --output-dir=./_inc/blocks

@enejb
enejb force-pushed the add/tiled-galleries-to-create-sepeate-bundle branch from ba89961 to 0fe6f77 Compare October 29, 2018 20:53
@enejb
enejb dismissed simison’s stale review October 29, 2018 20:56

Fixed the code so that it runs with all 3 commands as expected

@enejb

enejb commented Oct 29, 2018

Copy link
Copy Markdown
Member Author

@tyxla and @simison
I updated the PR so that we don't create view.js and view.css files.

editor.js
editor.css
editor.rtl.css
tiled-gallery/view.js
tiled-gallery/view.css
tiled-gallery/view.rtl.css

Since view.css and js files should just be included in the editor. (js and css) files. No need to load them separately.

I also created a shared that contains the js files that should be included with each bundle.
and editor-shared folder that should contain only the editor shared includes.

enejb and others added 2 commits October 29, 2018 20:09
```
npx prettier --write \
  bin/sdk/gutenberg.js \
  docs/sdk.md \
  client/gutenberg/extensions/presets/jetpack/utils/README.md
```
Comment thread bin/sdk/gutenberg.js Resolved

@simison simison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tested different ways to use the SDK (building all different presets, building a single block, building using the npm script and the bin command) and everything worked as expected.

Nice job driving this pretty big change through while keeping backwards compatibility! 👍

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Test well for me 👍 Left a couple of minor suggestions, other than that LGTM 🚢

Comment thread bin/sdk/gutenberg.js Resolved
Comment thread bin/sdk/gutenberg.js Resolved
@enejb
enejb merged commit 3f273eb into master Oct 30, 2018
@enejb
enejb deleted the add/tiled-galleries-to-create-sepeate-bundle branch October 30, 2018 18:13
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Pri] High Address as soon as possible after BLOCKER issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants