Browse Source

Fix ESM and CJS exports on renderers (#21)

Loris Leiva 1 year ago
parent
commit
0dec0c8fff

+ 11 - 0
.changeset/poor-pigs-design.md

@@ -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

+ 1 - 1
.eslintrc.js

@@ -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 - 0
packages/errors/src/types/global.d.ts

@@ -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;

+ 1 - 0
packages/errors/test/types/global.d.ts

@@ -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;

+ 2 - 2
packages/internals/scripts/lint.mjs

@@ -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 - 0
packages/internals/tsup.config.base.ts

@@ -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',

+ 1 - 0
packages/internals/vitest.config.base.mts

@@ -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',

+ 1 - 0
packages/library/src/types/global.d.ts

@@ -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;

+ 1 - 0
packages/nodes/src/types/global.d.ts

@@ -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;

+ 1 - 0
packages/nodes/test/types/global.d.ts

@@ -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;

+ 1 - 0
packages/renderers-core/src/types/global.d.ts

@@ -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;

+ 1 - 0
packages/renderers-core/test/types/global.d.ts

@@ -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;

+ 2 - 1
packages/renderers-js-umi/package.json

@@ -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"

+ 1 - 0
packages/renderers-js-umi/src/types/global.d.ts

@@ -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;

+ 1 - 1
packages/renderers-js-umi/src/utils/render.ts

@@ -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);

+ 7 - 0
packages/renderers-js-umi/test/exports/commonjs.cjs

@@ -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 - 0
packages/renderers-js-umi/test/exports/module.mjs

@@ -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());

+ 2 - 1
packages/renderers-js/package.json

@@ -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"

+ 1 - 0
packages/renderers-js/src/types/global.d.ts

@@ -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;

+ 1 - 1
packages/renderers-js/src/utils/render.ts

@@ -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);

+ 7 - 0
packages/renderers-js/test/exports/commonjs.cjs

@@ -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 - 0
packages/renderers-js/test/exports/module.mjs

@@ -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());

+ 2 - 1
packages/renderers-rust/package.json

@@ -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"

+ 1 - 0
packages/renderers-rust/src/types/global.d.ts

@@ -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;

+ 1 - 1
packages/renderers-rust/src/utils/render.ts

@@ -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);

+ 7 - 0
packages/renderers-rust/test/exports/commonjs.cjs

@@ -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 - 0
packages/renderers-rust/test/exports/module.mjs

@@ -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());