Skip to content

Commit

Permalink
Fix ESM and CJS exports on renderers (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored May 7, 2024
1 parent 5a6e769 commit 0dec0c8
Show file tree
Hide file tree
Showing 27 changed files with 86 additions and 9 deletions.
11 changes: 11 additions & 0 deletions .changeset/poor-pigs-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@kinobi-so/renderers-js-umi": patch
"@kinobi-so/renderers-core": patch
"@kinobi-so/renderers-rust": patch
"@kinobi-so/renderers-js": patch
"kinobi": patch
"@kinobi-so/errors": patch
"@kinobi-so/nodes": patch
---

Fix ESM and CJS exports on renderers
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
extends: ['turbo', '@solana/eslint-config-solana'],
root: true,
ignorePatterns: ['.eslintrc.js', '.eslintrc.cjs', 'dist/', '*.json', '*.njk', '*.cjs'],
ignorePatterns: ['.eslintrc.js', '.eslintrc.cjs', 'dist/', '*.json', '*.njk'],
};
1 change: 1 addition & 0 deletions packages/errors/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/errors/test/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/internals/scripts/lint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { $, argv } from 'zx';
// Lint and format the code.
$.stdio = 'inherit';
if (argv.fix) {
await $`pnpm eslint --fix src/* test/* && pnpm prettier --log-level warn --ignore-unknown --write ./*`;
await $`pnpm eslint --fix "src/*" "test/*" && pnpm prettier --log-level warn --ignore-unknown --write ./*`;
} else {
await $`pnpm eslint src/* test/* && pnpm prettier --check .`;
await $`pnpm eslint "src/*" "test/*" && pnpm prettier --check .`;
}
1 change: 1 addition & 0 deletions packages/internals/tsup.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function getBuildConfig(options: BuildOptions): TsupConfig {
return {
define: {
__BROWSER__: `${platform === 'browser'}`,
__ESM__: `${format === 'esm'}`,
__NODEJS__: `${platform === 'node'}`,
__REACTNATIVE__: `${platform === 'react-native'}`,
__TEST__: 'false',
Expand Down
1 change: 1 addition & 0 deletions packages/internals/vitest.config.base.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function getVitestConfig(platform: Platform) {
define: {
__BROWSER__: `${platform === 'browser'}`,
__DEV__: 'true',
__ESM__: 'true',
__NODEJS__: `${platform === 'node'}`,
__REACTNATIVE__: `${platform === 'react-native'}`,
__TEST__: 'true',
Expand Down
1 change: 1 addition & 0 deletions packages/library/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/nodes/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/nodes/test/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/renderers-core/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/renderers-core/test/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/renderers-js-umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"dev": "zx ../../node_modules/@kinobi-so/internals/scripts/test-unit.mjs node --watch",
"lint": "zx ../../node_modules/@kinobi-so/internals/scripts/lint.mjs",
"lint:fix": "zx ../../node_modules/@kinobi-so/internals/scripts/lint.mjs --fix",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e && pnpm test:exports",
"test:e2e": "./e2e/test.sh",
"test:exports": "node ./test/exports/module.mjs && node ./test/exports/commonjs.cjs",
"test:node": "zx ../../node_modules/@kinobi-so/internals/scripts/test-unit.mjs node",
"test:treeshakability": "zx ../../node_modules/@kinobi-so/internals/scripts/test-treeshakability.mjs",
"test:types": "zx ../../node_modules/@kinobi-so/internals/scripts/test-types.mjs"
Expand Down
1 change: 1 addition & 0 deletions packages/renderers-js-umi/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/renderers-js-umi/src/utils/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function jsDocblock(docs: string[]): string {

export const render = (template: string, context?: object, options?: NunJucksOptions): string => {
// @ts-expect-error import.meta will be used in the right environment.
const dirname = typeof __dirname !== 'undefined' ? __dirname : pathDirname(fileURLToPath(import.meta.url));
const dirname = __ESM__ ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
const templates = __TEST__ ? join(dirname, '..', '..', 'public', 'templates') : join(dirname, 'templates'); // Path to templates from bundled output file.
const env = nunjucks.configure(templates, { autoescape: false, trimBlocks: true, ...options });
env.addFilter('pascalCase', pascalCase);
Expand Down
7 changes: 7 additions & 0 deletions packages/renderers-js-umi/test/exports/commonjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { definedTypeNode, numberTypeNode } = require('@kinobi-so/nodes');
const { visit } = require('@kinobi-so/visitors-core');

const { getRenderMapVisitor } = require('../../dist/index.node.cjs');

const node = definedTypeNode({ name: 'answerToLife', type: numberTypeNode('u8') });
visit(node, getRenderMapVisitor());
10 changes: 10 additions & 0 deletions packages/renderers-js-umi/test/exports/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This ensures that we do not rely on `__dirname` in ES modules even when it is polyfilled.
globalThis.__dirname = 'DO_NOT_USE';

import { definedTypeNode, numberTypeNode } from '@kinobi-so/nodes';
import { visit } from '@kinobi-so/visitors-core';

import { getRenderMapVisitor } from '../../dist/index.node.mjs';

const node = definedTypeNode({ name: 'answerToLife', type: numberTypeNode('u8') });
visit(node, getRenderMapVisitor());
3 changes: 2 additions & 1 deletion packages/renderers-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"dev": "zx ../../node_modules/@kinobi-so/internals/scripts/test-unit.mjs node --watch",
"lint": "zx ../../node_modules/@kinobi-so/internals/scripts/lint.mjs",
"lint:fix": "zx ../../node_modules/@kinobi-so/internals/scripts/lint.mjs --fix",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e && pnpm test:exports",
"test:e2e": "./e2e/test.sh",
"test:exports": "node ./test/exports/module.mjs && node ./test/exports/commonjs.cjs",
"test:node": "zx ../../node_modules/@kinobi-so/internals/scripts/test-unit.mjs node",
"test:treeshakability": "zx ../../node_modules/@kinobi-so/internals/scripts/test-treeshakability.mjs",
"test:types": "zx ../../node_modules/@kinobi-so/internals/scripts/test-types.mjs"
Expand Down
1 change: 1 addition & 0 deletions packages/renderers-js/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/renderers-js/src/utils/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function jsDocblock(docs: string[]): string {

export const render = (template: string, context?: object, options?: NunJucksOptions): string => {
// @ts-expect-error import.meta will be used in the right environment.
const dirname = typeof __dirname !== 'undefined' ? __dirname : pathDirname(fileURLToPath(import.meta.url));
const dirname = __ESM__ ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
const templates = __TEST__ ? join(dirname, '..', '..', 'public', 'templates') : join(dirname, 'templates'); // Path to templates from bundled output file.
const env = nunjucks.configure(templates, { autoescape: false, trimBlocks: true, ...options });
env.addFilter('pascalCase', pascalCase);
Expand Down
7 changes: 7 additions & 0 deletions packages/renderers-js/test/exports/commonjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { definedTypeNode, numberTypeNode } = require('@kinobi-so/nodes');
const { visit } = require('@kinobi-so/visitors-core');

const { getRenderMapVisitor } = require('../../dist/index.node.cjs');

const node = definedTypeNode({ name: 'answerToLife', type: numberTypeNode('u8') });
visit(node, getRenderMapVisitor());
10 changes: 10 additions & 0 deletions packages/renderers-js/test/exports/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This ensures that we do not rely on `__dirname` in ES modules even when it is polyfilled.
globalThis.__dirname = 'DO_NOT_USE';

import { definedTypeNode, numberTypeNode } from '@kinobi-so/nodes';
import { visit } from '@kinobi-so/visitors-core';

import { getRenderMapVisitor } from '../../dist/index.node.mjs';

const node = definedTypeNode({ name: 'answerToLife', type: numberTypeNode('u8') });
visit(node, getRenderMapVisitor());
3 changes: 2 additions & 1 deletion packages/renderers-rust/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
"dev": "zx ../../node_modules/@kinobi-so/internals/scripts/test-unit.mjs node --watch",
"lint": "zx ../../node_modules/@kinobi-so/internals/scripts/lint.mjs",
"lint:fix": "zx ../../node_modules/@kinobi-so/internals/scripts/lint.mjs --fix",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e && pnpm test:exports",
"test:e2e": "./e2e/test.sh",
"test:exports": "node ./test/exports/module.mjs && node ./test/exports/commonjs.cjs",
"test:node": "zx ../../node_modules/@kinobi-so/internals/scripts/test-unit.mjs node",
"test:treeshakability": "zx ../../node_modules/@kinobi-so/internals/scripts/test-treeshakability.mjs",
"test:types": "zx ../../node_modules/@kinobi-so/internals/scripts/test-types.mjs"
Expand Down
1 change: 1 addition & 0 deletions packages/renderers-rust/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __BROWSER__: boolean;
declare const __DEV__: boolean;
declare const __ESM__: boolean;
declare const __NODEJS__: boolean;
declare const __REACTNATIVE__: boolean;
declare const __TEST__: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/renderers-rust/src/utils/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function rustDocblock(docs: string[]): string {

export const render = (template: string, context?: object, options?: NunJucksOptions): string => {
// @ts-expect-error import.meta will be used in the right environment.
const dirname = typeof __dirname !== 'undefined' ? __dirname : pathDirname(fileURLToPath(import.meta.url));
const dirname = __ESM__ ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
const templates = __TEST__ ? join(dirname, '..', '..', 'public', 'templates') : join(dirname, 'templates'); // Path to templates from bundled output file.
const env = nunjucks.configure(templates, { autoescape: false, trimBlocks: true, ...options });
env.addFilter('pascalCase', pascalCase);
Expand Down
7 changes: 7 additions & 0 deletions packages/renderers-rust/test/exports/commonjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { definedTypeNode, numberTypeNode } = require('@kinobi-so/nodes');
const { visit } = require('@kinobi-so/visitors-core');

const { getRenderMapVisitor } = require('../../dist/index.node.cjs');

const node = definedTypeNode({ name: 'answerToLife', type: numberTypeNode('u8') });
visit(node, getRenderMapVisitor());
10 changes: 10 additions & 0 deletions packages/renderers-rust/test/exports/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This ensures that we do not rely on `__dirname` in ES modules even when it is polyfilled.
globalThis.__dirname = 'DO_NOT_USE';

import { definedTypeNode, numberTypeNode } from '@kinobi-so/nodes';
import { visit } from '@kinobi-so/visitors-core';

import { getRenderMapVisitor } from '../../dist/index.node.mjs';

const node = definedTypeNode({ name: 'answerToLife', type: numberTypeNode('u8') });
visit(node, getRenderMapVisitor());

0 comments on commit 0dec0c8

Please sign in to comment.