Skip to content

Commit

Permalink
webpack improvements, refs Mermade#122
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRalphson committed Jan 10, 2019
1 parent 06b20a6 commit 5011576
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 30 deletions.
30 changes: 22 additions & 8 deletions docs/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@

Many thanks to @RomanGotsiy for getting these sizes down somewhat.
Further size reductions were made by replacing `js-yaml` with `yaml` and
forcing `webpack` to deduplicate across packages.
forcing `webpack` to deduplicate across packages, inspired by @mrin9.

```shell
$ npm run webpack
$ ls -lh dist
total 568K
-rw-r--r-- 1 mike mike 42K Jan 6 21:21 converter.min.js
-rw-r--r-- 1 mike mike 5.0K Jan 6 21:21 linter.min.js
-rw-r--r-- 1 mike mike 13K Jan 6 21:21 resolver.min.js
-rw-r--r-- 1 mike mike 82K Jan 6 21:21 validator.min.js
-rw-r--r-- 1 mike mike 412K Jan 6 21:21 vendor.min.js
-rw-r--r-- 1 mike mike 2.5K Jan 6 21:21 walker.min.js
total 564K
-rw-r--r-- 1 mike mike 42K Jan 7 10:09 converter.min.js
-rw-r--r-- 1 mike mike 5.0K Jan 7 10:09 linter.min.js
-rw-r--r-- 1 mike mike 406K Jan 7 10:09 oas-lib.min.js
-rw-r--r-- 1 mike mike 13K Jan 7 10:09 resolver.min.js
-rw-r--r-- 1 mike mike 82K Jan 7 10:09 validator.min.js
-rw-r--r-- 1 mike mike 2.5K Jan 7 10:09 walker.min.js
```

The whole suite is therefore around 149K gzipped.

The converter only is:

```shell
-rw-r--r-- 1 mike mike 172K Jan 7 09:41 dist/converter-lib.min.js
-rw-r--r-- 1 mike mike 42K Jan 7 09:41 dist/converterOnly.min.js
```

And the validator only is:

```shell
-rw-r--r-- 1 mike mike 359K Jan 7 09:40 dist/validator-lib.min.js
-rw-r--r-- 1 mike mike 82K Jan 7 09:40 dist/validatorOnly.min.js
```

## Browserify

