Skip to content

Commit b0f0fb4

Browse files
committed
remove need for esModuleInterop
1 parent 8796c44 commit b0f0fb4

10 files changed

Lines changed: 119 additions & 26 deletions

File tree

jest.config.cjs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
module.exports = {
55
// ESM config
66
transform: {
7-
'\\.[jt]sx?$': 'ts-jest'
8-
},
9-
globals: {
10-
'ts-jest': {
7+
'\\.[jt]sx?$': ['ts-jest', {
118
useESM: true
12-
}
9+
}]
1310
},
1411
moduleNameMapper: {
1512
'(.+)\\.js': '$1'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-pdf-html",
3-
"version": "1.3.5",
3+
"version": "1.3.6",
44
"author": "Dan Blaisdell dan@manifestwebdesign.com",
55
"description": "Html component for react-pdf with CSS support",
66
"keywords": [

src/Html.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import renderHtml, { HtmlRenderers } from './render.js';
33
import { HtmlStyle, HtmlStyles } from './styles.js';
44

src/camelize.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
function walk(obj: any) {
4+
if (!obj || typeof obj !== 'object') {
5+
return obj;
6+
}
7+
if (isDate(obj) || isRegex(obj)) {
8+
return obj;
9+
}
10+
if (isArray(obj)) {
11+
return map(obj, walk);
12+
}
13+
return reduce(
14+
objectKeys(obj),
15+
function (acc: any, key: string) {
16+
const camel = camelCase(key);
17+
acc[camel] = walk(obj[key]);
18+
return acc;
19+
},
20+
{}
21+
);
22+
}
23+
24+
function camelCase(str: string) {
25+
return str.replace(/[_.-](\w|$)/g, function (_, x) {
26+
return x.toUpperCase();
27+
});
28+
}
29+
30+
const isArray =
31+
Array.isArray ||
32+
function (obj) {
33+
return Object.prototype.toString.call(obj) === '[object Array]';
34+
};
35+
36+
const isDate = function (obj: any) {
37+
return Object.prototype.toString.call(obj) === '[object Date]';
38+
};
39+
40+
const isRegex = function (obj: any) {
41+
return Object.prototype.toString.call(obj) === '[object RegExp]';
42+
};
43+
44+
const has = Object.prototype.hasOwnProperty;
45+
const objectKeys =
46+
Object.keys ||
47+
function (obj) {
48+
const keys = [];
49+
for (const key in obj) {
50+
if (has.call(obj, key)) {
51+
keys.push(key);
52+
}
53+
}
54+
return keys;
55+
};
56+
57+
function map(xs: any, f: any) {
58+
if (xs.map) {
59+
return xs.map(f);
60+
}
61+
const res = [];
62+
for (let i = 0; i < xs.length; i++) {
63+
res.push(f(xs[i], i));
64+
}
65+
return res;
66+
}
67+
68+
function reduce(xs: any, f: any, acc: any) {
69+
if (xs.reduce) {
70+
return xs.reduce(f, acc);
71+
}
72+
for (let i = 0; i < xs.length; i++) {
73+
acc = f(acc, xs[i], i);
74+
}
75+
return acc;
76+
}
77+
78+
const camelize = function (obj: any) {
79+
if (typeof obj === 'string') {
80+
return camelCase(obj);
81+
}
82+
return walk(obj);
83+
};
84+
85+
export default camelize;

src/parse.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ import {
66
TextNode,
77
} from 'node-html-parser';
88
import { Tag } from './tags.js';
9-
import cssTree, { Block, Declaration, List, Rule, StyleSheet } from 'css-tree';
9+
import {
10+
generate,
11+
parse as cssParse,
12+
Block,
13+
Declaration,
14+
List,
15+
Rule,
16+
StyleSheet,
17+
} from 'css-tree';
1018
import supportedStyles from './supportedStyles.js';
1119
import { HtmlStyle, HtmlStyles } from './styles.js';
1220
import remoteCss from './resolveCssFile.js';
13-
import camelize from 'camelize';
21+
import camelize from './camelize.js';
1422

1523
export type HtmlContent = (HtmlElement | string)[];
1624

@@ -38,7 +46,7 @@ export const convertRule = (
3846
property: camelize(entry.property as string),
3947
}))
4048
.reduce((style, { property, value }: Declaration) => {
41-
let valueString = cssTree.generate(value);
49+
let valueString = generate(value);
4250
if (property && value) {
4351
if (property === 'fontFamily') {
4452
valueString = valueString.replace(/["']+/g, '');
@@ -74,7 +82,7 @@ export const convertRule = (
7482
export const convertStylesheet = (stylesheet: string): HtmlStyles => {
7583
const response = {} as HtmlStyles;
7684
try {
77-
const parsed = cssTree.parse(stylesheet) as StyleSheet;
85+
const parsed = cssParse(stylesheet) as StyleSheet;
7886
const rules = parsed.children.filter(
7987
(rule) => rule.type === 'Rule' && rule.prelude?.type === 'SelectorList'
8088
) as List<Rule>;
@@ -84,7 +92,7 @@ export const convertStylesheet = (stylesheet: string): HtmlStyles => {
8492
return;
8593
}
8694
rule.prelude.children.forEach((selector) => {
87-
const selectorString = cssTree.generate(selector);
95+
const selectorString = generate(selector);
8896
response[selectorString] = style;
8997
});
9098
});
@@ -99,7 +107,7 @@ export const convertElementStyle = (
99107
tag: string
100108
): HtmlStyle | undefined => {
101109
try {
102-
const parsed = cssTree.parse(`${tag} { ${styleAttr} }`) as StyleSheet;
110+
const parsed = cssParse(`${tag} { ${styleAttr} }`) as StyleSheet;
103111
const rules = parsed.children.filter(
104112
(rule) => rule.type === 'Rule' && rule.prelude?.type === 'SelectorList'
105113
) as List<Rule>;

src/render.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactElement, ReactNode } from 'react';
1+
import * as React from 'react';
22
// const stringify = require('json-stringify-safe');
33
import { HtmlContent, HtmlElement } from './parse.js';
44
import renderHtml, {
@@ -29,13 +29,12 @@ import ReactPDF, {
2929
Font,
3030
renderToString,
3131
} from '@react-pdf/renderer';
32-
import path from 'path';
32+
import * as path from 'path';
3333
import renderers, {
3434
renderBlock,
3535
renderNoop,
3636
renderPassThrough,
3737
} from './renderers.js';
38-
import exp from 'constants';
3938

4039
const inlineElement: HtmlElement = {
4140
tag: 'span',

src/render.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactElement } from 'react';
1+
import * as React from 'react';
22
import renderers, {
33
renderBlock,
44
renderInline,
@@ -149,7 +149,11 @@ export const bucketElements = (
149149
return buckets;
150150
};
151151

152-
type RenderedContent = ReactElement | ReactElement[] | string | string[];
152+
type RenderedContent =
153+
| React.ReactElement
154+
| React.ReactElement[]
155+
| string
156+
| string[];
153157

154158
export const renderElement = (
155159
element: HtmlElement | string,
@@ -330,7 +334,7 @@ const renderHtml = (
330334
stylesheet?: HtmlStyles | HtmlStyles[];
331335
resetStyles?: boolean;
332336
} = {}
333-
): ReactElement => {
337+
): React.ReactElement => {
334338
const defaultFontSize = 18;
335339
const fontSizeStyle = { fontSize: defaultFontSize };
336340
const styles = options.style

src/renderers.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import {
33
Circle,
44
ClipPath,
@@ -25,7 +25,7 @@ import { HtmlElement } from './parse.js';
2525
import { HtmlStyle } from './styles.js';
2626
import { lowerAlpha, orderedAlpha, upperAlpha } from './ordered.type.js';
2727
import { Style } from '@react-pdf/types';
28-
import camelize from 'camelize';
28+
import camelize from './camelize.js';
2929

3030
export const renderNoop: HtmlRenderer = ({ children }) => <></>;
3131

src/resolveCssFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import fs from 'fs';
2-
import url from 'url';
3-
import path from 'path';
4-
import fetch from 'sync-fetch';
1+
import * as fs from 'fs';
2+
import * as url from 'url';
3+
import * as path from 'path';
4+
import * as fetch from 'sync-fetch';
55

66
const isBrowser = !fs || !url || !path;
77

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// "typeRoots": [], /* List of folders to include type definitions from. */
4747
// "types": [], /* Type declaration files to be included in compilation. */
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49-
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
49+
// "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
5151
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5252

0 commit comments

Comments
 (0)