|
6 | 6 | /**
|
7 | 7 | * External dependencies
|
8 | 8 | */
|
9 |
| - |
10 |
| -const _ = require( 'lodash' ); |
11 |
| -const path = require( 'path' ); |
12 | 9 | 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... |
21 | 17 |
|
22 | 18 | /**
|
23 | 19 | * Internal variables
|
24 | 20 | */
|
25 | 21 | const editorSetup = path.join( __dirname, 'src', 'setup', 'editor' );
|
26 | 22 | const viewSetup = path.join( __dirname, 'src', 'setup', 'view' );
|
27 | 23 |
|
28 |
| -function blockScripts( type, inputDir, presetBlocks ) { |
29 |
| - return presetBlocks |
| 24 | +function blockScripts( type, inputDir, blocks ) { |
| 25 | + return blocks |
30 | 26 | .map( block => path.join( inputDir, 'blocks', block, `${ type }.js` ) )
|
31 | 27 | .filter( fs.existsSync );
|
32 | 28 | }
|
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 |
| -} |
113 | 29 |
|
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