From 046013ee0b51f541d556b4623cde88aba77402a9 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 14 Sep 2019 18:36:22 +0100 Subject: [PATCH] added webpack build script --- .eslintrc | 3 +- .gitignore | 1 + demo/index.html | 2 +- package.json | 7 ++++ src/core/Matter.js | 2 +- webpack.config.js | 65 ++++++++++++++++++++++++++++++++++++++ webpack.examples.config.js | 49 ++++++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 webpack.config.js create mode 100644 webpack.examples.config.js diff --git a/.eslintrc b/.eslintrc index e67f987..a88e884 100644 --- a/.eslintrc +++ b/.eslintrc @@ -65,7 +65,8 @@ "Runner": false, "Svg": false, "Metrics": false, - "Example": false + "Example": false, + "__MATTER_VERSION__": false }, "extends": "eslint:recommended" } diff --git a/.gitignore b/.gitignore index b409461..34076c3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ build/matter-dev.js build/matter-dev.min.js demo/js/lib/matter-dev.js demo/js/Examples.js +demo/js/Examples.min.js examples/build test/browser/diffs test/browser/refs diff --git a/demo/index.html b/demo/index.html index 95f4b7b..d0f2a8a 100644 --- a/demo/index.html +++ b/demo/index.html @@ -17,7 +17,7 @@ - + diff --git a/package.json b/package.json index dcb4754..f4d5e18 100644 --- a/package.json +++ b/package.json @@ -48,10 +48,17 @@ "through2": "^2.0.3", "vinyl-transform": "^1.0.0", "watchify": "^3.9.0", + "webpack": "^4.39.3", + "webpack-cli": "^3.3.8", + "webpack-dev-server": "^3.8.0", "yuidocjs": "^0.10.2" }, "scripts": { "test": "gulp build:dev && gulp lint" + "dev": "webpack-dev-server", + "build": "webpack --mode=production & webpack --mode=production --env.MINIMIZE", + "build-alpha": "webpack --mode=production --env.EDGE & webpack --mode=production --env.MINIMIZE --env.EDGE", + "build-examples": "webpack --config webpack.examples.config.js --mode=production --env.EDGE & webpack --config webpack.examples.config.js --mode=production --env.MINIMIZE --env.EDGE", }, "dependencies": {}, "files": [ diff --git a/src/core/Matter.js b/src/core/Matter.js index 60ea644..8761eaa 100644 --- a/src/core/Matter.js +++ b/src/core/Matter.js @@ -27,7 +27,7 @@ var Common = require('./Common'); * @readOnly * @type {String} */ - Matter.version = '@@VERSION@@'; + Matter.version = typeof __MATTER_VERSION__ !== 'undefined' ? __MATTER_VERSION__ : '*'; /** * A list of plugin dependencies to be installed. These are normally set and installed through `Matter.use`. diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..89364af --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,65 @@ +"use strict"; + +const webpack = require('webpack'); +const path = require('path'); +const pkg = require('./package.json'); +const execSync = require('child_process').execSync; + +module.exports = (env = {}) => { + const minimize = env.MINIMIZE || false; + const edge = env.EDGE || false; + const maxSize = minimize ? 100 * 1024 : 512 * 1024; + + const commitHash = execSync('git rev-parse --short HEAD').toString().trim(); + const version = !edge ? pkg.version : `${pkg.version}-alpha-${commitHash}`; + const date = new Date().toISOString().slice(0, 10); + const name = 'matter'; + const banner = `${name} ${version} by @liabru ${date} + ${pkg.homepage} + License ${pkg.license}`; + + return { + entry: { [name]: './src/module/main.js' }, + output: { + library: 'Matter', + libraryTarget: 'umd', + umdNamedDefine: true, + globalObject: 'this', + path: path.resolve(__dirname, './build'), + filename: `[name]${minimize ? '.min' : ''}.js` + }, + node: false, + optimization: { minimize }, + performance: { + maxEntrypointSize: maxSize, + maxAssetSize: maxSize + }, + plugins: [ + new webpack.BannerPlugin(banner), + new webpack.DefinePlugin({ + __MATTER_VERSION__: JSON.stringify(version), + }) + ], + externals: { + 'poly-decomp': { + commonjs: 'poly-decomp', + commonjs2: 'poly-decomp', + amd: 'poly-decomp', + root: 'decomp' + } + }, + devServer: { + contentBase: __dirname, + open: true, + openPage: 'demo/index.html', + compress: true, + port: 8000, + proxy: { + '/build': { + target: 'http://localhost:8000/', + pathRewrite: { '^/build' : '/' } + } + } + } + }; +}; diff --git a/webpack.examples.config.js b/webpack.examples.config.js new file mode 100644 index 0000000..c82f779 --- /dev/null +++ b/webpack.examples.config.js @@ -0,0 +1,49 @@ +"use strict"; + +const webpack = require('webpack'); +const path = require('path'); +const pkg = require('./package.json'); +const execSync = require('child_process').execSync; + +module.exports = (env = {}) => { + const minimize = env.MINIMIZE || false; + const edge = env.EDGE || false; + const maxSize = minimize ? 100 * 1024 : 512 * 1024; + + const commitHash = execSync('git rev-parse --short HEAD').toString().trim(); + const version = !edge ? pkg.version : `${pkg.version}-alpha-${commitHash}`; + const date = new Date().toISOString().slice(0, 10); + const name = 'matter-examples'; + const banner = `${name} ${version} by @liabru ${date} + ${pkg.homepage} + License ${pkg.license}`; + + return { + entry: './examples/index.js', + output: { + library: 'Example', + libraryTarget: 'umd', + umdNamedDefine: true, + globalObject: 'this', + path: path.resolve(__dirname, './demo/js'), + filename: `Examples${minimize ? '.min' : ''}.js` + }, + node: false, + optimization: { minimize }, + performance: { + maxEntrypointSize: maxSize, + maxAssetSize: maxSize + }, + plugins: [ + new webpack.BannerPlugin(banner) + ], + externals: { + 'matter-js': { + commonjs: 'matter-js', + commonjs2: 'matter-js', + amd: 'matter-js', + root: 'Matter' + } + } + }; +};