1
1
"use strict" ;
2
2
3
3
const webpack = require ( 'webpack' ) ;
4
- const fs = require ( 'node-fs-extra' ) ;
5
4
const path = require ( 'path' ) ;
6
5
const pkg = require ( './package.json' ) ;
6
+ const fs = require ( 'fs' ) ;
7
+ const execSync = require ( 'child_process' ) . execSync ;
7
8
const Case = require ( 'case' ) ;
8
- const uglify = require ( 'uglify-js' ) ;
9
- const glob = require ( 'glob' ) ;
10
9
11
- const isDevServer = process . argv . join ( ' ' ) . includes ( 'webpack-dev-server' ) ;
12
- const name = Case . kebab ( pkg . name ) ;
13
- const date = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
14
- const author = pkg . author . slice ( 0 , pkg . author . indexOf ( ' <' ) ) ;
15
- const banner = `${ name } ${ pkg . version } by ${ author } ${ date }
16
- ${ pkg . homepage }
17
- License ${ pkg . license } ` ;
10
+ module . exports = ( env = { } ) => {
11
+ const minimize = env . MINIMIZE || false ;
12
+ const alpha = env . ALPHA || false ;
13
+ const maxSize = 512 * 1024 ;
14
+ const commitHash = execSync ( 'git rev-parse --short HEAD' ) . toString ( ) . trim ( ) ;
15
+ const version = ! alpha ? pkg . version : `${ pkg . version } -alpha+${ commitHash } ` ;
16
+ const license = fs . readFileSync ( 'LICENSE' , 'utf8' ) ;
17
+ const date = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
18
+ const name = pkg . name ;
19
+ const alphaInfo = 'Experimental pre-release build.\n ' ;
20
+
21
+ const banner =
22
+ `${ pkg . name } ${ version } by @liabru ${ date }
23
+ ${ alpha ? alphaInfo : '' } ${ pkg . homepage }
24
+ License ${ pkg . license } ${ ! minimize ? '\n\n' + license : '' } ` ;
18
25
19
- let postBuildTasksPlugin = {
20
- apply : function ( compiler ) {
21
- fs . copySync (
22
- path . dirname ( require . resolve ( 'matter-js' ) ) + '/matter.min.js' ,
23
- 'docs/demo/lib/matter.min.js'
24
- ) ;
25
-
26
- if ( isDevServer ) {
27
- return ;
28
- }
29
-
30
- let buildDir = compiler . options . output . path ;
31
-
32
- // minify and rename all output
33
- compiler . plugin ( 'after-emit' , function ( ) {
34
- glob ( buildDir + '/*.js' , ( error , files ) => {
35
- files . forEach ( ( file ) => {
36
- if ( ! file . includes ( '.min.js' ) ) {
37
- var lowerFile = file . toLowerCase ( ) ;
38
-
39
- fs . writeFileSync (
40
- lowerFile . replace ( / \. j s $ / , '.min.js' ) ,
41
- uglify . minify ( file , {
42
- compress : true ,
43
- output : {
44
- comments : / ^ \! /
45
- }
46
- } ) . code
47
- ) ;
48
-
49
- fs . renameSync ( file , lowerFile ) ;
50
- }
51
- } ) ;
52
-
53
- // copy libs to demo page
54
- fs . copySync ( buildDir , 'docs/demo/lib' ) ;
55
- } ) ;
56
- } ) ;
57
- }
58
- } ;
59
-
60
- module . exports = {
61
- entry : {
62
- Inspector : './src/tools/Inspector' ,
63
- Demo : './src/tools/Demo' ,
64
- Gui : './src/tools/Gui' ,
65
- Serializer : './src/tools/Serializer'
66
- } ,
67
- output : {
68
- library : [ Case . pascal ( name ) , '[name]' ] ,
69
- path : './build' ,
70
- publicPath : '/demo/lib' ,
71
- filename : 'matter-tools.[name].js' ,
72
- libraryTarget : 'umd'
73
- } ,
74
- alias : {
75
- '$' : 'src/JqueryStub' ,
76
- 'jquery' : 'src/JqueryStub'
77
- } ,
78
- externals : {
79
- 'matter-js' : {
80
- commonjs : 'matter-js' ,
81
- commonjs2 : 'matter-js' ,
82
- amd : 'matter-js' ,
83
- root : 'Matter'
26
+ return {
27
+ entry : {
28
+ Inspector : './src/tools/Inspector' ,
29
+ Demo : './src/tools/Demo' ,
30
+ Gui : './src/tools/Gui' ,
31
+ Serializer : './src/tools/Serializer'
84
32
} ,
85
- 'jquery' : {
86
- commonjs : 'jquery' ,
87
- commonjs2 : 'jquery' ,
88
- amd : 'jquery' ,
89
- root : 'jQuery'
33
+ output : {
34
+ library : [ 'MatterTools' , '[name]' ] ,
35
+ path : './build' ,
36
+ publicPath : '/demo/lib' ,
37
+ libraryTarget : 'umd' ,
38
+ umdNamedDefine : true ,
39
+ globalObject : 'this' ,
40
+ path : path . resolve ( __dirname , './build' ) ,
41
+ filename : params => `matter-tools.${ Case . camel ( params . chunk . name ) } ${ minimize ? '.min' : '' } .js`
90
42
} ,
91
- 'matter-tools' : {
92
- commonjs : 'matter-tools' ,
93
- commonjs2 : 'matter-tools' ,
94
- amd : 'matter-tools' ,
95
- root : 'MatterTools'
96
- }
97
- } ,
98
- module : {
99
- loaders : [
100
- {
101
- test : / \. j s $ / ,
102
- exclude : / n o d e _ m o d u l e s / ,
103
- loader : 'babel-loader'
104
- } ,
105
- {
106
- test : / \. j s $ / ,
107
- exclude : / n o d e _ m o d u l e s / ,
108
- loader : 'exports-loader'
109
- } ,
110
- {
111
- test : / \. c s s $ / ,
112
- loader : "raw-loader"
113
- } ,
114
- {
115
- test : / \. j s $ / ,
116
- exclude : / n o d e _ m o d u l e s / ,
117
- loader : 'string-replace' ,
118
- query : {
119
- multiple : [
120
- { search : '%NAME%' , replace : name } ,
121
- { search : '%VERSION%' , replace : pkg . version }
122
- ]
123
- }
43
+ module : {
44
+ rules : [
45
+ {
46
+ test : / \. c s s $ / i,
47
+ use : [
48
+ {
49
+ loader : 'raw-loader' ,
50
+ options : {
51
+ esModule : false ,
52
+ } ,
53
+ } ,
54
+ ] ,
55
+ }
56
+ ]
57
+ } ,
58
+ optimization : { minimize } ,
59
+ performance : {
60
+ maxEntrypointSize : maxSize ,
61
+ maxAssetSize : maxSize
62
+ } ,
63
+ plugins : [
64
+ new webpack . BannerPlugin ( banner ) ,
65
+ new webpack . DefinePlugin ( {
66
+ '%NAME%' : name ,
67
+ '%VERSION%' : version
68
+ } )
69
+ ] ,
70
+ resolve : {
71
+ alias : {
72
+ jquery : 'jquery/dist/jquery.min'
124
73
}
125
- ]
126
- } ,
127
- plugins : [
128
- new webpack . BannerPlugin ( banner ) ,
129
- postBuildTasksPlugin
130
- ] ,
131
- devServer : {
132
- proxy : {
133
- '/demo/lib/matter-tools.gui.js' : {
134
- target : 'http://localhost:8080' ,
135
- pathRewrite : { '\\.gui' : '.Gui' }
136
- } ,
137
- '/demo/lib/matter-tools.demo.js' : {
138
- target : 'http://localhost:8080' ,
139
- pathRewrite : { '\\.demo' : '.Demo' }
140
- } ,
141
- '/demo/lib/matter-tools.inspector.js' : {
142
- target : 'http://localhost:8080' ,
143
- pathRewrite : { '\\.inspector' : '.Inspector' }
74
+ } ,
75
+ externals : {
76
+ 'matter-js' : {
77
+ commonjs : 'matter-js' ,
78
+ commonjs2 : 'matter-js' ,
79
+ amd : 'matter-js' ,
80
+ root : 'Matter'
144
81
} ,
145
- '/demo/lib/matter-tools.serializer.js' : {
146
- target : 'http://localhost:8080' ,
147
- pathRewrite : { '\\.serializer' : '.Serializer' }
82
+ 'matter-tools' : {
83
+ commonjs : 'matter-tools' ,
84
+ commonjs2 : 'matter-tools' ,
85
+ amd : 'matter-tools' ,
86
+ root : 'MatterTools'
87
+ }
88
+ } ,
89
+ devServer : {
90
+ open : true ,
91
+ openPage : '' ,
92
+ compress : true ,
93
+ port : 8080 ,
94
+ contentBase : [
95
+ path . resolve ( __dirname , './docs' ) ,
96
+ path . resolve ( __dirname , './node_modules/matter-js/build' )
97
+ ] ,
98
+ proxy : {
99
+ '/demo/lib/matter.min.js' : {
100
+ target : 'http://localhost:8080/' ,
101
+ pathRewrite : { '^/demo/lib/' : '/' }
102
+ }
148
103
}
149
104
}
150
- }
151
- } ;
105
+ } ;
106
+ } ;
0 commit comments