Skip to content

Commit a9be014

Browse files
committed
update build scripts
1 parent 1898504 commit a9be014

File tree

5 files changed

+100
-147
lines changed

5 files changed

+100
-147
lines changed

.babelrc

-1
This file was deleted.

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"valid-jsdoc": 0,
88
"no-console": 0,
99
"no-unused-vars": 2,
10+
"no-useless-escape": 0,
1011
"indent": [
1112
2,
1213
2

.travis.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ language: node_js
22
sudo: false
33
node_js:
44
- "node"
5-
before_install:
6-
- npm install -g yarn
7-
install:
8-
- yarn install
5+
install:
6+
- npm install
97
script:
108
- npm run lint
9+
- npm run test
1110
- npm run build
12-
- npm run test

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"rigid body physics"
2121
],
2222
"scripts": {
23-
"build": "webpack",
24-
"dev": "webpack-dev-server --content-base docs/",
25-
"lint": "eslint *.js src/**/*.js test/*.js",
26-
"doc": "markdox *.js src/**/*.js -o API.md",
23+
"dev": "webpack-dev-server --watch-content-base",
24+
"build": "webpack --mode=production & webpack --mode=production --env.MINIMIZE",
25+
"lint": "eslint src/**/*.js",
26+
"doc": "markdox src/**/*.js -o API.md",
2727
"release": "npm version minor -m 'release %s' && npm publish",
2828
"release-patch": "npm version patch -m 'release %s' && npm publish",
2929
"preversion": "npm run lint",

webpack.config.js

+92-137
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,106 @@
11
"use strict";
22

33
const webpack = require('webpack');
4-
const fs = require('node-fs-extra');
54
const path = require('path');
65
const pkg = require('./package.json');
6+
const fs = require('fs');
7+
const execSync = require('child_process').execSync;
78
const Case = require('case');
8-
const uglify = require('uglify-js');
9-
const glob = require('glob');
109

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 : ''}`;
1825

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(/\.js$/, '.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'
8432
},
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`
9042
},
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: /\.js$/,
102-
exclude: /node_modules/,
103-
loader: 'babel-loader'
104-
},
105-
{
106-
test: /\.js$/,
107-
exclude: /node_modules/,
108-
loader: 'exports-loader'
109-
},
110-
{
111-
test: /\.css$/,
112-
loader: "raw-loader"
113-
},
114-
{
115-
test: /\.js$/,
116-
exclude: /node_modules/,
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: /\.css$/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'
12473
}
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'
14481
},
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+
}
148103
}
149104
}
150-
}
151-
};
105+
};
106+
};

0 commit comments

Comments
 (0)