From 9363d1f8e97985d8a94ee6dac1fac39631ee3c77 Mon Sep 17 00:00:00 2001 From: Andrew Dillon Date: Wed, 19 Dec 2018 11:41:26 -0600 Subject: [PATCH] Add ESM exports --- .gitignore | 1 + Makefile.js | 52 +++++++++++++++---------------------------- package.json | 24 ++++++++++---------- rollup.config.js | 27 +++++++++++++--------- src/CmapProcessor.js | 5 +---- src/base.js | 3 ++- src/index.js | 8 ------- yarn.lock | 53 +++++++++++++++++++++++++++++++++++++++++++- 8 files changed, 103 insertions(+), 70 deletions(-) diff --git a/.gitignore b/.gitignore index 8b7f920f..e47980a1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ lib/ /**/trieUse.json /**/indic.json /**/use.json +stats.html diff --git a/Makefile.js b/Makefile.js index c3068e4a..26988deb 100644 --- a/Makefile.js +++ b/Makefile.js @@ -9,8 +9,8 @@ config.verbose = true; target.all = () => { target.clean(); target.generateTrieJson(); - target.moveTrieJsonToRoot(); - target.compileBabel(); + target.rollupESM(); + target.rollupESMMin(); target.rollupUMD(); target.rollupUMDMin(); }; @@ -22,53 +22,37 @@ target.generateTrieJson = () => { exec('babel-node src/opentype/shapers/gen-indic.js'); }; -target.moveTrieJsonToRoot = () => { +target.rollupESM = () => { target.generateTrieJson(); - mkdir('-p', 'es/opentype/shapers/') - mkdir('-p', 'lib/opentype/shapers/') - cp( - 'src/opentype/shapers/trie.json', - 'src/opentype/shapers/trieUse.json', - 'src/opentype/shapers/trieIndic.json', - 'src/opentype/shapers/indic.json', - 'src/opentype/shapers/use.json', - 'es/opentype/shapers/' - ); - cp( - 'src/opentype/shapers/trie.json', - 'src/opentype/shapers/trieUse.json', - 'src/opentype/shapers/trieIndic.json', - 'src/opentype/shapers/indic.json', - 'src/opentype/shapers/use.json', - 'lib/opentype/shapers/' - ); + env.UGLIFY = false; + env.MODULE_TYPE = 'esm'; + exec('rollup -c rollup.config.js -o dist/fontkit.es.js'); }; -target.compileBabel = () => { - target.moveTrieJsonToRoot(); - env.MODULE_TYPE = 'es6'; - exec(`babel --out-dir es src/`); - env.MODULE_TYPE = 'commonjs'; - exec(`babel --out-dir lib src/`); -} +target.rollupESMMin = () => { + target.generateTrieJson(); + env.UGLIFY = true; + env.MODULE_TYPE = 'esm'; + exec('rollup -c rollup.config.js -o dist/fontkit.es.min.js'); +}; target.rollupUMD = () => { - target.moveTrieJsonToRoot(); + target.generateTrieJson(); env.UGLIFY = false; - exec('rollup -c rollup.config.js -o dist/fontkit.js'); + env.MODULE_TYPE = 'umd'; + exec('rollup -c rollup.config.js -o dist/fontkit.umd.js'); }; target.rollupUMDMin = () => { - target.moveTrieJsonToRoot(); + target.generateTrieJson(); env.UGLIFY = true; - exec('rollup -c rollup.config.js -o dist/fontkit.min.js'); + env.MODULE_TYPE = 'umd'; + exec('rollup -c rollup.config.js -o dist/fontkit.umd.min.js'); }; target.clean = () => { rm( '-rf', - 'lib', - 'es', 'dist', 'src/opentype/shapers/trie.json', 'src/opentype/shapers/trieUse.json', diff --git a/package.json b/package.json index 06e36c67..73d3d3a6 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,10 @@ "name": "fontkit", "version": "1.7.7", "description": "An advanced font engine for Node and the browser", - "main": "dist/fontkit.js", - "unpkg": "dist/fontkit.min.js", + "main": "dist/fontkit.umd.js", + "unpkg": "dist/fontkit.umd.min.js", + "module": "dist/fontkit.es.js", + "types": "lib/index.d.ts", "scripts": { "make": "node Makefile.js", "clean": "node Makefile.js clean", @@ -22,20 +24,19 @@ "layout" ], "dependencies": { - }, - "devDependencies": { "base64-arraybuffer": "^0.1.5", - "brotli": "git+https://github.com/Hopding/brotli.js.git#0350970de946950ce47c1948913827931d88fc11", - "browserify-optional": "^1.0.0", + "brotli": "git+https://github.com/Hopding/brotli.js.git#2a623817f6bd8d1f7f528f69ac8ce8067b3383fb", "clone": "^1.0.1", "deep-equal": "^1.0.0", "dfa": "^1.0.0", + "iconv-lite": "^0.4.13", "pako": "^1.0.6", "restructure": "^0.5.3", "tiny-inflate": "^1.0.2", - "unicode-properties": "git+https://github.com/Hopding/unicode-properties.git#d97bf46ebdcef78f838f0803ec3643e608410add", - "unicode-trie": "^0.3.0", - + "unicode-properties": "git+https://github.com/Hopding/unicode-properties.git#51a63d54730b78507206f07d19bd04186e5cffe7", + "unicode-trie": "^0.3.0" + }, + "devDependencies": { "@babel/cli": "^7.2.0", "@babel/core": "^7.2.2", "@babel/node": "^7.2.2", @@ -47,7 +48,6 @@ "concat-stream": "^1.4.6", "esdoc": "^0.4.8", "esdoc-es7-plugin": "0.0.3", - "iconv-lite": "^0.4.13", "mocha": "^2.0.1", "nyc": "^10.3.2", "rollup": "^0.68.0", @@ -55,11 +55,11 @@ "rollup-plugin-babel": "^4.1.0", "rollup-plugin-commonjs": "^9.2.0", "rollup-plugin-json": "^3.1.0", - "rollup-plugin-local-resolve": "^1.0.7", "rollup-plugin-node-builtins": "^2.1.2", "rollup-plugin-node-globals": "^1.4.0", "rollup-plugin-node-resolve": "^4.0.0", - "rollup-plugin-uglify": "^6.0.0", + "rollup-plugin-terser": "^3.0.0", + "rollup-plugin-visualizer": "^0.9.2", "shelljs": "^0.8.3" } } diff --git a/rollup.config.js b/rollup.config.js index d662876f..14ec91ae 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,26 +1,33 @@ import babel from 'rollup-plugin-babel'; import nodeResolve from 'rollup-plugin-node-resolve'; -import builtins from 'rollup-plugin-node-builtins'; -import globals from 'rollup-plugin-node-globals'; +import nodeBuiltins from 'rollup-plugin-node-builtins'; +import nodeGlobals from 'rollup-plugin-node-globals'; import commonjs from 'rollup-plugin-commonjs'; import json from 'rollup-plugin-json'; -import { uglify } from 'rollup-plugin-uglify'; +import { terser } from 'rollup-plugin-terser'; import { plugin as analyze } from 'rollup-plugin-analyzer'; +import visualizer from 'rollup-plugin-visualizer'; -const { UGLIFY } = process.env; +const { UGLIFY, MODULE_TYPE } = process.env; export default { input: 'src/index.js', output: { name: 'fontkit', - format: 'umd', + format: MODULE_TYPE, }, + external: MODULE_TYPE === 'esm' + ? ['pako'] // pdf-lib will provide pako for us + : [], plugins: [ // analyze(), + // visualizer({ + // // sourcemap: true, + // open: true, + // }), nodeResolve({ jsnext: true, preferBuiltins: false, - // browser: true, }), commonjs({ exclude: 'src/**', @@ -32,7 +39,7 @@ export default { babel({ babelrc: false, presets: [ - ['@babel/preset-env', { loose: true }] + ['@babel/preset-env', { modules: false, loose: true }] ], plugins: [ ['@babel/plugin-proposal-decorators', { legacy: true }], @@ -40,8 +47,8 @@ export default { ], runtimeHelpers: true }), - globals(), - builtins(), - UGLIFY === 'true' && uglify(), + nodeGlobals(), + nodeBuiltins(), + UGLIFY === 'true' && terser(), ], }; diff --git a/src/CmapProcessor.js b/src/CmapProcessor.js index 26a89b24..fa4da13c 100644 --- a/src/CmapProcessor.js +++ b/src/CmapProcessor.js @@ -3,10 +3,7 @@ import {getEncoding} from './encodings'; import {cache} from './decorators'; import {range} from './utils'; -// iconv-lite is an optional dependency. -try { - var iconv = require('iconv-lite'); -} catch (err) {} +import iconv from 'iconv-lite'; export default class CmapProcessor { constructor(cmapTable) { diff --git a/src/base.js b/src/base.js index 5ff74e04..1d87181c 100644 --- a/src/base.js +++ b/src/base.js @@ -9,7 +9,8 @@ const fontkit = { formats.push(format); }, - create: (buffer, postscriptName) => { + create: (uint8ArrayFontData, postscriptName) => { + const buffer = new Buffer.from(uint8ArrayFontData); for (let i = 0; i < formats.length; i++) { const format = formats[i]; if (format.probe(buffer)) { diff --git a/src/index.js b/src/index.js index e0dc5b2f..11f4882e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,3 @@ -// import './shims/process'; -// import 'stream'; - -// exports.decompress = require('./dec/decode').BrotliDecompressBuffer; - -// import 'brotli/dec/decode'; - - import fontkit from './base'; import TTFFont from './TTFFont'; import WOFFFont from './WOFFFont'; diff --git a/yarn.lock b/yarn.lock index be02a21b..0748c919 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2504,6 +2504,11 @@ is-windows@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d" +is-wsl@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d" + integrity sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0= + is@~0.2.6: version "0.2.7" resolved "https://registry.yarnpkg.com/is/-/is-0.2.7.tgz#3b34a2c48f359972f35042849193ae7264b63562" @@ -3304,6 +3309,13 @@ once@^1.3.0: dependencies: wrappy "1" +opn@^5.3.0: + version "5.4.0" + resolved "https://registry.yarnpkg.com/opn/-/opn-5.4.0.tgz#cb545e7aab78562beb11aa3bfabc7042e1761035" + integrity sha512-YF9MNdVy/0qvJvDtunAOzFw9iasOQHpVthTCvGzxt61Il64AYSGdK+rYwld7NAfk9qJ7dt+hymBNSc9LNYS+Sw== + dependencies: + is-wsl "^1.1.0" + optimist@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686" @@ -3908,6 +3920,16 @@ rollup-plugin-node-resolve@^4.0.0: is-module "^1.0.0" resolve "^1.8.1" +rollup-plugin-terser@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-terser/-/rollup-plugin-terser-3.0.0.tgz#045bd7cf625ee1affcfe6971dab6fffe6fb48c65" + integrity sha512-Ed9zRD7OoCBnh0XGlEAJle5TCUsFXMLClwKzZWnS1zbNO4MelHjfCSdFZxCAdH70M40nhZ1nRrY2GZQJhSMcjA== + dependencies: + "@babel/code-frame" "^7.0.0" + jest-worker "^23.2.0" + serialize-javascript "^1.5.0" + terser "^3.8.2" + rollup-plugin-uglify@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/rollup-plugin-uglify/-/rollup-plugin-uglify-6.0.0.tgz#15aa8919e5cdc63b7cfc9319c781788b40084ce4" @@ -3918,6 +3940,16 @@ rollup-plugin-uglify@^6.0.0: serialize-javascript "^1.5.0" uglify-js "^3.4.9" +rollup-plugin-visualizer@^0.9.2: + version "0.9.2" + resolved "https://registry.yarnpkg.com/rollup-plugin-visualizer/-/rollup-plugin-visualizer-0.9.2.tgz#bbc8e8e67d5aa3e6c188c5ca0fcfa57234fb9f92" + integrity sha512-EHXHLp9Q8v5QdRTSjgio4Alr2MKxCJroLhJunmcH+pWAM5869nI5mdWjk2jp64rjxzEahrMYmfF/G5sbTHIhKw== + dependencies: + mkdirp "^0.5.1" + opn "^5.3.0" + source-map "^0.7.3" + typeface-oswald "0.0.54" + rollup-pluginutils@^2.3.0, rollup-pluginutils@^2.3.1, rollup-pluginutils@^2.3.3: version "2.3.3" resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.3.3.tgz#3aad9b1eb3e7fe8262820818840bf091e5ae6794" @@ -4072,7 +4104,7 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@^0.5.9: +source-map-support@^0.5.9, source-map-support@~0.5.6: version "0.5.9" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.9.tgz#41bc953b2534267ea2d605bccfa7bfa3111ced5f" integrity sha512-gR6Rw4MvUlYy83vP0vxoVNzM6t8MUXqNuRsuBmBHQDu1Fh6X015FrLdgoDKcNdkwGubozq0P4N0Q37UyFVr1EA== @@ -4098,6 +4130,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" +source-map@^0.7.3: + version "0.7.3" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383" + integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ== + source-map@~0.1.30: version "0.1.43" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.1.43.tgz#c24bc146ca517c1471f5dacbe2571b2b7f9e3346" @@ -4269,6 +4306,15 @@ tar@^4: safe-buffer "^5.1.2" yallist "^3.0.2" +terser@^3.8.2: + version "3.11.0" + resolved "https://registry.yarnpkg.com/terser/-/terser-3.11.0.tgz#60782893e1f4d6788acc696351f40636d0e37af0" + integrity sha512-5iLMdhEPIq3zFWskpmbzmKwMQixKmTYwY3Ox9pjtSklBLnHiuQ0GKJLhL1HSYtyffHM3/lDIFBnb82m9D7ewwQ== + dependencies: + commander "~2.17.1" + source-map "~0.6.1" + source-map-support "~0.5.6" + test-exclude@^4.1.0, test-exclude@^4.2.1: version "4.2.1" resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.1.tgz#dfa222f03480bca69207ca728b37d74b45f724fa" @@ -4368,6 +4414,11 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" +typeface-oswald@0.0.54: + version "0.0.54" + resolved "https://registry.yarnpkg.com/typeface-oswald/-/typeface-oswald-0.0.54.tgz#1e253011622cdd50f580c04e7d625e7f449763d7" + integrity sha512-U1WMNp4qfy4/3khIfHMVAIKnNu941MXUfs3+H9R8PFgnoz42Hh9pboSFztWr86zut0eXC8byalmVhfkiKON/8Q== + uglify-js@^2.6: version "2.8.29" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"