This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
webpack.mix.js
154 lines (129 loc) · 3.56 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
const mix = require('laravel-mix');
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
/**
* Paths to module directories
*
* @type {[string,string]}
*/
const moduleDirectories = [
'modules',
'workbench/Collejo/App/Modules',
'vendor/CodeBreez/collejo-app/modules'
];
const publicDir = 'public';
const resourcesDir = 'resources';
/**
* Returns a list of directories from the given path
*
* @param p
* @returns {*}
*/
const dirs = (p) => {
if (!fs.existsSync(p)) {
return [];
}
return fs.readdirSync(p).filter(f => fs.statSync(path.join(p, f)).isDirectory());
};
/**
* Returns a list of files from the given path
*
* @param p
* @returns {*}
*/
const files = (p) => {
if(!fs.existsSync(p)){
return [];
}
return fs.readdirSync(p).filter(f => !fs.statSync(path.join(p, f)).isDirectory() && f.charAt(0) !== '_');
};
const createDirMap = (module, jsDir, sassDir, imagesDir) => {
return {
js: files(jsDir).map(file => {
return {
src: `${jsDir}/${file}`,
dest: `${publicDir}/assets/${module.toLowerCase()}/js/${path.basename(file, '.js')}.js`
};
}),
scss: files(sassDir).map(file => {
return {
src: `${sassDir}/${file}`,
dest: `${publicDir}/assets/${module.toLowerCase()}/css/${path.basename(file, '.scss')}.css`
};
}),
images: files(imagesDir).map(file => {
return {
src: `${imagesDir}/${file}`,
dest: `${publicDir}/images/${module.toLowerCase()}-${path.basename(file)}`
};
})
};
};
/**
* Filter and map the modules paths in to watchable objects
*
* @type {Array}
*/
const fileMap = moduleDirectories.map(directory => {
return dirs(directory).filter(module => {
return fs.lstatSync(`${directory}/${module}`).isDirectory();
}).map(module => {
const modulePath = `${directory}/${module}`;
if (fs.lstatSync(modulePath).isDirectory()) {
const jsDir = `${modulePath}/resources/assets/js`;
const sassDir = `${modulePath}/resources/assets/sass`;
const imagesDir = `${modulePath}/resources/images`;
return createDirMap(module, jsDir, sassDir, imagesDir);
}
});
});
fileMap.push([
{
js: files(`${resourcesDir}/assets/js`).map(file => {
return {
src: `${resourcesDir}/assets/js/${file}`,
dest: `${publicDir}/js/${path.basename(file, '.js')}.js`
};
}),
scss: files(`${resourcesDir}/assets/sass`).map(file => {
return {
src: `${resourcesDir}/assets/sass/${file}`,
dest: `${publicDir}/css/${path.basename(file, '.scss')}.css`
};
})
}
]);
/**
* Webpack configuration
*
* @type {{module: {rules: [null]}, plugins: [null,null]}}
*/
const webpackConfig = {
plugins: [
new webpack.optimize.CommonsChunkPlugin({
minChunks: 2,
name: 'commons',
filename: 'js/commons.js',
})
]
};
/**
* Laravel Mix
*/
fileMap.forEach(dir => {
dir.forEach(module => {
module.js.forEach(file => {
mix.js(file.src, file.dest).version();
});
module.scss.forEach(file => {
mix.sass(file.src, file.dest).version();
});
if(module.images){
module.images.forEach(file => {
mix.copy(file.src, file.dest).version();
});
}
});
});
mix.webpackConfig(webpackConfig);