Skip to content

Commit

Permalink
Provide minified style spec and use it where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahocevar committed Apr 1, 2022
1 parent 9fff531 commit e3afc15
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/style-spec/feature_filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {createExpression} from '../expression/index.js';
import {isFeatureConstant} from '../expression/is_constant.js';
import {deepUnbundle} from '../util/unbundle_jsonlint.js';
import latest from '../reference/latest.js';
import {min as latest} from '../reference/latest.js';
import type {GlobalProperties, Feature} from '../expression/index.js';
import type {CanonicalTileID} from '../../source/tile_id.js';
import type Point from '@mapbox/point-geometry';
Expand Down
7 changes: 4 additions & 3 deletions src/style-spec/reference/latest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @flow

import spec from './v8.json';
import * as spec from './v8.json';

export type StyleReference = typeof spec;
export type StyleReference = typeof spec.default;

export default spec;
export default spec.default;
export const min = spec.default;
30 changes: 28 additions & 2 deletions src/style-spec/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import unassert from 'rollup-plugin-unassert';
import json from '@rollup/plugin-json';
import {flow} from '../../build/rollup_plugins.js';

// Build es modules?
Expand All @@ -12,6 +11,8 @@ const esm = 'esm' in process.env;
import {fileURLToPath} from 'url';
const __dirname = fileURLToPath(new URL('.', import.meta.url));

const reviver = (key, value) => ['doc', 'example'].includes(key) ? undefined : value;

const config = [{
input: `${__dirname}/style-spec.js`,
output: {
Expand Down Expand Up @@ -46,8 +47,33 @@ const config = [{
'_token_stack:': ''
}
}),
replace({
include: /\/reference\/latest\.js$/,
delimiters: ['', ''],
values: {
'export const min = spec.default': 'export const min = spec.min'
}
}),
flow(),
json(),
{
name: 'json-min',
transform(code, id) {
if (id.endsWith('.json')) {
const json = JSON.parse(code);
const min = JSON.stringify(json, reviver);
const full = JSON.stringify(json);
code = `export default ${full}`;
if (min !== full) {
code += `\nexport const min = ${min}`;
}
return {
code,
map: null
};
}
return null;
}
},
unassert(),
resolve({
browser: true,
Expand Down
3 changes: 2 additions & 1 deletion src/style-spec/style-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type StylePropertySpecification = {
};

import v8 from './reference/v8.json';
import latest from './reference/latest.js';
import latest, {min as latestWithoutDocsAndExamples} from './reference/latest.js';
import format from './format.js';
import migrate from './migrate.js';
import composite from './composite.js';
Expand Down Expand Up @@ -109,6 +109,7 @@ const visit = {eachSource, eachLayer, eachProperty};
export {
v8,
latest,
latestWithoutDocsAndExamples,
format,
migrate,
composite,
Expand Down

0 comments on commit e3afc15

Please sign in to comment.