Skip to content

Commit

Permalink
fix(rich-text-types): resolve generated JSON schemas (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Dec 10, 2021
1 parent b10a608 commit 1e5b4c4
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 34 deletions.
45 changes: 22 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentful-slatejs-adapter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/rich-text-html-renderer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/rich-text-types/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentful/rich-text-types",
"version": "15.9.0",
"main": "dist/rich-text-types.es5.js",
"main": "dist/index.js",
"typings": "dist/types/index.d.ts",
"files": [
"dist"
Expand Down Expand Up @@ -30,7 +30,6 @@
"devDependencies": {
"@types/jest": "^27.0.1",
"@types/node": "^14.17.14",
"core-js": "^3.17.2",
"faker": "^4.1.0",
"jest": "^27.1.0",
"rimraf": "^2.6.3",
Expand Down
9 changes: 8 additions & 1 deletion packages/rich-text-types/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ const customConfig = () => {

return {
...baseConfig,
output: {
// Maintain the same file names & folders structure as "src"
// This is necessary to resolve generated JSON schema files
//
// https://rollupjs.org/guide/en/#outputpreservemodules
preserveModules: true,
},
plugins: [
...baseConfig.plugins,
copy({
targets: {
'src/schemas/generated': 'dist/lib/schemas/generated',
'src/schemas/generated': 'dist/schemas/generated',
},
}),
],
Expand Down
18 changes: 16 additions & 2 deletions packages/rich-text-types/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ import { Node, Block, Inline, Text } from './types';
import { BLOCKS } from './blocks';
import { INLINES } from './inlines';

/**
* Tiny replacement for Object.values(object).includes(key) to
* avoid including CoreJS polyfills
*/
function hasValue(obj: Record<string, unknown>, value: unknown) {
for (const key of Object.keys(obj)) {
if (value === obj[key]) {
return true;
}
}

return false;
}

/**
* Checks if the node is an instance of Inline.
*/
export function isInline(node: Node): node is Inline {
return Object.values(INLINES).includes(node.nodeType as INLINES);
return hasValue(INLINES, node.nodeType);
}

/**
* Checks if the node is an instance of Block.
*/
export function isBlock(node: Node): node is Block {
return Object.values(BLOCKS).includes(node.nodeType as BLOCKS);
return hasValue(BLOCKS, node.nodeType);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/rich-text-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import 'core-js/features/object/values';
import 'core-js/features/array/includes';

export { BLOCKS } from './blocks';
export { INLINES } from './inlines';
export { default as MARKS } from './marks';
Expand Down
23 changes: 23 additions & 0 deletions packages/rich-text-types/src/schemas/__test__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BLOCKS } from '../../blocks';
import { INLINES } from '../../inlines';
import { isBlock, isInline } from '../../helpers';

test('isBlock', () => {
const block: any = { nodeType: BLOCKS.PARAGRAPH };
const nonBlock: any = { nodeType: 'Paragraph' };
const nonBlock2: any = { nodeType: undefined };

expect(isBlock(block)).toBe(true);
expect(isBlock(nonBlock)).toBe(false);
expect(isBlock(nonBlock2)).toBe(false);
});

test('isInline', () => {
const inline: any = { nodeType: INLINES.HYPERLINK };
const noninline: any = { nodeType: 'Hyperlink' };
const noninline2: any = { nodeType: undefined };

expect(isInline(inline)).toBe(true);
expect(isInline(noninline)).toBe(false);
expect(isInline(noninline2)).toBe(false);
});
2 changes: 1 addition & 1 deletion packages/rich-text-types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"declarationDir": "dist/types",
"outDir": "dist/lib",
"outDir": "dist",
"typeRoots": ["../../node_modules/@types", "node_modules/@types", "src/typings"],
"types": ["jest", "node"]
},
Expand Down

0 comments on commit 1e5b4c4

Please sign in to comment.