Skip to content

Commit

Permalink
Fix: CJS support for require() without .default
Browse files Browse the repository at this point in the history
  • Loading branch information
vHeemstra committed Jul 22, 2023
1 parent 24cb61f commit 416a423
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"scripts": {
"dev": "npm run build -- --watch src",
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
"build": "tsup",
"lint": "eslint src/*.ts test/*.ts",
"lint:fix": "eslint --fix src/*.ts test/*.ts",
"test": "jest",
Expand Down
7 changes: 4 additions & 3 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
PoObject,
Source,
} from './shared';
import { isVinyl as _isVinyl } from './utils';

let pot_input_files: Vinyl[] = [];
let po_input_files: string[][] = [];
Expand Down Expand Up @@ -75,7 +74,7 @@ export const processPOT = (
resolve: ProcessPotResolveCallback,
reject: ProcessPotRejectCallback
) => {
const isVinyl = _isVinyl(pot_file);
const isVinyl = Vinyl.isVinyl(pot_file);
const pot_filepath = isVinyl ? pot_file.path : pot_file;

// Get filepaths of POs
Expand Down Expand Up @@ -139,7 +138,9 @@ export default (cb: AsyncCallback, options: Options) => {
// Process all POT files
Promise.all(
resolvedOptions.potSources.map((pot_file) => {
const pot_filepath = _isVinyl(pot_file) ? pot_file.relative : pot_file;
const pot_filepath = Vinyl.isVinyl(pot_file)
? pot_file.relative
: pot_file;

return new Promise(
(
Expand Down
26 changes: 25 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fillPotPo from './async';
import fillPotPoSync from './sync';
import prepareOptions from './options';

/*
* Default content-related options for generating PO files.
Expand All @@ -13,4 +14,27 @@ const testOptions = {
includeGenerator: false,
};

export { fillPotPo as default, fillPotPoSync as sync, testOptions };
export default fillPotPo;
export { fillPotPoSync as sync, testOptions, prepareOptions };
// export { fillPotPo as default, fillPotPoSync as sync, testOptions, prepareOptions };

/**
* Unfortunately needed shim for fixing the problem with ugly import syntax.
* Before:
* `const fillPotPo = require('fill-pot-po').default`
* After:
* `const fillPotPo = require('fill-pot-po')`
* See: https://github.com/egoist/tsup/issues/255#issuecomment-784856826
*/
if (typeof module !== 'undefined') {
// @ts-ignore
fillPotPo.sync = fillPotPoSync;
// @ts-ignore
fillPotPo.testOptions = testOptions;
// @ts-ignore
fillPotPo.prepareOptions = prepareOptions;
// @ts-ignore
module.exports = fillPotPo;
// @ts-ignore
module.exports.default = fillPotPo;
}
6 changes: 3 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { resolve, relative } from 'node:path';
import Vinyl from 'vinyl';
// // import { Buffer } from 'node:buffer';
// import { Buffer } from 'safe-buffer';
import PluginError from './plugin-error';
import {
isArray,
isObject,
isString,
isBool,
isString,
isArrayOfStrings,
isArrayOfVinyls,
// isArrayOfBuffers,
// isArrayOfVinylsOrBuffers,
} from './utils';
import { isVinyl } from 'vinyl';

import type {
ValidatedOptions,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const validateOptionsInput = (options: Options): ValidatedOptions => {
// !Buffer.isBuffer(options.potSources) &&
// !isArrayOfBuffers(options.potSources) &&
// !isArrayOfVinylsOrBuffers(options.potSources) &&
!isVinyl(options.potSources) &&
!Vinyl.isVinyl(options.potSources) &&
!isArrayOfVinyls(options.potSources)
) {
throw new OptionsError(
Expand Down
3 changes: 1 addition & 2 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ResolvedOptions,
PoObject,
} from './shared';
import { isVinyl as _isVinyl } from './utils';

let pot_input_files: Vinyl[] = [];
let po_input_files: string[][] = [];
Expand Down Expand Up @@ -65,7 +64,7 @@ export const processPOs = (
* @return {void}
*/
export const processPOT = (pot_file: Source, options: ResolvedOptions) => {
const isVinyl = _isVinyl(pot_file);
const isVinyl = Vinyl.isVinyl(pot_file);
const pot_filepath: string = isVinyl ? pot_file.path : pot_file;

// Get filepaths of POs
Expand Down
14 changes: 2 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ export const isBool = (value: unknown): value is boolean => {
return Object.prototype.toString.call(value) === '[object Boolean]';
};

/**
* Determine if `value` is a Vinyl object or not.
*
* @param {mixed} value
* @return {boolean}
*/
export const isVinyl = (value: unknown): value is Vinyl => {
return Vinyl.isVinyl(value);
};

/**
* Determine if `value` is an array containing only strings or not.
*
Expand All @@ -86,7 +76,7 @@ export const isArrayOfStrings = (value: unknown): value is Array<string> => {
*/
export const isArrayOfVinyls = (value: unknown): value is Array<Vinyl> => {
if (!isArray(value)) return false;
return Boolean(value.reduce((r, v) => isVinyl(v) && r, true));
return Boolean(value.reduce((r, v) => Vinyl.isVinyl(v) && r, true));
};

/**
Expand All @@ -110,7 +100,7 @@ export const isArrayOfVinyls = (value: unknown): value is Array<Vinyl> => {
// value: unknown
// ): value is Array<Vinyl | Buffer> => {
// if (!isArray(value)) return false;
// return value.reduce((r, v) => isVinyl(v) && r, true);
// return value.reduce((r, v) => Vinyl.isVinyl(v) && r, true);
// };

/**
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"compilerOptions": {
"module": "es2022",
"module": "ES2022",
"moduleResolution": "NodeNext",
"lib": ["ES2021.String"],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
}
}
11 changes: 8 additions & 3 deletions tsup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { defineConfig } from 'tsup';

export default defineConfig({
// entry: ['src/index.ts'],
// splitting: false,
entry: ['src/index.ts'],
format: ['cjs', 'esm'],
dts: true,
splitting: false,
// sourcemap: true,
// clean: true,
clean: true,
minify: true,
shims: true,
target: 'node14',
tsconfig: './tsconfig.json',
silent: true,
});

0 comments on commit 416a423

Please sign in to comment.