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

Blocks: Make webpack.config.extensions.js extend calypso-build's #3

Merged
merged 7 commits into from
Apr 8, 2019

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Apr 4, 2019

WIP.

Inspired by Automattic/jetpack#11802. The idea is to encapsulate complexity in @automattic/calypso-build and provide a nice enough interface to consumers.

@ockham ockham self-assigned this Apr 4, 2019
@simison
Copy link
Member

simison commented Apr 5, 2019

Does newspack need concept of "beta blocks" and outputting index.json

Could we simplify to:

/**
 * External dependencies
 */
const fs = require( 'fs' );
const getBaseWebpackConfig = require( '@automattic/calypso-build/webpack.config.js' );
const path = require( 'path' );

/**
 * Internal variables
 */
const editorSetup = path.join( __dirname, 'src', 'setup', 'editor' );
const viewSetup = path.join( __dirname, 'src', 'setup', 'view' );

function blockScripts( type, inputDir, blocks ) {
	return blocks
		.map( block => path.join( inputDir, 'blocks', block, `${ type }.js` ) )
		.filter( fs.existsSync );
}

const blocks = require( path.join( __dirname, 'src', 'setup', 'blocks.json' ) );

// Helps split up each block into its own folder view script
const viewBlocksScripts = blocks.reduce( ( viewBlocks, block ) => {
	const viewScriptPath = path.join( __dirname, 'src', 'blocks', block, 'view.js' );
	if ( fs.existsSync( viewScriptPath ) ) {
		viewBlocks[ block + '/view' ] = [ viewSetup, ...[ viewScriptPath ] ];
	}
	return viewBlocks;
}, {} );

// Combines all the different blocks into one editor.js script
const editorScript = [
	editorSetup,
	...blockScripts( 'editor', path.join( __dirname, 'src' ), blocks ),
];

const webpackConfig = getBaseWebpackConfig( null, {
	entry: {
		editor: editorScript,
		...viewBlocksScripts,
	},
	'output-path': path.join( __dirname, 'dist' ),
} );

module.exports = webpackConfig;

Alternatively ditch completely any json file and just read the src folder.

@jeffersonrabb
Copy link
Contributor

Does newspack need concept of "beta blocks" and outputting index.json

I don't think Newspack needs beta blocks, at least not for the foreseeable future. If I understand correctly, Jetpack/WPCOM beta blocks were conceived primarily to allow for testing on WPCOM before a block is ready for release. If true, the concept wouldn't really be necessary since Newspack is purely a plugin. Does this thinking sound correct or am I forgetting some aspect of the rationale behind beta blocks?

@jeffersonrabb
Copy link
Contributor

Alternatively ditch completely any json file and just read the src folder.

I like this approach. I tried removing src/setup/blocks.json and replacing the require() with:

const blocks = fs
  .readdirSync( path.join( __dirname, 'src', 'blocks' ) )
  .filter( block => fs.existsSync( path.join( __dirname, 'src', 'blocks', block, 'editor.js' ) ) );

How's this look?

@jeffersonrabb
Copy link
Contributor

What do these recent changes mean for Babel? Can we remove babel.config.js and all of the @babel/ dependencies? I tested this, and it seemed to work, although for some reason I had to keep the following dependencies in to avoid errors:

"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-export-default-from": "^7.2.0",
 "@babel/plugin-proposal-export-namespace-from": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-react": "^7.0.0",

@simison
Copy link
Member

simison commented Apr 6, 2019

Does this thinking sound correct or am I forgetting some aspect of the rationale behind beta blocks?

Yeah, I think the need for beta blocks. came from dealing with several environments. Folks should be able to merge small PRs early on and continue working on blocks even before they're published to production. I'd imagine in a plugin like this you'd control release by simply versioning the release, while Calypso and wpcom are continuously delivered.

@jeffersonrabb
Copy link
Contributor

What's the best strategy for controlling the isDevelopment variable? The output is un-minified which looks to be because isDevelopment is always going to be true outside of a Jetpack build environment. How would I override within this repo?

const path = require( 'path' );

