44*/
55"use strict" ;
66
7+ const path = require ( "path" ) ;
8+
79const OptionsDefaulter = require ( "./OptionsDefaulter" ) ;
810const Template = require ( "./Template" ) ;
911
12+ const isProductionLikeMode = options => {
13+ return options . mode === "production" || ! options . mode ;
14+ } ;
15+
1016class WebpackOptionsDefaulter extends OptionsDefaulter {
1117 constructor ( ) {
1218 super ( ) ;
13- this . set ( "devtool" , "make" , options => options . mode === "development" ? "eval" : false ) ;
19+
20+ this . set ( "entry" , "./src" ) ;
21+
22+ this . set ( "devtool" , "make" , options => ( options . mode === "development" ? "eval" : false ) ) ;
1423 this . set ( "cache" , "make" , options => options . mode === "development" ) ;
1524
1625 this . set ( "context" , process . cwd ( ) ) ;
@@ -33,18 +42,22 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
3342 this . set ( "module.unsafeCache" , "make" , options => ! ! options . cache ) ;
3443 this . set ( "module.rules" , [ ] ) ;
3544 this . set ( "module.defaultRules" , [ {
36- type : "javascript/auto" ,
37- resolve : { }
38- } , {
39- test : / \. m j s $ / i,
40- type : "javascript/esm"
41- } , {
42- test : / \. j s o n $ / i,
43- type : "json" ,
44- } , {
45- test : / \. w a s m $ / i,
46- type : "webassembly/experimental" ,
47- } ] ) ;
45+ type : "javascript/auto" ,
46+ resolve : { }
47+ } ,
48+ {
49+ test : / \. m j s $ / i,
50+ type : "javascript/esm"
51+ } ,
52+ {
53+ test : / \. j s o n $ / i,
54+ type : "json"
55+ } ,
56+ {
57+ test : / \. w a s m $ / i,
58+ type : "webassembly/experimental"
59+ }
60+ ] ) ;
4861
4962 this . set ( "output" , "call" , ( value , options ) => {
5063 if ( typeof value === "string" ) {
@@ -57,8 +70,9 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
5770 return Object . assign ( { } , value ) ;
5871 }
5972 } ) ;
73+
6074 this . set ( "output.filename" , "[name].js" ) ;
61- this . set ( "output.chunkFilename" , "make" , ( options ) => {
75+ this . set ( "output.chunkFilename" , "make" , options => {
6276 const filename = options . output . filename ;
6377 const hasName = filename . indexOf ( "[name]" ) >= 0 ;
6478 const hasChunkHash = filename . indexOf ( "[chunkhash]" ) >= 0 ;
@@ -71,13 +85,13 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
7185 } ) ;
7286 this . set ( "output.webassemblyModuleFilename" , "[modulehash].module.wasm" ) ;
7387 this . set ( "output.library" , "" ) ;
74- this . set ( "output.hotUpdateFunction" , "make" , ( options ) => {
88+ this . set ( "output.hotUpdateFunction" , "make" , options => {
7589 return Template . toIdentifier ( "webpackHotUpdate" + Template . toIdentifier ( options . output . library ) ) ;
7690 } ) ;
77- this . set ( "output.jsonpFunction" , "make" , ( options ) => {
91+ this . set ( "output.jsonpFunction" , "make" , options => {
7892 return Template . toIdentifier ( "webpackJsonp" + Template . toIdentifier ( options . output . library ) ) ;
7993 } ) ;
80- this . set ( "output.chunkCallbackName" , "make" , ( options ) => {
94+ this . set ( "output.chunkCallbackName" , "make" , options => {
8195 return Template . toIdentifier ( "webpackChunk" + Template . toIdentifier ( options . output . library ) ) ;
8296 } ) ;
8397 this . set ( "output.globalObject" , "make" , options => {
@@ -96,11 +110,11 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
96110 return "self" ;
97111 }
98112 } ) ;
99- this . set ( "output.devtoolNamespace" , "make" , ( options ) => {
113+ this . set ( "output.devtoolNamespace" , "make" , options => {
100114 return options . output . library || "" ;
101115 } ) ;
102116 this . set ( "output.libraryTarget" , "var" ) ;
103- this . set ( "output.path" , process . cwd ( ) ) ;
117+ this . set ( "output.path" , path . join ( process . cwd ( ) , "dist" ) ) ;
104118 this . set ( "output.pathinfo" , "make" , options => options . mode === "development" ) ;
105119 this . set ( "output.sourceMapFilename" , "[file].map[query]" ) ;
106120 this . set ( "output.hotUpdateChunkFilename" , "[id].[hash].hot-update.js" ) ;
@@ -128,7 +142,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
128142 this . set ( "node.__filename" , "mock" ) ;
129143 this . set ( "node.__dirname" , "mock" ) ;
130144
131- this . set ( "performance" , "make" , options => options . mode !== "production" ? false : undefined ) ;
145+ this . set ( "performance" , "make" , options => ( isProductionLikeMode ( options ) ? false : undefined ) ) ;
132146 this . set ( "performance" , "call" , value => {
133147 if ( typeof value === "boolean" ) {
134148 return value ;
@@ -138,22 +152,22 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
138152 } ) ;
139153 this . set ( "performance.maxAssetSize" , 250000 ) ;
140154 this . set ( "performance.maxEntrypointSize" , 250000 ) ;
141- this . set ( "performance.hints" , "make" , options => options . mode === "production" ? "warning" : false ) ;
155+ this . set ( "performance.hints" , "make" , options => ( isProductionLikeMode ( options ) ? "warning" : false ) ) ;
142156
143157 this . set ( "optimization.removeAvailableModules" , true ) ;
144158 this . set ( "optimization.removeEmptyChunks" , true ) ;
145159 this . set ( "optimization.mergedDuplicateChunks" , true ) ;
146- this . set ( "optimization.flagIncludedChunks" , "make" , options => options . mode === "production" ) ;
147- this . set ( "optimization.occurrenceOrder" , "make" , options => options . mode === "production" ) ;
148- this . set ( "optimization.sideEffects" , "make" , options => options . mode === "production" ) ;
160+ this . set ( "optimization.flagIncludedChunks" , "make" , options => isProductionLikeMode ( options ) ) ;
161+ this . set ( "optimization.occurrenceOrder" , "make" , options => isProductionLikeMode ( options ) ) ;
162+ this . set ( "optimization.sideEffects" , "make" , options => isProductionLikeMode ( options ) ) ;
149163 this . set ( "optimization.providedExports" , true ) ;
150- this . set ( "optimization.usedExports" , "make" , options => options . mode === "production" ) ;
151- this . set ( "optimization.concatenateModules" , "make" , options => options . mode === "production" ) ;
152- this . set ( "optimization.noEmitOnErrors" , "make" , options => options . mode === "production" ) ;
164+ this . set ( "optimization.usedExports" , "make" , options => isProductionLikeMode ( options ) ) ;
165+ this . set ( "optimization.concatenateModules" , "make" , options => isProductionLikeMode ( options ) ) ;
166+ this . set ( "optimization.noEmitOnErrors" , "make" , options => isProductionLikeMode ( options ) ) ;
153167 this . set ( "optimization.namedModules" , "make" , options => options . mode === "development" ) ;
154168 this . set ( "optimization.namedChunks" , "make" , options => options . mode === "development" ) ;
155169 this . set ( "optimization.portableRecords" , "make" , options => ! ! ( options . recordsInputPath || options . recordsOutputPath || options . recordsPath ) ) ;
156- this . set ( "optimization.minimize" , "make" , options => options . mode === "production" ) ;
170+ this . set ( "optimization.minimize" , "make" , options => isProductionLikeMode ( options ) ) ;
157171 this . set ( "optimization.minimizer" , "make" , options => [ {
158172 apply : compiler => {
159173 // Lazy load the uglifyjs plugin
@@ -172,19 +186,15 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
172186 this . set ( "resolve.modules" , [ "node_modules" ] ) ;
173187 this . set ( "resolve.extensions" , [ ".wasm" , ".mjs" , ".js" , ".json" ] ) ;
174188 this . set ( "resolve.mainFiles" , [ "index" ] ) ;
175- this . set ( "resolve.aliasFields" , "make" , ( options ) => {
176- if ( options . target === "web" || options . target === "webworker" )
177- return [ "browser" ] ;
178- else
179- return [ ] ;
189+ this . set ( "resolve.aliasFields" , "make" , options => {
190+ if ( options . target === "web" || options . target === "webworker" ) return [ "browser" ] ;
191+ else return [ ] ;
180192 } ) ;
181- this . set ( "resolve.mainFields" , "make" , ( options ) => {
182- if ( options . target === "web" || options . target === "webworker" )
183- return [ "browser" , "module" , "main" ] ;
184- else
185- return [ "module" , "main" ] ;
193+ this . set ( "resolve.mainFields" , "make" , options => {
194+ if ( options . target === "web" || options . target === "webworker" ) return [ "browser" , "module" , "main" ] ;
195+ else return [ "module" , "main" ] ;
186196 } ) ;
187- this . set ( "resolve.cacheWithContext" , "make" , ( options ) => {
197+ this . set ( "resolve.cacheWithContext" , "make" , options => {
188198 return Array . isArray ( options . resolve . plugins ) && options . resolve . plugins . length > 0 ;
189199 } ) ;
190200
@@ -193,7 +203,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
193203 this . set ( "resolveLoader.mainFields" , [ "loader" , "main" ] ) ;
194204 this . set ( "resolveLoader.extensions" , [ ".js" , ".json" ] ) ;
195205 this . set ( "resolveLoader.mainFiles" , [ "index" ] ) ;
196- this . set ( "resolveLoader.cacheWithContext" , "make" , ( options ) => {
206+ this . set ( "resolveLoader.cacheWithContext" , "make" , options => {
197207 return Array . isArray ( options . resolveLoader . plugins ) && options . resolveLoader . plugins . length > 0 ;
198208 } ) ;
199209 }
0 commit comments