-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
81 lines (75 loc) · 2.08 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
**** WARNING: No ES6 modules here. Not transpiled! ****
*/
/* eslint-disable import/no-nodejs-modules, @typescript-eslint/no-var-requires */
/**
* External dependencies
*/
const fs = require( 'fs' );
const getBaseWebpackConfig = require( 'newspack-scripts/config/getWebpackConfig' );
const path = require( 'path' );
const { exec } = require( 'child_process' );
/**
* Internal variables
*/
const frontEndDir = path.join( __dirname, 'src', 'js', 'front-end' );
const frontEnd = fs
.readdirSync( frontEndDir )
.filter( asset => /.(j|t)sx?$/.test( asset ) )
.reduce(
( acc, filename ) => ( {
...acc,
[ filename.replace( /\.[^/.]+$/, '' ) ]: path.join(
__dirname,
'src',
'js',
'front-end',
filename
),
} ),
{}
);
const blocks = fs
.readdirSync( path.join( __dirname, 'includes', 'blocks' ) )
.reduce( ( acc, asset ) => {
if ( fs.lstatSync( path.join( __dirname, 'includes', 'blocks', asset ) ).isDirectory() ) {
fs.readdirSync( path.join( __dirname, 'includes', 'blocks', asset ) )
.filter( file => /\.(j|t)sx?$/.test( file ) )
.forEach( file => {
const name = file.replace( /\.[^/.]+$/, '' );
acc[ `${ asset }-${ name }` ] = path.join( __dirname, 'includes', 'blocks', asset, file );
} );
}
return acc;
}, {} );
const editor = path.join( __dirname, 'src', 'js', 'editor' );
const style = [ path.join( __dirname, 'src', 'scss' ) ];
const webpackConfig = getBaseWebpackConfig(
{
entry: { editor, ...frontEnd, ...blocks, style },
output: {
path: path.join( __dirname, 'dist' ),
}
}
);
// Copy built CSS files to the root of the theme. See: https://stackoverflow.com/questions/30312715/run-command-after-webpack-build
webpackConfig.plugins.push(
{
apply: compiler => {
compiler.hooks.afterEmit.tap( 'AfterEmitPlugin', () => {
exec( 'cp ./dist/*.css ./', ( err, stdout, stderr ) => {
if ( err ) {
process.stderr.write( err );
}
if ( stdout ) {
process.stdout.write( stdout );
}
if ( stderr ) {
process.stderr.write( stderr );
}
});
});
}
}
);
module.exports = webpackConfig;