-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.mix.js
172 lines (157 loc) · 4.17 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const mix = require( 'laravel-mix' ),
fs = require( 'node:fs' ),
path = require( 'node: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();
} );
};
/**
* Checks if directory is empty.
* @param {string} dirPath The path to the directory.
* @return {boolean}
*/
const isDirectoryEmpty = function ( dirPath ) {
try {
const files = fs.readdirSync( dirPath );
return files.length === 0;
} catch ( err ) {
return true; // Directory does not exist or there was an error accessing it
}
};
/**
* Checks if file is empty.
* @param {string} filePath The path to the file.
* @return {boolean}
*/
const isFileEmpty = function ( filePath ) {
try {
const stats = fs.statSync( filePath );
return stats.size === 0;
} catch ( err ) {
return true; // File does not exist or there was an error accessing it
}
};
/**
* 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':
if (
! isDirectoryEmpty( path ) &&
! isFileEmpty( `${ path }/${ file }` )
) {
mix.sass( `${ path }/${ file }`, outputPath ).options(
MIX_OPTIONS.styles
);
}
break;
case 'scripts':
if (
! isDirectoryEmpty( path ) &&
! isFileEmpty( `${ path }/${ file }` )
) {
mix.js( `${ path }/${ file }`, outputPath );
}
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.webpackConfig( {
resolve: {
alias: {
'@gemeente-denhaag': path.resolve(
__dirname,
'node_modules/@gemeente-denhaag'
),
},
},
} );
mix.setPublicPath( 'build' ).version().sourceMaps();