Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert to generating commonjs for esm and commonjs support #409

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-fishes-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: revert to generating commonjs for esm and commonjs support
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

'use strict';

import { readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
const { writeFileSync } = require('fs');
const { resolve } = require('path');

import camelCase from 'camelcase';
import { program } from 'commander';

const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url)).toString());
const { program } = require('commander');
const pkg = require('../package.json');

const params = program
.name(Object.keys(pkg.bin)[0])
Expand Down Expand Up @@ -53,7 +51,7 @@ const processParams = (obj, booleanKeys) => {
if (typeof value === 'string') {
const parsedValue = stringToBoolean(value);
delete obj[key];
obj[camelCase(key)] = parsedValue;
obj[key] = parsedValue;
}
}
return obj;
Expand All @@ -62,7 +60,7 @@ const processParams = (obj, booleanKeys) => {
async function start() {
let userConfig;
try {
const { createClient } = await import(new URL('../dist/node/index.js', import.meta.url));
const { createClient } = require(resolve(__dirname, '../dist/node/index.cjs'));
userConfig = processParams(params, [
'dryRun',
'exportCore',
Expand All @@ -80,7 +78,7 @@ async function start() {
} catch (error) {
if (!userConfig.dryRun) {
const logName = `openapi-ts-error-${Date.now()}.log`;
const logPath = path.resolve(process.cwd(), logName);
const logPath = resolve(process.cwd(), logName);
writeFileSync(logPath, `${error.message}\n${error.stack}`);
console.error(`🔥 Unexpected error occurred. Log saved to ${logPath}`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"angular",
"node"
],
"main": "./dist/node/index.js",
"main": "./dist/node/index.cjs",
"types": "./dist/node/index.d.ts",
"bin": {
"openapi-ts": "bin/index.js"
"openapi-ts": "bin/index.cjs"
},
"files": [
"bin",
Expand Down
14 changes: 10 additions & 4 deletions packages/openapi-ts/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url));

const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)).toString());

// ESM only dependencies are not treated as external so that we can fully support CommonJS and ESM
const esmDependencies = ['camelcase'];

export const externalDependencies = [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies)].filter(
dependency => !esmDependencies.includes(dependency)
);

function createConfig(isProduction: boolean) {
return defineConfig({
external: [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies)],
external: externalDependencies,
input: path.resolve(__dirname, 'src/node/index.ts'),
output: {
file: path.resolve(__dirname, 'dist/node/index.js'),
format: 'esm',
file: path.resolve(__dirname, 'dist/node/index.cjs'),
format: 'cjs',
},
plugins: [
nodeResolve({ preferBuiltins: true }),
Expand All @@ -67,7 +74,6 @@ function createConfig(isProduction: boolean) {
tsconfig: path.resolve(__dirname, 'src/node/tsconfig.json'),
}),
commonjs({
extensions: ['.js'],
sourceMap: false,
}),
json(),
Expand Down
10 changes: 3 additions & 7 deletions packages/openapi-ts/rollup.dts.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { readFileSync } from 'node:fs';

import { defineConfig } from 'rollup';
import dts from 'rollup-plugin-dts';

const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)).toString());

const external = [/^node:*/, ...Object.keys(pkg.dependencies), ...Object.keys(pkg.devDependencies)];
import { externalDependencies } from './rollup.config';

export default defineConfig({
external,
external: externalDependencies,
input: {
index: './temp/node/index.d.ts',
},
output: {
dir: './dist/node',
format: 'esm',
format: 'cjs',
},
plugins: [dts({ respectExternal: true })],
});
32 changes: 16 additions & 16 deletions packages/openapi-ts/test/bin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from 'vitest';
describe('bin', () => {
it('supports required parameters', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -19,7 +19,7 @@ describe('bin', () => {

it('generates angular client', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -35,7 +35,7 @@ describe('bin', () => {

it('generates axios client', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -51,7 +51,7 @@ describe('bin', () => {

it('generates fetch client', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -67,7 +67,7 @@ describe('bin', () => {

it('generates node client', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -83,7 +83,7 @@ describe('bin', () => {

it('generates xhr client', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -99,7 +99,7 @@ describe('bin', () => {

it('supports all parameters', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -124,7 +124,7 @@ describe('bin', () => {

it('supports regexp parameters', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -142,7 +142,7 @@ describe('bin', () => {

it('formats output with Prettier', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -154,7 +154,7 @@ describe('bin', () => {

it('lints output with ESLint', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -166,14 +166,14 @@ describe('bin', () => {
});

it('throws error without parameters', () => {
const result = sync('node', ['./bin/index.js', '--dry-run', 'true']);
const result = sync('node', ['./bin/index.cjs', '--dry-run', 'true']);
expect(result.stdout.toString()).toBe('');
expect(result.stderr.toString()).toContain('Unexpected error occurred');
});

it('throws error with wrong parameters', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand All @@ -187,7 +187,7 @@ describe('bin', () => {
});

it('displays help', () => {
const result = sync('node', ['./bin/index.js', '--help', '--dry-run', 'true']);
const result = sync('node', ['./bin/index.cjs', '--help', '--dry-run', 'true']);
expect(result.stdout.toString()).toContain(`Usage: openapi-ts [options]`);
expect(result.stdout.toString()).toContain(`-i, --input <value>`);
expect(result.stdout.toString()).toContain(`-o, --output <value>`);
Expand All @@ -198,7 +198,7 @@ describe('bin', () => {
describe('cli', () => {
it('handles false booleans', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand Down Expand Up @@ -240,7 +240,7 @@ describe('cli', () => {

it('handles true booleans', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('cli', () => {

it('handles optional booleans', () => {
const result = sync('node', [
'./bin/index.js',
'./bin/index.cjs',
'--input',
'./test/spec/v3.json',
'--output',
Expand Down