1
+ const { mix } = require ( 'laravel-mix' )
2
+ const Mix = mix . config
3
+ const glob = require ( 'glob' )
4
+ const argv = require ( 'yargs' ) . argv
5
+ const del = require ( 'del' )
6
+ const merge = require ( 'webpack-merge' ) . smart
7
+ let webpackConfig = { }
8
+
9
+ ///////////////////////////////////////////
10
+ // RUN SPECIFIC TASKS //
11
+ // npm run development -- --env.run html //
12
+ // npm run development -- --env.run js //
13
+ // npm run development -- --env.run sass //
14
+ // npm run development -- --env.run copy //
15
+ ///////////////////////////////////////////
16
+
17
+ const __RUN = argv . env ? argv . env . run : undefined
18
+
19
+ /////////////
20
+ // CLEANUP //
21
+ /////////////
22
+
23
+ if ( __RUN === 'clean' || ! __RUN ) {
24
+ del . sync ( [
25
+ 'dist/**/*.html' ,
26
+ 'dist/assets/{css,fonts,js,vendor}'
27
+ ] )
28
+ }
29
+
30
+ ////////
31
+ // JS //
32
+ ////////
33
+
34
+ // npm run development -- --env.run js
35
+ if ( __RUN === 'js' || ! __RUN ) {
36
+ for ( let file of glob . sync ( './src/js/**/**.{js,vue}' , { ignore : '**/_*' } ) ) {
37
+ mix . js ( file , './dist/assets/js' )
38
+ }
39
+ }
40
+
41
+ ////////////////////////
42
+ // COPY VENDOR ASSETS //
43
+ // from node_modules //
44
+ ////////////////////////
45
+
46
+ // npm run development -- --env.run copy
47
+ if ( __RUN === 'copy' || ! __RUN ) {
48
+ try {
49
+ require ( path . join ( process . cwd ( ) , 'copy-vendor-assets.json' ) ) . forEach ( function ( asset ) {
50
+ var dest = path . join ( process . cwd ( ) , 'dist/assets/vendor' )
51
+ var src = asset
52
+ if ( asset instanceof Object ) {
53
+ src = Object . keys ( asset ) . pop ( )
54
+ dest = Object . values ( asset ) . pop ( )
55
+ }
56
+ for ( let file of glob . sync ( src , { cwd : path . join ( process . cwd ( ) , 'node_modules/' ) } ) ) {
57
+ mix . copy ( path . join ( process . cwd ( ) , 'node_modules' , file ) , dest )
58
+ }
59
+ } )
60
+ }
61
+ catch ( e ) { }
62
+ }
63
+
64
+ //////////
65
+ // SASS //
66
+ //////////
67
+
68
+ // npm run development -- --env.theme dark
69
+ const __THEME = argv . env ? argv . env . theme || 'default' : 'default'
70
+
71
+ // npm run development -- --env.run sass
72
+ if ( __RUN === 'sass' || ! __RUN ) {
73
+
74
+ let sassOptions = {
75
+ importer : require ( 'sass-importer-npm' ) ,
76
+ // inject $theme variable
77
+ data : '$theme: ' + __THEME + ';'
78
+ }
79
+
80
+ mix . options ( {
81
+ // ignore fonts
82
+ processCssUrls : false
83
+ } )
84
+
85
+ for ( let file of glob . sync ( 'src/sass/*.scss' , { ignore : '**/_*' } ) ) {
86
+ mix . sass ( file , './dist/assets/css/themes/' + __THEME , sassOptions )
87
+ }
88
+ }
89
+
90
+ /////////
91
+ // RTL //
92
+ /////////
93
+
94
+ // npm run development -- --env.run sass
95
+ if ( __RUN === 'sass' || ! __RUN ) {
96
+ const WebpackRTLPlugin = require ( 'webpack-rtl-plugin' )
97
+ const WebpackRTLWrapPlugin = require ( 'webpack-rtl-wrap-plugin' )
98
+ const cacheDirectory = path . resolve ( path . join ( 'temp' , 'rtl' , __THEME ) )
99
+
100
+ del . sync ( cacheDirectory )
101
+
102
+ webpackConfig = merge ( webpackConfig , {
103
+ plugins : [
104
+ // Creates .rtl.css
105
+ new WebpackRTLPlugin ( {
106
+ minify : false
107
+ } ) ,
108
+ // wraps CSS into [dir=ltr|rtl]
109
+ new WebpackRTLWrapPlugin ( {
110
+ cacheDirectory
111
+ } )
112
+ ]
113
+ } )
114
+ }
115
+
116
+ ///////////////////////////////////
117
+ // WRAP CSS with .theme-$__THEME //
118
+ ///////////////////////////////////
119
+
120
+ // npm run development -- --env.run sass
121
+ if ( __RUN === 'sass' || ! __RUN ) {
122
+ const WebpackWrapThemePlugin = require ( './webpack-wrap-theme-plugin' )
123
+ const cacheDirectory = path . resolve ( path . join ( 'temp' , 'themeClass' , __THEME ) )
124
+
125
+ del . sync ( cacheDirectory )
126
+
127
+ webpackConfig = merge ( webpackConfig , {
128
+ plugins : [
129
+ new WebpackWrapThemePlugin ( {
130
+ themeClass : '.theme-' + __THEME ,
131
+ cacheDirectory
132
+ } )
133
+ ]
134
+ } )
135
+ }
136
+
137
+ //////////////
138
+ // NUNJUCKS //
139
+ //////////////
140
+
141
+ // npm run development -- --env.run html
142
+ if ( __RUN === 'html' || ! __RUN ) {
143
+ for ( let file of glob . sync ( './src/html/pages/*.html' , { ignore : '**/_*' } ) ) {
144
+ Mix . entry ( ) . entry . add ( 'mix' , path . resolve ( file ) )
145
+ }
146
+
147
+ webpackConfig = merge ( webpackConfig , {
148
+ module : {
149
+ rules : [ {
150
+ test : / \. h t m l $ / ,
151
+ loaders : [ {
152
+ loader : 'file-loader' ,
153
+ options : {
154
+ name : 'dist/[path][name].html' ,
155
+ context : './src/html/pages' ,
156
+ useRelativePath : true
157
+ }
158
+ } , 'jsbeautify-loader' , {
159
+ loader : 'nunjucks-html-loader' ,
160
+ options : {
161
+ searchPaths : [
162
+ './src/html'
163
+ ]
164
+ }
165
+ } , 'front-matter-loader' ]
166
+ } ]
167
+ }
168
+ } )
169
+ }
170
+
171
+ //////////////////
172
+ // APPLY CONFIG //
173
+ //////////////////
174
+
175
+ mix . webpackConfig ( webpackConfig )
176
+
177
+ /////////////////
178
+ // BROWSERSYNC //
179
+ /////////////////
180
+
181
+ if ( ! __RUN ) {
182
+ mix . browserSync ( require ( './bs-config.json' ) )
183
+ }
0 commit comments