-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.mix.js
111 lines (96 loc) · 3.14 KB
/
webpack.mix.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const mix = require( 'laravel-mix' ),
fs = require( 'fs' ),
path = require( 'path' );
const ALLOWED_FILES = [ '.scss', 'client.js' ];
const MIX_OPTIONS = {
styles: {
outputStyle: 'compressed'
}
};
/**
* Get all files from specific directory.
* @param {string} directory path to file from root.
* @return {string[]}
*/
const getFiles = function ( directory ) {
return fs.readdirSync( directory ).filter( file => {
return ( ALLOWED_FILES.includes( path.extname( file ) ) || ALLOWED_FILES.includes( file ) ) ? fs.statSync( `${ directory }/${ file }` ).isFile() : false;
} );
};
/**
* Get all directories from a specific directory.
* @param {string} directory The directory to check.
* @return {string[]}
*/
const getDirectories = function ( directory ) {
return fs.readdirSync( directory ).filter( function ( file ) {
return fs.statSync( path.join( directory, file ) ).isDirectory();
} );
};
/**
* Loop through the community block directories and block directories and build any files necessary.
*
* @param {string} folder name of the folder to scan.
* @param {string} outputFolder name of the folder to output.
* @constructor
*/
const NCB_build_blocks = ( folder, outputFolder = folder ) => {
Array.from( getDirectories( folder ) ).forEach( ( companyDir ) => {
Array.from( getDirectories( `${ folder }/${ companyDir }` ) ).forEach( ( blockDir ) => {
Array.from( getDirectories( `${ folder }/${ companyDir }/${ blockDir }/assets/` ) ).forEach( ( typeDir ) => {
NCB_build_files( typeDir, `${ folder }/${ companyDir }/${ blockDir }/assets/${ typeDir }`, `${ outputFolder }/${ companyDir }/${ blockDir }` );
} );
} );
} );
};
/**
* Little helper to reduce duplication.
*
* @param {string} typeDir The type of script directory.
* @param {string} path The path of the input directory.
* @param outputPath The path of the output directory.
* @constructor
*/
const NCB_build_files = ( typeDir, path, outputPath = path ) => {
const files = getFiles( path );
if( 0 === files.length) {
return;
}
files.forEach( ( file ) => {
switch ( typeDir ) {
case 'styles':
mix.sass(
`${ path }/${ file }`,
outputPath
).options( MIX_OPTIONS.styles );
break;
case 'scripts':
mix.js( `${ path }/${ file }`, outputPath ).vue({ version: 3 });
break;
}
} );
};
/**
* Loop through the client directory and build any files necessary.
*
* @param {string} folder name of the folder to scan.
* @param {string} outputFolder name of the folder to output.
* @constructor
*/
const NCB_build_client = ( folder, outputFolder = folder ) => {
Array.from( getDirectories( folder ) ).forEach( ( typeDir ) => {
NCB_build_files( typeDir, `${ folder }/${ typeDir }`, outputFolder );
const directories = Array.from( getDirectories( `${ folder }/${ typeDir }` ) );
if ( directories.length > 0 ) {
directories.forEach( ( dir ) => NCB_build_files( typeDir, `${ folder }/${ typeDir }/${ dir }`, `${ outputFolder }/${ dir }` ) );
}
} );
};
NCB_build_blocks( 'src/blocks', 'blocks' );
NCB_build_client( 'src/client', 'client' );
mix.setPublicPath( 'build' )
.version()
.sourceMaps()
.options( {
cssnano: { preset: "default" },
} );