Skip to content

Commit

Permalink
build(rulesets): try sth
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Jan 12, 2024
1 parent bf5918c commit d51690b
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 50 deletions.
6 changes: 4 additions & 2 deletions packages/rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@
"tslib": "^2.3.0"
},
"devDependencies": {
"@rollup/plugin-sucrase": "^5.0.2",
"@rollup/plugin-terser": "^0.4.4",
"@stoplight/path": "^1.3.2",
"@stoplight/spectral-parsers": "*",
"@stoplight/spectral-ref-resolver": "*",
"gzip-size": "^6.0.0",
"immer": "^9.0.6",
"terser": "^5.26.0"
"immer": "^9.0.6"
},
"scripts": {
"compile-schemas": "ts-node -T ./scripts/compile-schemas.ts",
"postbuild": "ts-node -T ./scripts/bundle.ts",
"prelint": "yarn compile-schemas --quiet",
"pretest": "yarn compile-schemas --quiet",
"prebuild": "yarn compile-schemas --quiet"
Expand Down
37 changes: 37 additions & 0 deletions packages/rulesets/scripts/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { rollup } from 'rollup';
import terser from '@rollup/plugin-terser';
import * as path from 'path';
import sucrase from '@rollup/plugin-sucrase';

const cwd = path.join(__dirname, '..');

rollup({
input: path.join(cwd, 'src/oas/functions/_oasDocumentSchema.ts'),
plugins: [
sucrase({
transforms: ['typescript'],
}),
terser({
ecma: 2020,
module: true,
compress: {
passes: 2,
},
mangle: {
// properties: true,
// reserved: ['message', 'path'],
},
}),
],
treeshake: true,
watch: false,
perf: false,
external: ['@stoplight/spectral-core', '@stoplight/json', 'leven'],
}).then(bundle =>
bundle.write({
format: 'commonjs',
exports: 'default',
sourcemap: true,
file: path.join(cwd, 'dist/oas/functions/_oasDocumentSchema.js'),
}),
);
30 changes: 2 additions & 28 deletions packages/rulesets/scripts/compile-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import standaloneCode from 'ajv/dist/standalone/index.js';
import ajvErrors from 'ajv-errors';
import ajvFormats from 'ajv-formats';
import chalk from 'chalk';
import { minify } from 'terser';
import { sync } from 'gzip-size';

const cwd = path.join(__dirname, '../src');

Expand Down Expand Up @@ -56,33 +54,9 @@ Promise.all(schemas)
oas3_1: 'https://spec.openapis.org/oas/3.1/schema/2021-09-28',
});

const minified = (
await minify(code, {
compress: {
passes: 2,
ecma: 2020,
},
ecma: 2020,
module: true,
mangle: {
toplevel: true,
module: true,
},
format: {
comments: false,
},
})
).code!;
log('writing %s size is %dKB', path.join(target, '..', basename), Math.round((code.length / 1024) * 100) / 100);

log(
'writing %s size is %dKB (original), %dKB (minified) %dKB (minified + gzipped)',
path.join(target, '..', basename),
Math.round((code.length / 1024) * 100) / 100,
Math.round((minified.length / 1024) * 100) / 100,
Math.round((sync(minified) / 1024) * 100) / 100,
);

await fs.promises.writeFile(path.join(target, '..', basename), ['// @ts-nocheck', minified].join('\n'));
await fs.promises.writeFile(path.join(target, '..', basename), ['// @ts-nocheck', code].join('\n'));
})
.then(() => {
log(chalk.green('Validators generated.'));
Expand Down
Loading

0 comments on commit d51690b

Please sign in to comment.