-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.mix.js
102 lines (82 loc) · 2.94 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
const mix = require('laravel-mix');
const fs = require('fs');
const { resolve } = require('path');
const config = require('./webpack.mix.config');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const plugins = [];
if (mix.inProduction()) {
plugins.push(new CleanWebpackPlugin());
}
const directoryPath = resolve(__dirname, `${config.themeDir}/src/scss/entry-points`);
const getFiles = (dir) => fs.readdirSync(dir).filter((file) => /\.[scss|css|tsx|ts|js]/.test(file));
mix.setPublicPath(config.distDir);
mix.setResourceRoot('../');
const mainJs = mix.js(`${config.themeDir}/src/js/main.js`, 'js');
mainJs.js(`${config.themeDir}/src/admin/js/admin.js`, 'js');
const sassOptions = {
sassOptions: {
precision: 8,
quietDeps: true,
outputStyle: 'expanded',
},
};
const mainStyle = mix.sass(`${config.themeDir}/src/scss/main.scss`, 'css', sassOptions);
mainStyle.sass(`${config.themeDir}/src/admin/scss/admin.scss`, 'css', sassOptions);
mainStyle.sass(`${config.themeDir}/src/scss/bootstrap-grid.scss`, 'css', sassOptions);
getFiles(directoryPath).forEach((filepath) => mainStyle.sass(`${directoryPath}/${filepath}`, 'css', sassOptions));
mix.extend('rewriteRules', (webpackConfig) => {
const fontsRule = /(\.(woff2?|ttf|eot|otf)$|font.*\.svg$)/;
const imagesRule = /(\.(png|jpe?g|gif|webp)$|^((?!font).)*\.svg$)/;
const fonts = webpackConfig.module.rules.find((rule) => String(rule.test) === String(fontsRule));
const images = webpackConfig.module.rules.find((rule) => String(rule.test) === String(imagesRule));
if (fonts && fonts.use && fonts.use[0]) {
fonts.use[0].options.name = `${Config.assetDirs.fonts}/[name]-[contenthash].[ext]`;
}
if (images && images.use && images.use[0]) {
images.use[0].options.name = `${Config.assetDirs.images}/[name]-[hash].[ext]`;
}
});
mix.rewriteRules();
mix.webpackConfig((webpack) => {
return {
output: {
publicPath: `${config.publicPath}/`,
chunkFilename: 'js/[name].[contenthash].js',
},
externals: config.externals,
watchOptions: {
ignored: /node_modules/,
},
plugins,
devtool: 'source-map',
stats: {
children: true,
},
};
});
if (!mix.inProduction()) {
mix.sourceMaps();
}
mix.disableNotifications();
mix.disableSuccessNotifications();
mix.options({
processCssUrls: true,
terser: {
parallel: true,
// cache: true,
terserOptions: {
compress: mix.inProduction(),
},
},
});
if (mix.inProduction()) {
mix.version();
mix.then(async () => {
const convertToFileHash = require('laravel-mix-make-file-hash');
const fileHashedManifest = await convertToFileHash({
publicPath: `${config.distDir}`,
blacklist: ['/js/style/*', '*.map'],
manifestFilePath: `${config.distDir}/mix-manifest.json`,
});
});
}