Skip to content

Commit

Permalink
metrics: Provide ESM entrypoints for individual font metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltaranto committed Feb 26, 2024
1 parent 6a7a7dc commit aa85e04
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/sweet-terms-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@capsizecss/metrics': minor
---

metrics: Provide ESM entrypoints for individual font metrics

Build an ESM version of individual font metrics in preparation for adding named exports as part of the upcoming support for non-latin unicode subsets.
6 changes: 5 additions & 1 deletion packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
"require": "./dist/entireMetricsCollection.cjs"
},
"./package.json": "./package.json",
"./*": "./entireMetricsCollection/*.js"
"./*": {
"types": "./entireMetricsCollection/*.d.ts",
"import": "./entireMetricsCollection/*.mjs",
"require": "./entireMetricsCollection/*.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down
6 changes: 5 additions & 1 deletion packages/metrics/scripts/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import del from 'del';
import { metricsDir } from './paths';

(async () => {
await del([`${metricsDir}/*.d.ts`, `${metricsDir}/*.js`]);
await del([
`${metricsDir}/*.d.ts`,
`${metricsDir}/*.cjs`,
`${metricsDir}/*.mjs`,
]);
})();
12 changes: 8 additions & 4 deletions packages/metrics/scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ const buildFiles = async ({

allMetrics[fileName] = data;

const jsOutput = `module.exports = ${JSON.stringify(data, null, 2)
const jsOutput = `${JSON.stringify(data, null, 2)
.replace(/"(.+)":/g, '$1:')
.replace(/"/g, `'`)};\n`;
.replace(/"/g, `'`)};`;

const cjsOutput = `module.exports = ${jsOutput}\n`;
const mjsOutput = `export default ${jsOutput}\n`;

const typesOutput = dedent`
declare module '@capsizecss/metrics/${fileName}' {
Expand Down Expand Up @@ -102,8 +105,9 @@ const buildFiles = async ({
export default fontMetrics;
`;

await writeMetricsFile(`${fileName}.js`, jsOutput);
await writeMetricsFile(`${fileName}.d.ts`, `${typesOutput}}\n`);
await writeMetricsFile(`${fileName}.cjs`, cjsOutput);
await writeMetricsFile(`${fileName}.mjs`, mjsOutput);
await writeMetricsFile(`${fileName}.d.ts`, `${typesOutput}\n}\n`);
};

(async () => {
Expand Down
6 changes: 1 addition & 5 deletions site/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export default defineConfig(() => ({
build: {
// These settings only apply on `vite build`
commonjsOptions: {
include: [
new RegExp('packages/metrics/entireMetricsCollection'),
/node_modules/,
],
include: [/node_modules/],
exclude: [/react-helmet-async/],
defaultIsModuleExports: true,
},
Expand All @@ -31,7 +28,6 @@ export default defineConfig(() => ({
plugins: [
viteCommonjs({
include: [
'packages/metrics/entireMetricsCollection',
'@emotion/react',
'hoist-non-react-statics',
'react-helmet-async',
Expand Down

0 comments on commit aa85e04

Please sign in to comment.