Repository navigation
Expand file tree
/
Copy pathwebpack.mix.js
More file actions
193 lines (183 loc) · 5.97 KB
/
Copy pathwebpack.mix.js
File metadata and controls
193 lines (183 loc) · 5.97 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require("dotenv").config();
const path = require("path");
const mix = require("laravel-mix");
const environment = process.env.APP_ENV;
const WebpackObfuscation = require("webpack-obfuscator");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const CompressionPlugin = require("compression-webpack-plugin");
const MomentLocalesPlugin = require("moment-locales-webpack-plugin");
const fs = require("fs");
const { execSync } = require("child_process");
const { VuetifyLoaderPlugin } = require("vuetify-loader");
const bObfuscate = "false" !== process.env.WEBPACK_OBFUSCATE;
const bMinify = bObfuscate || "true" === process.env.WEBPACK_MINIFY;
const bCompress = bObfuscate || "true" === process.env.WEBPACK_COMPRESS;
const bAnalyze = "true" === process.env.WEBPACK_ANALYZE;
const bCleanup = bObfuscate || "true" === process.env.WEBPACK_CLEANUP;
const buildFolder = "builds";
const buildFolderFull = path.resolve(__dirname, "public", buildFolder);
const readdirSync = (dir, list = []) => {
if (fs.statSync(dir).isDirectory()) {
fs.readdirSync(dir).map((f) => readdirSync(list[list.push(path.join(dir, f)) - 1], list));
}
return list;
};
const cleanMaps = (folder) => {
console.log("Clean maps: ", folder);
const dirCont = readdirSync(folder);
const files = dirCont.filter((elm) => elm.match(/.*\.js\.map$/gi));
files.forEach((file) => fs.rmSync(file));
};
//List of modules that you want to build, can be overridden at .env file WEBPACK_MODULES variable (comma separated list)
let modules = ["producer"];
if (typeof process.env.WEBPACK_MODULES !== "undefined" && process.env.WEBPACK_MODULES !== "") {
modules = process.env.WEBPACK_MODULES.toString()
.split(",")
.map((s) => s.trim());
}
modules.sort();
console.log("=====================================================");
console.log(" Environment: ", environment);
console.log(" Builds folder: ", buildFolderFull);
console.log(" Modules: ", modules.join(", "));
console.log(" Clean builds: ", bCleanup ? "Yes" : "No");
console.log(" Minify: ", bMinify ? "Yes" : "No");
console.log(" Compress: ", bCompress ? "Yes" : "No");
console.log(" Obfuscate: ", bObfuscate ? "Yes" : "No");
console.log(" Analyze packages: ", bAnalyze ? "Yes" : "No");
console.log("=====================================================");
modules.forEach((moduleName) => {
// pass asset from each module for compilation
require(`./resources/asset/${moduleName}/webpack`)(mix, buildFolder);
mix.copy("./public/fonts", `public/${buildFolder}/${moduleName}/fonts`);
});
let mixOptions = {
processCssUrls: true,
purifyCss: false,
extractStyles: false,
imgLoaderOptions: {
enabled: false,
},
terser: {
extractComments: false,
},
runtimeChunkPath: `${buildFolder}/vendor`,
};
let webpackConfig = {
resolve: {
extensions: [".ts", ".vue", ".js"],
alias: {
"@": path.resolve(__dirname, "./resources/asset"),
},
},
output: {
chunkFilename: `${buildFolder}/chunks/[name].[hash].js`,
},
plugins: [new MomentLocalesPlugin()],
optimization: {
splitChunks: {
chunks: "all",
},
usedExports: true,
sideEffects: true,
mangleWasmImports: true,
mergeDuplicateChunks: true,
minimize: bMinify,
providedExports: true,
removeEmptyChunks: true,
},
performance: {
maxAssetSize: 10000,
},
};
if (bMinify) {
mixOptions.uglify = {
uglifyOptions: {
compress: {
drop_debugger: true,
sequences: true,
booleans: true,
loops: true,
unused: true,
warnings: false,
errors: false,
drop_console: true,
unsafe: false,
comments: false,
},
},
};
webpackConfig.devtool = "source-map";
}
//obfuscate
if (bObfuscate) {
webpackConfig.plugins.push(
new WebpackObfuscation({
sourceMap: false,
sourceMapFileName: "[file].map",
rotateStringArray: true,
rotateUnicodeArray: true,
})
);
webpackConfig.devtool = "hidden-source-map";
}
if (bCompress) {
webpackConfig.plugins.push(
new CompressionPlugin({
algorithm: "gzip",
test: /\.js$|\.ts$|\.css$|\.html$|\.jp2$|\.jpg$|\.jpeg$|\.svg$|\.png$|\.webp$|\.mp3$|\.ico$/,
threshold: 10240,
minRatio: 1,
compressionOptions: {
level: 9,
},
}),
new VuetifyLoaderPlugin({
match(originalTag, { kebabTag, camelTag, path, component }) {
if (kebabTag.startsWith("core-")) {
return [camelTag, `import ${camelTag} from '@/components/core/${camelTag.substring(4)}.vue'`];
}
},
})
);
}
if (bAnalyze) {
webpackConfig.plugins.push(new BundleAnalyzerPlugin());
}
if (bCleanup) {
mix.webpackConfig({
output: {
clean: {
keep: function (asset) {
// remove all files from builds directory. leave hidden files
let m = new RegExp(`^\\/?${buildFolder}\\/(?!($|\\.))`);
return asset.match(m) === null;
},
},
},
});
}
if ("local" !== environment) {
mix.webpackConfig({
stats: {
warnings: false,
},
});
}
// Global webpack config for all bundles
mix.webpackConfig(webpackConfig)
.vue({ version: 2 })
.options(mixOptions)
.extract({
to: `${buildFolder}/vendor/vendor.js`,
libraries: ["vue", "vuex", "vue-router", "vee-validate", "vuetify"],
})
.after((stats) => {
if (bObfuscate) {
cleanMaps(buildFolderFull);
}
})
.disableSuccessNotifications()
.disableNotifications()
.sourceMaps(false, 'source-map')
.version();