-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.mix.js
More file actions
135 lines (105 loc) · 3.7 KB
/
webpack.mix.js
File metadata and controls
135 lines (105 loc) · 3.7 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
const fs = require('fs');
const mix = require('laravel-mix');
const cssnano = require('cssnano');
const tailwindcss = require('tailwindcss');
const autoprefixer = require('autoprefixer');
const postcssImport = require('postcss-import');
const { components } = require('./components.config.js');
const tailwindcssNesting = require('tailwindcss/nesting');
require('laravel-mix-criticalcss');
const sassPlugins = [
postcssImport(),
tailwindcssNesting(),
tailwindcss('tailwindcss.config.js'),
autoprefixer(),
];
if (mix.inProduction()) {
sassPlugins.push(cssnano());
}
const uid = () => {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 16; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
const readAppend = async (file, appendFile) =>
new Promise((resolve, reject) =>
fs.readFile(appendFile, function (error, data) {
if (error) reject(error);
fs.appendFile(file, data, function (error) {
if (error) reject(error);
resolve();
});
})
);
mix.webpackConfig({
stats: {
children: false
}
});
mix.disableNotifications();
mix.setPublicPath(process.env.MIX_WEB_ROOT);
// App Helpers
mix.js('src/js/event-listeners.js', `${process.env.MIX_WEB_ROOT}/assets/js/event-listeners.js`);
mix.copy('src/icons', `${process.env.MIX_WEB_ROOT}/assets/icons`);
mix.copy('src/fonts', `${process.env.MIX_WEB_ROOT}/assets/fonts`);
mix.copy('src/img', `${process.env.MIX_WEB_ROOT}/assets/img`);
if (process.env.LOAD === 'all') {
// IF LOADING ALL COMPONENTS UPON PAGE LOAD, WE COMBINE MAIN APP AND COMPONENTS SCSS TO GENERATE SINGLE CSS FILE, MOVING THE FILE INTO THE PUBLIC FOLDER
const combinedScss = 'src/scss/app-combined.scss';
mix.before(async () => {
await readAppend(combinedScss, 'src/scss/app.scss');
for (let i = 0; i < components.length; i++) {
await readAppend(combinedScss, `templates/_components/${components[i]}/${components[i]}.scss`);
}
});
mix.sass(combinedScss, 'assets/css/app.css', {}, sassPlugins);
} else {
// IF LOADING COMPONENTS ON-DEMAND, WE GENERATE EACH INDIVIDUAL SCSS INTO CSS, MOVING THE FILES INTO THE PUBLIC FOLDER
mix.sass('src/scss/app.scss', 'assets/css/app.css', {}, sassPlugins);
components.forEach(component => mix.sass(`templates/_components/${component}/${component}.scss`, `assets/css/${component}.css`, {}, sassPlugins));
}
if (process.env.LOAD === 'all') {
// IF LOADING ALL COMPONENTS UPON PAGE LOAD, WE COMBINE MAIN APP AND COMPONENTS JS TO GENERATE SINGLE JS FILE, MOVING THE FILE INTO THE PUBLIC FOLDER
const scripts = [];
scripts.push('src/js/app.js');
components.map(component => scripts.push(`templates/_components/${component}/${component}.js`));
mix.js(scripts, `${process.env.MIX_WEB_ROOT}/assets/js/app.js`);
} else {
// IF LOADING COMPONENTS ON-DEMAND, WE GENERATE EACH INDIVIDUAL JS FILE, MOVING THE FILE INTO THE PUBLIC FOLDER
mix.js('src/js/app.js', `${process.env.MIX_WEB_ROOT}/assets/js/app.js`);
components.forEach(component => mix.js(`templates/_components/${component}/${component}.js`, `${process.env.MIX_WEB_ROOT}/assets/js/${component}.js`));
}
mix.extract();
if (mix.inProduction()) {
const criticalCssUrls = [];
criticalCssUrls.push(
{
url: '/',
template: 'index',
},
);
mix.criticalCss({
enabled: true,
paths: {
base: process.env.PRIMARY_SITE_URL,
templates: `${process.env.MIX_WEB_ROOT}/assets/css/`,
suffix: '.critical',
},
urls: criticalCssUrls,
options: {
minify: true,
extract: false,
inline: false,
inlineImages: false,
width: 1600,
height: 1200,
penthouse: {
timeout: 1200000,
},
},
});
}
mix.after(() => fs.writeFileSync('templates/buildVersion.twig', uid()));