Use CommonJS export for "module": "node16" + ESM#171
Use CommonJS export for "module": "node16" + ESM#171karlhorky wants to merge 2 commits intosimov:masterfrom
Conversation
The change in simov#19 to switch to an ESM-style export seemed like the right solution at the time. However, module systems have evolved since then and the `export default` fails when "module": "node16" is configured in tsconfig.json Since `slugify` is a CommonJS module, it seems like using a CommonJS-style export is indeed the right choice for this package.
|
cc @andrewbranch in case you can see an error in my logic about |
|
slugify’s JS uses a UMD wrapper that includes module.exports = factory()
module.exports['default'] = factory()so technically the declare module slugify {
type ExtendArgs = {
[key: string]: any;
}
export function extend (args: ExtendArgs): void;
const _default: typeof slugify;
export { _default as default };
}
declare function slugify(
string: string,
options?:
| {
replacement?: string;
remove?: RegExp;
lower?: boolean;
strict?: boolean;
locale?: string;
trim?: boolean;
}
| string,
): string;
export = slugify;
export as namespace slugify;Note the |
|
Also, I will note that this kind of problem is not yet detectable by https://arethetypeswrong.github.io, but it’s next on my list. |
|
Edit: I realized that the issue I described in my comment was not caused by the changes proposed in this PR. Actually, merging this PR will be solving my problem |
|
Is there a reason to use I think it’s purely stylistic, but I’m curious to know if there are any differences. I would write it like this: declare namespace slugify {
type ExtendArgs = {
[key: string]: any;
}
interface Options {
replacement?: string;
remove?: RegExp;
lower?: boolean;
strict?: boolean;
locale?: string;
trim?: boolean;
}
}
interface Slugify {
(string: string, options?: slugify.Options | string): string;
default: Slugify;
extend: (args: slugify.ExtendArgs) => void;
}
declare const slugify: Slugify;
export = slugify;
export as namespace slugify; |
|
There’s no difference, and |
Hi there 👋 First of all, thanks for
slugify, it's very useful!The ESM-style export (
export default slugify) causes an error when using the"module": "node16"option intsconfig.jsonand"type": "module"inpackage.json:Demo on StackBlitz - run
yarn tscin the Terminal at the bottom to reproduce:https://stackblitz.com/edit/node-wshdsj?file=tsconfig.json,package.json,index.ts&file=tsconfig.json,index.ts
Background
The change to ESM-style export in #19 seemed like the right solution at the time, because bundlers such as webpack +
ts-loaderdid not understand the CommonJS syntax (export = slugify).However, module systems have evolved since then and now the
export defaultfails when"module": "node16"is configured intsconfig.jsoninside of a project which is an ES Module ("type": "module"inpackage.json).Since
slugifyis a CommonJS module, it seems like using a CommonJS-style export is indeed the right choice for this package.See more information about the problem of misconfigured declaration files in packages here:
defaultCJS or ESM exports with modulenodenext. microsoft/TypeScript#49189 (comment)