diff --git a/packages/layout/src/svg/layoutText.js b/packages/layout/src/svg/layoutText.js index 6aa63ab48..ee59bd128 100644 --- a/packages/layout/src/svg/layoutText.js +++ b/packages/layout/src/svg/layoutText.js @@ -1,12 +1,13 @@ import * as P from '@react-pdf/primitives'; -import layoutEngine from '@react-pdf/textkit/lib/layout'; -import linebreaker from '@react-pdf/textkit/lib/engines/linebreaker'; -import justification from '@react-pdf/textkit/lib/engines/justification'; -import scriptItemizer from '@react-pdf/textkit/lib/engines/scriptItemizer'; -import wordHyphenation from '@react-pdf/textkit/lib/engines/wordHyphenation'; -import decorationEngine from '@react-pdf/textkit/lib/engines/textDecoration'; -import fromFragments from '@react-pdf/textkit/lib/attributedString/fromFragments'; - +import { + layoutEngine, + linebreaker, + justification, + scriptItemizer, + wordHyphenation, + textDecoration as decorationEngine, + attributedStringFromFragments as fromFragments, +} from '@react-pdf/textkit'; import transformText from '../text/transformText'; import fontSubstitution from '../text/fontSubstitution'; diff --git a/packages/layout/src/text/getAttributedString.js b/packages/layout/src/text/getAttributedString.js index 135655ede..b7a3347fd 100644 --- a/packages/layout/src/text/getAttributedString.js +++ b/packages/layout/src/text/getAttributedString.js @@ -1,6 +1,5 @@ import * as P from '@react-pdf/primitives'; -import fromFragments from '@react-pdf/textkit/lib/attributedString/fromFragments'; - +import { attributedStringFromFragments as fromFragments } from '@react-pdf/textkit'; import { embedEmojis } from './emoji'; import ignoreChars from './ignoreChars'; import transformText from './transformText'; diff --git a/packages/layout/src/text/layoutText.js b/packages/layout/src/text/layoutText.js index f00913636..7f68159ef 100644 --- a/packages/layout/src/text/layoutText.js +++ b/packages/layout/src/text/layoutText.js @@ -1,10 +1,11 @@ -import layoutEngine from '@react-pdf/textkit/lib/layout'; -import linebreaker from '@react-pdf/textkit/lib/engines/linebreaker'; -import justification from '@react-pdf/textkit/lib/engines/justification'; -import textDecoration from '@react-pdf/textkit/lib/engines/textDecoration'; -import scriptItemizer from '@react-pdf/textkit/lib/engines/scriptItemizer'; -import wordHyphenation from '@react-pdf/textkit/lib/engines/wordHyphenation'; - +import { + layoutEngine, + linebreaker, + justification, + scriptItemizer, + wordHyphenation, + textDecoration, +} from '@react-pdf/textkit'; import fontSubstitution from './fontSubstitution'; import getAttributedString from './getAttributedString'; diff --git a/packages/layout/src/text/linesWidth.js b/packages/layout/src/text/linesWidth.js index 0b4d6eaaf..2600ae528 100644 --- a/packages/layout/src/text/linesWidth.js +++ b/packages/layout/src/text/linesWidth.js @@ -1,4 +1,4 @@ -import advanceWidth from '@react-pdf/textkit/lib/attributedString/advanceWidth'; +import { attributedStringAdvanceWidth as advanceWidth } from '@react-pdf/textkit'; /** * Get lines width (if any) diff --git a/packages/layout/tests/text/layoutText.test.js b/packages/layout/tests/text/layoutText.test.js index 816df8b1c..50a50ec85 100644 --- a/packages/layout/tests/text/layoutText.test.js +++ b/packages/layout/tests/text/layoutText.test.js @@ -1,6 +1,5 @@ import * as P from '@react-pdf/primitives'; -import runWidth from '@react-pdf/textkit/lib/run/advanceWidth'; - +import { runAdvanceWidth as runWidth } from '@react-pdf/textkit'; import layoutText from '../../src/text/layoutText'; const TEXT = diff --git a/packages/render/src/primitives/renderSvgText.js b/packages/render/src/primitives/renderSvgText.js index 94f766e0f..f39935fce 100644 --- a/packages/render/src/primitives/renderSvgText.js +++ b/packages/render/src/primitives/renderSvgText.js @@ -1,6 +1,7 @@ -import runWidth from '@react-pdf/textkit/lib/run/advanceWidth'; -import lineWidth from '@react-pdf/textkit/lib/attributedString/advanceWidth'; - +import { + runAdvanceWidth as runWidth, + attributedStringAdvanceWidth as lineWidth, +} from '@react-pdf/textkit'; import renderGlyphs from './renderGlyphs'; const renderRun = (ctx, run) => { diff --git a/packages/render/src/primitives/renderText.js b/packages/render/src/primitives/renderText.js index 8d83177a9..bdd465457 100644 --- a/packages/render/src/primitives/renderText.js +++ b/packages/render/src/primitives/renderText.js @@ -1,10 +1,11 @@ /* eslint-disable no-param-reassign */ import { isNil } from '@react-pdf/fns'; -import runHeight from '@react-pdf/textkit/lib/run/height'; -import runDescent from '@react-pdf/textkit/lib/run/descent'; -import advanceWidth from '@react-pdf/textkit/lib/run/advanceWidth'; -import ascent from '@react-pdf/textkit/lib/attributedString/ascent'; - +import { + runHeight, + runDescent, + runAdvanceWidth as advanceWidth, + attributedStringAscent as ascent, +} from '@react-pdf/textkit'; import renderGlyphs from './renderGlyphs'; import parseColor from '../utils/parseColor'; diff --git a/packages/textkit/package.json b/packages/textkit/package.json index f164d6725..02effcdf9 100644 --- a/packages/textkit/package.json +++ b/packages/textkit/package.json @@ -3,7 +3,12 @@ "license": "MIT", "version": "3.0.0", "description": "An advanced text layout framework", - "main": "./lib/index.js", + "main": "lib/textkit.cjs.js", + "module": "lib/textkit.es.js", + "browser": { + "./lib/textkit.es.js": "./lib/textkit.browser.es.js", + "./lib/textkit.cjs.js": "./lib/textkit.browser.cjs.js" + }, "repository": { "type": "git", "url": "https://github.com/diegomura/react-pdf.git", @@ -15,8 +20,8 @@ ], "scripts": { "test": "jest", - "build": "rimraf ./lib && babel src --out-dir lib", - "watch": "rimraf ./lib && babel src --out-dir lib --watch" + "build": "rimraf ./lib && rollup -c", + "watch": "rimraf ./lib && rollup -c -w" }, "files": [ "lib" diff --git a/packages/textkit/rollup.config.js b/packages/textkit/rollup.config.js new file mode 100644 index 000000000..3719c3b40 --- /dev/null +++ b/packages/textkit/rollup.config.js @@ -0,0 +1,72 @@ +import babel from '@rollup/plugin-babel'; +import localResolve from 'rollup-plugin-local-resolve' +import pkg from './package.json'; + +const cjs = { + format: 'cjs', + exports: 'named', +}; + +const esm = { + format: 'es', +}; + +const getCJS = override => Object.assign({}, cjs, override); +const getESM = override => Object.assign({}, esm, override); + +const babelConfig = ({ browser }) => ({ + babelrc: false, + exclude: 'node_modules/**', + babelHelpers: 'runtime', + presets: [ + [ + '@babel/preset-env', + { + loose: true, + modules: false, + ...(browser + ? { targets: { browsers: 'last 2 versions' } } + : { targets: { node: '12' } }), + }, + ], + ], + plugins: [ + ['@babel/plugin-transform-runtime', { version: '^7.16.4' }], + ], +}); + +const input = './src/index.js'; + +const getExternal = ({ browser }) => [ + ...(Object.keys(pkg.dependencies)), +]; + +const getPlugins = ({ browser }) => [ + localResolve(), + babel(babelConfig({ browser })), +]; + +const serverConfig = { + input, + output: [ + getESM({ file: 'lib/textkit.es.js' }), + getCJS({ file: 'lib/textkit.cjs.js' }), + ], + external: getExternal({ browser: false }), + plugins: getPlugins({ browser: false }), +}; + +const browserConfig = { + input, + output: [ + getESM({ file: 'lib/textkit.browser.es.js' }), + getCJS({ file: 'lib/textkit.browser.cjs.js' }), + ], + external: getExternal({ browser: true }), + plugins: getPlugins({ browser: true }), +}; + +export default [ + serverConfig, + browserConfig, +]; diff --git a/packages/textkit/src/index.js b/packages/textkit/src/index.js index 21935e2d5..caa08bca8 100644 --- a/packages/textkit/src/index.js +++ b/packages/textkit/src/index.js @@ -5,6 +5,12 @@ import textDecoration from './engines/textDecoration'; import scriptItemizer from './engines/scriptItemizer'; import wordHyphenation from './engines/wordHyphenation'; import fontSubstitution from './engines/fontSubstitution'; +import attributedStringFromFragments from './attributedString/fromFragments'; +import attributedStringAdvanceWidth from './attributedString/advanceWidth'; +import runAdvanceWidth from './run/advanceWidth'; +import runHeight from './run/height'; +import runDescent from './run/descent'; +import attributedStringAscent from './attributedString/ascent'; const engines = { linebreaker, @@ -18,3 +24,18 @@ const engines = { const engine = layoutEngine(engines); export default engine; +export { + layoutEngine, + linebreaker, + justification, + textDecoration, + scriptItemizer, + wordHyphenation, + fontSubstitution, + attributedStringFromFragments, + attributedStringAdvanceWidth, + runAdvanceWidth, + runHeight, + runDescent, + attributedStringAscent, +};