Please see [api-spec-converter](https://github.com/LucyBot-Inc/api-spec-converter/) for setup or to use this [bundle](https://github.com/LucyBot-Inc/api-spec-converter/blob/master/dist/api-spec-converter.js).
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"scripts": {
"test": "npx mocha",
"webpack": "npx webpack -p",
"webpack-converter": "npx webpack -p --config webpack.converter.js",
"webpack-validator": "npx webpack -p --config webpack.validator.js",
"changelog": "npx conventional-changelog -p angular -i CHANGELOG.md -s",
"audits": "npx lerna exec npm audit",
"postinstall": "echo If you need Linode hosting, the oas-kit referral code: \"https://www.linode.com/?r=5734be467cc501b23267cf66d451bc339042ddfa\""
Expand Down
5 changes: 2 additions & 3 deletions packages/oas-linter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,11 @@ function lint(objectName,object,key,options) {
options.warnings = options.warnings.concat(results);
}

loadRules(path.join(__dirname,'rules.yaml'));

module.exports = {
lint : lint,
loadRules : loadRules,
applyRules : applyRules,
getRules : function() { return { rules: rules }; }
loadDefaultRules : function() { return loadRules(path.join(__dirname,'rules.yaml')) },
getRules : function() { return { rules }; }
};

1 change: 0 additions & 1 deletion packages/oas-resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const fs = require('fs');
const path = require('path');
const url = require('url');
const util = require('util');

const fetch = require('node-fetch-h2');
const yaml = require('yaml');
Expand Down
8 changes: 5 additions & 3 deletions packages/oas-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const fs = require('fs');
const url = require('url');
const URL = url.URL;
const util = require('util');

const yaml = require('yaml');
const should = require('should/as-function');
Expand Down Expand Up @@ -101,7 +100,7 @@ function validateSchema(schema, openapi, options) {
const errorStr = bae(schema, openapi, errors);
throw (new CLIError(errorStr));
}
throw (new JSONSchemaError('Schema invalid: ' + util.inspect(errors)));
throw (new JSONSchemaError('Schema invalid:\n'+ yaml.stringfy(errors)));
}
options.schema = schema;
return !(errors && errors.length);
Expand Down Expand Up @@ -1372,7 +1371,10 @@ function setupOptions(options,openapi) {
options.operationIds = [];
options.allScopes = {};
options.openapi = openapi;
if (options.lint && !options.linter) options.linter = linter.lint;
if (options.lint && !options.linter) {
options.linter = linter.lint;
linter.loadDefaultRules();
}
if (!options.cache) options.cache = {};
options.schema = openapi3Schema;
}
Expand Down
1 change: 0 additions & 1 deletion packages/reftools/api/testapi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const util = require('util');
const fs = require('fs');
const yaml = require('yaml');

Expand Down
4 changes: 0 additions & 4 deletions packages/reftools/lib/dereference.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const util = require('util');

const recurse = require('./recurse.js').recurse;
const clone = require('./clone.js').shallowClone;
const jptr = require('./jptr.js').jptr;
Expand Down Expand Up @@ -68,7 +66,6 @@ function dereference(o,definitions,options) {
options.cache[$ref] = entry;
entry.data = state.parent[state.pkey] = dereference(jptr(entry.source,entry.key),entry.source,options);
if ((options.$ref) && (typeof state.parent[state.pkey] === 'object')) state.parent[state.pkey][options.$ref] = $ref;
logger.warn(util.inspect(state.parent[state.pkey]));
entry.resolved = true;
}
else {
Expand All @@ -86,7 +83,6 @@ function dereference(o,definitions,options) {
else {
// we're dealing with a circular reference here
logger.warn('Unresolved ref');
logger.warn(util.inspect(entry));
state.parent[state.pkey] = jptr(entry.source,entry.path);
if (state.parent[state.pkey] === false) {
state.parent[state.pkey] = jptr(entry.source,entry.key);
Expand Down
1 change: 0 additions & 1 deletion packages/reftools/lib/toposort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* removed dependency on underscore, MER
*/

const util = require('util');
const recurse = require('./recurse.js').recurse;
const isRef = require('./isref.js').isRef;

Expand Down
2 changes: 0 additions & 2 deletions packages/swagger2openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const fs = require('fs');
const url = require('url');
const pathlib = require('path');
const util = require('util');

const maybe = require('call-me-maybe');
const fetch = require('node-fetch-h2');
Expand Down Expand Up @@ -749,7 +748,6 @@ function processParameter(param, op, path, index, openapi, options) {
param.required = true;
}
else {
console.warn(util.inspect(param));
throwError('(Patchable) path parameters must be required:true ['+param.name+' in '+index+']', options);
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/swagger2openapi/oas-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

const fs = require('fs');
const util = require('util');

const yaml = require('yaml');
const fetch = require('node-fetch-h2');
Expand Down
4 changes: 1 addition & 3 deletions packages/swagger2openapi/oas-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const fs = require('fs');
const path = require('path');
const util = require('util');
const readfiles = require('node-readfiles');
const should = require('should/as-function');
const yaml = require('yaml');
Expand Down Expand Up @@ -325,7 +324,7 @@ function processPathSpec(pathspec, expectFailure) {
}
else {
readfiles(pathspec, { readContents: false, filenameFormat: readfiles.FULL_PATH }, function (err) {
if (err) console.log(util.inspect(err));
if (err) console.log(yaml.stringify(err));
})
.then(files => {
files = files.sort();
Expand All @@ -335,7 +334,6 @@ function processPathSpec(pathspec, expectFailure) {
genStackNext();
})
.catch(err => {
//console.log(util.inspect(err));
handleResult(err,options);
});
}
Expand Down
3 changes: 1 addition & 2 deletions packages/swagger2openapi/swagger2openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const fs = require('fs');
const url = require('url');
const util = require('util');

const yaml = require('yaml');
const converter = require('./index.js');
Expand Down Expand Up @@ -69,7 +68,7 @@ let argv = require('yargs')
function processResult(err, options) {
if (err) {
delete err.options;
console.warn(util.inspect(err));
console.warn(yaml.stringify(err));
return process.exitCode = 1;
}
if (options.yaml && options.outfile && options.outfile.indexOf('.json') > 0) {
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
name: 'oas-lib',
chunks: 'all'
}
}
Expand Down
41 changes: 41 additions & 0 deletions webpack.converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
mode: 'production',
performance: { hints: false },
plugins: [
new BundleAnalyzerPlugin(),
new webpack.NormalModuleReplacementPlugin(/node_modules.yaml/, resource => {
const components = resource.request.split('/');
for (let i=0;i<components.length;i++) {
if (components[i].startsWith('oas-')) {
components[i] = 'swagger2openapi';
}
}
resource.request = components.join('/');
})
],
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'converter-lib',
chunks: 'all'
}
}
}
},
node: {
fs: 'empty'
},
entry: {
converterOnly: './packages/swagger2openapi/index.js'
},
output: {
filename: '[name].min.js'
}
};
50 changes: 50 additions & 0 deletions webpack.validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
mode: 'production',
performance: { hints: false },
plugins: [
new BundleAnalyzerPlugin(),
new webpack.NormalModuleReplacementPlugin(/node_modules.should/, resource => {
const components = resource.request.split('/');
for (let i=0;i<components.length;i++) {
if (components[i].match(/^oas-[rl]/)) {
components[i] = 'oas-validator';
}
}
resource.request = components.join('/');
}),
new webpack.NormalModuleReplacementPlugin(/node_modules.yaml/, resource => {
const components = resource.request.split('/');
for (let i=0;i<components.length;i++) {
if (components[i].match(/^oas-[rl]/)) {
components[i] = 'oas-validator';
}
}
resource.request = components.join('/');
})
],
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'validator-lib',
chunks: 'all'
}
}
}
},
node: {
fs: 'empty'
},
entry: {
validatorOnly: './packages/oas-validator/index.js'
},
output: {
filename: '[name].min.js'
}
};

0 comments on commit 5011576

Please sign in to comment.