1- import { exec } from 'child_process' ;
2- import { parse } from 'url' ;
3- import { resolve } from 'path' ;
4- import { readdirSync } from 'fs' ;
1+ const { exec } = require ( 'child_process' ) ;
2+ const { parse } = require ( 'url' ) ;
3+ const { resolve } = require ( 'path' ) ;
4+ const { readdirSync } = require ( 'fs' ) ;
55
6- import { argv } from 'yargs' ;
7- import * as webpack from 'webpack' ;
8- import * as magicImporter from 'node-sass-magic-importer' ;
9- import * as SpritesmithPlugin from 'webpack-spritesmith' ;
10- import * as BrowserSyncPlugin from 'browser-sync-webpack-plugin' ;
11- import * as ExtractTextPlugin from 'extract-text- webpack-plugin';
12- import * as WebpackShellPlugin from 'webpack-shell-plugin' ;
13- import * as CleanWebpackPlugin from 'clean-webpack- plugin';
6+ const { argv } = require ( 'yargs' ) ;
7+ const webpack = require ( 'webpack' ) ;
8+ const magicImporter = require ( 'node-sass-magic-importer' ) ;
9+ const SpritesmithPlugin = require ( 'webpack-spritesmith' ) ;
10+ const BrowserSyncPlugin = require ( 'browser-sync-webpack-plugin' ) ;
11+ const CleanWebpackPlugin = require ( 'clean- webpack-plugin') ;
12+ const WebpackShellPlugin = require ( 'webpack-shell-plugin-next' ) ;
13+ const MiniCssExtractPlugin = require ( 'mini-css-extract- plugin') ;
1414
15- import { Options as BrowsersyncOptions } from 'browser-sync' ;
16- import { OptimizeOptions as SVGOOptions } from 'svgo' ;
15+ const cssnano = require ( 'cssnano' ) ;
16+ const postcssURL = require ( 'postcss-url' ) ;
17+ const autoprefixer = require ( 'autoprefixer' ) ;
18+ const postcssUtilities = require ( 'postcss-utilities' ) ;
19+ const postcssEasyImport = require ( 'postcss-easy-import' ) ;
20+ const postcssMergeRules = require ( 'postcss-merge-rules' ) ;
21+ const postcssWatchFolder = require ( 'postcss-watch-folder' ) ;
22+ const postcssFlexbugsFixed = require ( 'postcss-flexbugs-fixes' ) ;
1723
18- import * as cssnano from 'cssnano' ;
19- import * as postcssURL from 'postcss-url' ;
20- import * as autoprefixer from 'autoprefixer' ;
21- import * as postcssUtilities from 'postcss-utilities' ;
22- import * as postcssEasyImport from 'postcss-easy-import' ;
23- import * as postcssMergeRules from 'postcss-merge-rules' ;
24- import * as postcssWatchFolder from 'postcss-watch-folder' ;
25- import * as postcssFlexbugsFixed from 'postcss-flexbugs-fixes' ;
26-
27- interface ISourceMap {
28- sourceMap : boolean ;
29- }
30-
31- interface IObjectsArray {
32- plugins : any ;
33- sourceMap ?: boolean ;
34- }
35-
36- const { url, server, NODE_ENV } : any = argv ;
37- const sourceMap : ISourceMap = {
38- sourceMap : NODE_ENV === 'development'
24+ const { url, server, mode } = argv ;
25+ const sourceMap = {
26+ sourceMap : mode === 'development'
3927} ;
4028
4129if ( server ) {
4230 exec ( 'php index.php > index.html' ) ;
4331}
4432
45- const svgoConfig : SVGOOptions = {
33+ const svgoConfig = {
4634 plugins : [
4735 { name : 'cleanupAttrs' , active : true } ,
4836 { name : 'removeDoctype' , active : true } ,
@@ -63,12 +51,12 @@ const svgoConfig: SVGOOptions = {
6351 ]
6452} ;
6553
66- const postcssOptions : IObjectsArray = {
54+ const postcssOptions = {
6755 plugins : [ postcssURL ( { url : 'rebase' } ) , autoprefixer ( ) , postcssUtilities , postcssEasyImport , postcssFlexbugsFixed ] ,
6856 ...sourceMap
6957} ;
7058
71- const browserSyncConfig : BrowsersyncOptions = {
59+ const browserSyncConfig = {
7260 host : 'localhost' ,
7361 port : 3000 ,
7462 open : 'external' ,
@@ -77,7 +65,7 @@ const browserSyncConfig: BrowsersyncOptions = {
7765 server
7866 ? {
7967 match : [ '*.php' ] ,
80- fn ( _ : any , file : string ) : void {
68+ fn ( _ , file ) {
8169 const name = file . replace ( / .p h p $ / , '' ) ;
8270
8371 exec ( `php ${ file } > ${ name } .html` ) ;
@@ -107,9 +95,8 @@ const browserSyncConfig: BrowsersyncOptions = {
10795 proxy : 'localhost'
10896} ;
10997
110- const extractTextConfig : ExtractTextPlugin . PluginOptions = {
111- filename : 'dist/app.css' ,
112- allChunks : true
98+ const extractTextConfig = {
99+ filename : 'dist/app.css'
113100} ;
114101
115102const spritesmithConfig = {
@@ -127,24 +114,24 @@ const spritesmithConfig = {
127114 retina : '@2x'
128115} ;
129116
130- const cleanConfig : Record < string , any > = {
117+ const cleanConfig = {
131118 verbose : false ,
132119 exclude : [ 'sprite.svg' ] ,
133120 allowExternal : true
134121} ;
135122
136- const shellScripts : string [ ] = [ ] ;
137- const svgs : string [ ] = readdirSync ( './assets/images/svg' ) . filter ( svg => svg [ 0 ] !== '.' ) ;
123+ const shellScripts = [ ] ;
124+ const svgs = readdirSync ( './assets/images/svg' ) . filter ( svg => svg [ 0 ] !== '.' ) ;
138125
139126if ( svgs . length ) {
140127 shellScripts . push ( 'svgo -f assets/images/svg --config=' + JSON . stringify ( svgoConfig ) ) ;
141128
142129 shellScripts . push ( 'spritesh -q -i assets/images/svg -o ./assets/dist/sprite.svg -p svg-' ) ;
143130}
144131
145- module . exports = ( ) : webpack . Configuration => {
146- const isDevelopment : boolean = NODE_ENV === 'development' ;
147- const isProduction : boolean = NODE_ENV === 'production' ;
132+ module . exports = ( ) => {
133+ const isDevelopment = mode === 'development' ;
134+ const isProduction = mode === 'production' ;
148135
149136 if ( isProduction ) {
150137 postcssOptions . plugins . push ( postcssMergeRules , cssnano ( ) ) ;
@@ -159,8 +146,8 @@ module.exports = (): webpack.Configuration => {
159146 ) ;
160147 }
161148
162- const config : webpack . Configuration = {
163- mode : NODE_ENV ,
149+ const config = {
150+ mode : mode ,
164151 entry : [ './assets/styles/main.scss' , './assets/scripts/main.ts' ] ,
165152 output : {
166153 path : resolve ( __dirname , './assets' ) ,
@@ -174,27 +161,28 @@ module.exports = (): webpack.Configuration => {
174161 rules : [
175162 {
176163 test : / \. ( s a | s c | c ) s s $ / ,
177- use : ExtractTextPlugin . extract ( {
178- use : [
179- {
180- loader : 'css-loader' ,
181- options : sourceMap
182- } ,
183- {
184- loader : 'postcss-loader' ,
185- options : { postcssOptions }
186- } ,
187- {
188- loader : 'sass-loader' ,
189- options : {
190- sassOptions : {
191- importer : magicImporter ( )
192- } ,
193- ...sourceMap
194- }
164+ use : [
165+ {
166+ loader : MiniCssExtractPlugin . loader
167+ } ,
168+ {
169+ loader : 'css-loader' ,
170+ options : sourceMap
171+ } ,
172+ {
173+ loader : 'postcss-loader' ,
174+ options : { postcssOptions }
175+ } ,
176+ {
177+ loader : 'sass-loader' ,
178+ options : {
179+ sassOptions : {
180+ importer : magicImporter ( )
181+ } ,
182+ ...sourceMap
195183 }
196- ]
197- } )
184+ }
185+ ]
198186 } ,
199187 {
200188 test : / \. t s $ / ,
@@ -226,11 +214,14 @@ module.exports = (): webpack.Configuration => {
226214 jQuery : 'jquery' ,
227215 'window.jQuery' : 'jquery'
228216 } ) ,
229- new ExtractTextPlugin ( extractTextConfig ) ,
217+ new MiniCssExtractPlugin ( extractTextConfig ) ,
230218 new SpritesmithPlugin ( spritesmithConfig ) ,
231219 new CleanWebpackPlugin ( [ '../assets/dist/' ] , cleanConfig ) ,
220+ // @ts -ignore
232221 new WebpackShellPlugin ( {
233- onBuildStart : shellScripts
222+ onBuildStart : {
223+ scripts : shellScripts
224+ }
234225 } )
235226 ] ,
236227 externals : {
@@ -244,7 +235,7 @@ module.exports = (): webpack.Configuration => {
244235
245236 if ( isDevelopment ) {
246237 if ( url ) {
247- browserSyncConfig . host = parse ( url ) . hostname as any ;
238+ browserSyncConfig . host = parse ( url ) . hostname ;
248239 browserSyncConfig . proxy = url ;
249240 }
250241
@@ -255,10 +246,10 @@ module.exports = (): webpack.Configuration => {
255246 browserSyncConfig . server = true ;
256247 }
257248
258- config . plugins ? .push (
249+ config . plugins . push (
259250 new BrowserSyncPlugin ( browserSyncConfig , {
260251 reload : false
261- } ) as any
252+ } )
262253 ) ;
263254 }
264255
0 commit comments