Skip to content

Commit 3e727a7

Browse files
Merge pull request #3 from Automattic/update/webpack-config-use-calypso-build
Blocks: Make webpack.config.extensions.js extend calypso-build's
2 parents fd10023 + ea1ef05 commit 3e727a7

File tree

3 files changed

+39
-99
lines changed

3 files changed

+39
-99
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "Gruntfile.js",
55
"author": "Automattic",
66
"devDependencies": {
7-
"@automattic/calypso-build": "^1.0.0-alpha.0",
7+
"@automattic/calypso-build": "^1.0.0-alpha.2",
88
"@automattic/calypso-color-schemes": "^1.0.0-alpha.1",
99
"@babel/core": "^7.4.0",
1010
"@babel/plugin-proposal-class-properties": "^7.4.0",
@@ -21,7 +21,6 @@
2121
"classnames": "^2.2.6",
2222
"copy-webpack-plugin": "^5.0.2",
2323
"css-loader": "^2.1.1",
24-
"duplicate-package-checker-webpack-plugin": "^3.0.0",
2524
"eslint": "^5.16.0",
2625
"file-loader": "^3.0.1",
2726
"grunt": "^0.4.5",

src/setup/blocks.json

-3
This file was deleted.

webpack.config.js

+38-94
Original file line numberDiff line numberDiff line change
@@ -6,109 +6,53 @@
66
/**
77
* External dependencies
88
*/
9-
10-
const _ = require( 'lodash' );
11-
const path = require( 'path' );
129
const fs = require( 'fs' );
13-
const webpack = require( 'webpack' );
14-
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
15-
const DuplicatePackageCheckerPlugin = require( 'duplicate-package-checker-webpack-plugin' );
16-
const FileConfig = require( '@automattic/calypso-build/webpack/file-loader' );
17-
const Minify = require( '@automattic/calypso-build/webpack/minify' );
18-
const SassConfig = require( '@automattic/calypso-build/webpack/sass' );
19-
const TranspileConfig = require( '@automattic/calypso-build/webpack/transpile' );
20-
const wordpressExternals = require( '@automattic/calypso-build/webpack/wordpress-externals' );
10+
const getBaseWebpackConfig = require( '@automattic/calypso-build/webpack.config.js' );
11+
const path = require( 'path' );
12+
13+
/**
14+
* Internal dependencies
15+
*/
16+
// const { workerCount } = require( './webpack.common' ); // todo: shard...
2117

2218
/**
2319
* Internal variables
2420
*/
2521
const editorSetup = path.join( __dirname, 'src', 'setup', 'editor' );
2622
const viewSetup = path.join( __dirname, 'src', 'setup', 'view' );
2723

28-
function blockScripts( type, inputDir, presetBlocks ) {
29-
return presetBlocks
24+
function blockScripts( type, inputDir, blocks ) {
25+
return blocks
3026
.map( block => path.join( inputDir, 'blocks', block, `${ type }.js` ) )
3127
.filter( fs.existsSync );
3228
}
33-
/**
34-
* Return a webpack config object
35-
*
36-
* @return {object} webpack config
37-
*/
38-
function getWebpackConfig() {
39-
const workerCount = 1;
40-
const cssFilename = '[name].css';
41-
42-
const presetPath = path.join( __dirname, 'src', 'setup', 'blocks.json' );
43-
console.log( presetPath );
44-
const presetIndex = require( presetPath );
45-
const presetBlocks = _.get( presetIndex, [ 'production' ], [] );
46-
47-
// Helps split up each block into its own folder view script
48-
const viewBlocksScripts = presetBlocks.reduce( ( viewBlocks, block ) => {
49-
const viewScriptPath = path.join( __dirname, 'src', 'blocks', block, 'view.js' );
50-
if ( fs.existsSync( viewScriptPath ) ) {
51-
viewBlocks[ block + '/view' ] = [ viewSetup, ...[ viewScriptPath ] ];
52-
}
53-
return viewBlocks;
54-
}, {} );
55-
56-
// Combines all the different blocks into one editor.js script
57-
const editorScript = [
58-
editorSetup,
59-
...blockScripts( 'editor', path.join( __dirname, 'src' ), presetBlocks ),
60-
];
61-
62-
const webpackConfig = {
63-
entry: {
64-
editor: editorScript,
65-
...viewBlocksScripts,
66-
},
67-
mode: 'production',
68-
module: {
69-
rules: [
70-
TranspileConfig.loader( {
71-
workerCount,
72-
configFile: path.join( __dirname, 'babel.config.js' ),
73-
cacheDirectory: true,
74-
exclude: /node_modules\//,
75-
} ),
76-
SassConfig.loader( {
77-
preserveCssCustomProperties: false,
78-
includePaths: [ path.join( __dirname, 'client' ) ],
79-
prelude: '@import "~@automattic/calypso-color-schemes/src/shared/colors";',
80-
} ),
81-
FileConfig.loader(),
82-
],
83-
},
84-
output: {
85-
path: path.join( __dirname, 'dist' ),
86-
filename: '[name].js',
87-
libraryTarget: 'window',
88-
},
89-
resolve: {
90-
extensions: [ '.json', '.js', '.jsx' ],
91-
modules: [ 'node_modules' ],
92-
},
93-
plugins: [
94-
new webpack.DefinePlugin( {
95-
'process.env.NODE_ENV': JSON.stringify( process.env.NODE_ENV ),
96-
global: 'window',
97-
} ),
98-
new webpack.IgnorePlugin( /^\.\/locale$/, /moment$/ ),
99-
...SassConfig.plugins( { cssFilename, minify: true } ),
100-
new DuplicatePackageCheckerPlugin(),
101-
new CopyWebpackPlugin( [
102-
{
103-
from: presetPath,
104-
to: 'index.json',
105-
},
106-
] ),
107-
],
108-
externals: [ wordpressExternals, 'wp', 'lodash' ],
109-
};
110-
111-
return webpackConfig;
112-
}
11329

114-
module.exports = getWebpackConfig;
30+
const blocksDir = path.join( __dirname, 'src', 'blocks' );
31+
const blocks = fs
32+
.readdirSync( blocksDir )
33+
.filter( block => fs.existsSync( path.join( __dirname, 'src', 'blocks', block, 'editor.js' ) ) );
34+
35+
// Helps split up each block into its own folder view script
36+
const viewBlocksScripts = blocks.reduce( ( viewBlocks, block ) => {
37+
const viewScriptPath = path.join( __dirname, 'src', 'blocks', block, 'view.js' );
38+
if ( fs.existsSync( viewScriptPath ) ) {
39+
viewBlocks[ block + '/view' ] = [ viewSetup, ...[ viewScriptPath ] ];
40+
}
41+
return viewBlocks;
42+
}, {} );
43+
44+
// Combines all the different blocks into one editor.js script
45+
const editorScript = [
46+
editorSetup,
47+
...blockScripts( 'editor', path.join( __dirname, 'src' ), blocks ),
48+
];
49+
50+
const webpackConfig = getBaseWebpackConfig( null, {
51+
entry: {
52+
editor: editorScript,
53+
...viewBlocksScripts,
54+
},
55+
'output-path': path.join( __dirname, 'dist' ),
56+
} );
57+
58+
module.exports = webpackConfig;

0 commit comments

Comments
 (0)