-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.mix.js
138 lines (121 loc) · 3.64 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
const mix = require( 'laravel-mix' ),
fs = require( 'fs' ),
path = require( 'path' );
const ALLOWED_FILES = ['.scss', '.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 build_blocks = (folder, outputFolder = folder) => {
Array.from( getDirectories( folder ) ).forEach( (blockDir) => {
build_files( blockDir, `${folder}/${blockDir}`, `${outputFolder}/${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 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 )
.vue( {version: 3, extractStyles: true} )
}
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 build_client = (folder, outputFolder = folder) => {
Array.from( getDirectories( folder ) ).forEach( (typeDir) => {
build_files( typeDir, `${folder}/${typeDir}`, outputFolder );
const directories = Array.from( getDirectories( `${folder}/${typeDir}` ) );
if (directories.length > 0) {
directories.forEach( (dir) => build_files( typeDir, `${folder}/${typeDir}/${dir}`, `${outputFolder}/${dir}` ) );
}
} );
};
build_blocks( 'src', '' );
mix
.setPublicPath( 'build' )
.version()
.sourceMaps();