git.iko.soy/rowtype-yoga/yoga-r3f
on main help sign in
88 lines 2.7 kB history raw
1// This build file creates a static version of the Typr.ts library used for
2// processing fonts. It's isolated within a "factory" function wrapper so it can
3// easily be marshalled into a web worker.
6import nodeResolve from 'rollup-plugin-node-resolve'
7import commonjs from '@rollup/plugin-commonjs'
8import { terser } from 'rollup-plugin-terser'
11const {LERNA_ROOT_PATH} = process.env
12if (!LERNA_ROOT_PATH) {
13 throw new Error("Please execute `npm run build-typr` from the repository root.")
17const OUTPUT_TEMPLATE = `
18/*!
19Custom build of Typr.ts (https://github.com/fredli74/Typr.ts) for use in Troika text rendering.
20Original MIT license applies: https://github.com/fredli74/Typr.ts/blob/master/LICENSE
21*/
23export default function() {
24 // Trick it into being able to run in a web worker
25 if (typeof window === 'undefined') {
26 self.window = self
27 }
29 $$CONTENT$$
31 return TyprExports.Typr
35const [banner, footer] = OUTPUT_TEMPLATE.split('$$CONTENT$$')
38export default {
39 input: LERNA_ROOT_PATH + '/node_modules/@lojjic/typr/dist/Typr.js',
40 plugins: [
41 nodeResolve(),
42 commonjs(),
43 {
44 name: 'custom',
45 transform(source, id) {
46 // Quiet the console.warn statements added in the Typr.ts fork
47 source = source.replace(/console\.warn/g, 'console.debug')
49 // Replace some unneeded property assignments with unused stub vars so they get pruned out
50 // SVG fonts are unsupported by browsers anyway
51 removeMethodAssignment('Typr.SVG')
52 removeMethodAssignment('Typr.SVG.parse')
53 removeMethodAssignment('Typr.SVG.toPath')
54 removeMethodAssignment('Typr.SVG._toPath')
55 removeMethodAssignment('Typr.SVG._tokens')
56 removeMethodAssignment('Typr.SVG._toksToPath')
57 removeMethodAssignment('Typr.SVG._reps')
58 removeMethodAssignment('Typr.U.pathToSVG')
59 removeMethodAssignment('Typr.U.pathToContext')
61 // We provide our own implementation for stringToGlyphs that fixes joining-type shaping
62 // and improves performance, so don't need the original
63 removeMethodAssignment('Typr.U.stringToGlyphs')
64 removeMethodAssignment('Typr.U._getWPfeature')
65 // Same for position adjustments to support mark anchoring
66 removeMethodAssignment('Typr.U.getPairAdjustment')
68 return source
70 function removeMethodAssignment(name) {
71 const re = new RegExp(`(${name}\\s*=)`)
72 source = source.replace(re, `var prunable${Math.round(Math.random()*1e12)}=`)
73 }
74 }
75 },
76 terser({
77 ecma: 5
78 })
79 ],
80 output: {
81 format: 'iife',
82 exports: 'named',
83 name: 'TyprExports',
84 file: 'libs/typr.factory.js',
85 banner,
86 footer
87 }