/**
* Internal dependencies
Copy link
Contributor

Choose a reason for hiding this comment

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

@ockham I'm curious why you put the Internal dependencies block back in even though the only line is commented out? Was this just a copy/paste from Jetpack or was it meant to be a reminder that the workerCount line will be significant in this repo too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ockham I'm curious why you put the Internal dependencies block back in even though the only line is commented out? Was this just a copy/paste from Jetpack or was it meant to be a reminder that the workerCount line will be significant in this repo too?

Both 🙂 It's in JP too, and the reason in both cases is that I haven't entirely thought through what to do with it. 😬

@simison
Copy link
Member

simison commented Apr 6, 2019

Without looking at code; does NODE_ENV=production npm run build work?

Adding --watch should work, too, as well other webpack cli stuff. (or maybe -- --watch to get pass NPM CLI.

@jeffersonrabb
Copy link
Contributor

Without looking at code; does NODE_ENV=production npm run build work?

Confirmed. NODE_ENV=production npm run build -- --watch gets us minified output as well as file watching.

@ockham
Copy link
Contributor Author

ockham commented Apr 8, 2019

Does newspack need concept of "beta blocks" and outputting index.json

I don't think Newspack needs beta blocks, at least not for the foreseeable future. If I understand correctly, Jetpack/WPCOM beta blocks were conceived primarily to allow for testing on WPCOM before a block is ready for release. If true, the concept wouldn't really be necessary since Newspack is purely a plugin. Does this thinking sound correct or am I forgetting some aspect of the rationale behind beta blocks?

No, that pretty much sums it up. I agree that removing the beta blocks makes sense for Newspack 👍

@ockham
Copy link
Contributor Author

ockham commented Apr 8, 2019

Alternatively ditch completely any json file and just read the src folder.

I like this approach. I tried removing src/setup/blocks.json and replacing the require() with:

const blocks = fs
  .readdirSync( path.join( __dirname, 'src', 'blocks' ) )
  .filter( block => fs.existsSync( path.join( __dirname, 'src', 'blocks', block, 'editor.js' ) ) );

How's this look?

Looks good 👍

You might not even need that though. The original motivation for iterating over individual blocks was that we wanted to be able to build individual JS bundles (i.e. entrypoints) for each block's view.js -- but that might not be a concern for Newspack.

For a single-entrypoint bundle, with no concept of beta blocks, Newspack might not need a webpack.config.js of its own but can maybe directly use @calypso-build's. This is what o2-blocks in Calypso now do: https://github.com/Automattic/wp-calypso/blob/15955b5bced579198c3f6784a97984ff84f43245/packages/o2-blocks/package.json#L22

@ockham
Copy link
Contributor Author

ockham commented Apr 8, 2019

What do these recent changes mean for Babel? Can we remove babel.config.js and all of the @babel/ dependencies? I tested this, and it seemed to work, although for some reason I had to keep the following dependencies in to avoid errors:

"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-export-default-from": "^7.2.0",
 "@babel/plugin-proposal-export-namespace-from": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-react": "^7.0.0",

We can at least heavily simplify babel.config.js by basing it on @automattic/calypso-build's (and removing those explicit dependencies), akin to Automattic/jetpack#11949

@jeffersonrabb
Copy link
Contributor

You might not even need that though. The original motivation for iterating over individual blocks was that we wanted to be able to build individual JS bundles (i.e. entrypoints) for each block's view.js -- but that might not be a concern for Newspack.

For a single-entrypoint bundle, with no concept of beta blocks, Newspack might not need a webpack.config.js of its own but can maybe directly use @calypso-build's. This is what o2-blocks in Calypso now do: https://github.com/Automattic/wp-calypso/blob/15955b5bced579198c3f6784a97984ff84f43245/packages/o2-blocks/package.json#L22

I'm going to save this for a future PR.

@jeffersonrabb
Copy link
Contributor

We can at least heavily simplify babel.config.js by basing it on @automattic/calypso-build's (and removing those explicit dependencies), akin to Automattic/jetpack#11949

Per discussion with @ockham, going to address Babel config reduction in a future PR.

Copy link
Contributor

@jeffersonrabb jeffersonrabb left a comment

Choose a reason for hiding this comment

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

This is great. Thank you @ockham for making this possible!

@jeffersonrabb jeffersonrabb merged commit 3e727a7 into master Apr 8, 2019
@jeffersonrabb jeffersonrabb deleted the update/webpack-config-use-calypso-build branch April 8, 2019 23:25
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants