Skip to content

Commit

Permalink
feat(esm): try and be a bit smarter about ESM generation
Browse files Browse the repository at this point in the history
  • Loading branch information
djMax committed Dec 12, 2023
1 parent 99cf9cd commit 8da8ca8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coconfig",
"version": "1.1.0",
"version": "1.2.0",
"description": "Centralize your per-package rc, dotfile and config files into one extensible config file",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function run() {
const pkgInfo = readPkgUp.sync({ cwd: argv.cwd, normalize: argv.normalize });
assert(pkgInfo, 'Cannot find package.json. Pass cwd option that points to a directory with a package.json');
const { packageJson, path } = pkgInfo;
const pkgValue = packageJson.config?.coconfig;
const pkgValue = packageJson.config?.coconfig as string | undefined;
const { coconfigPath, config } = await resolveConfig(path, pkgValue);

const coconfigEnv: CoConfigEnvironment = {
Expand Down
27 changes: 19 additions & 8 deletions src/passthrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,21 @@ function getModulePath(configRef: string) {
return configRef;
}

export function getFile(
env: CoConfigEnvironment,
key: string,
filename: string,
) {
export function getFile(env: CoConfigEnvironment, key: string, filename: string) {
const configRef = configReference(env.coconfigPath, env.packagePath);
const modulePath = getModulePath(configRef);
const isModule = env.packageJson?.type === 'module' || /.m[jt]s/.test(path.extname(filename));

const modulePath = isModule ? configRef : getModulePath(configRef);

const isTs = /\.[mc]?ts/.test(path.extname(filename));

const commonCode = `
const configItem = configModule.default || configModule.config || configModule;
const { configuration } = configItem && configItem['${key}'];
const resolved = typeof configuration === 'function' ? configuration() : configuration;
`;

if (path.extname(filename) === '.ts') {
// Target is Typescript
if (isModule && isTs) {
return `${header}
import cjs from '${modulePath}';
import * as esmToCjs from '${modulePath}';
Expand All @@ -56,6 +55,18 @@ ${commonCode}
// eslint-disable-next-line import/no-default-export
export default resolved;\n`;
}

if (isModule) {
return `${header}
import cjs from '${modulePath}';
import * as esmToCjs from '${modulePath}';
const configModule = cjs || esmToCjs;
${commonCode}
// eslint-disable-next-line import/no-default-export
export default resolved;\n`;
}

if (path.extname(env.coconfigPath) === '.ts') {
// Target is JS, source is typescript (requires ts-node)
return `${header}
Expand Down
6 changes: 5 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function getExport(module: Record<string, unknown>) {
return module;
}

/**
* Try and dynamically load a coconfig file in a variety of
* environments - esm, cjs, ts, js, cjs esm, etc.
*/
async function load(coconfigPath: string): Promise<{ coconfigPath: string; config: CoConfigFile }> {
try {
if (path.extname(coconfigPath) === '.ts') {
Expand All @@ -45,7 +49,7 @@ async function load(coconfigPath: string): Promise<{ coconfigPath: string; confi
}
}

export async function resolveConfig(pkgPath: string, pkgValue?: any) {
export async function resolveConfig(pkgPath: string, pkgValue?: string) {
if (typeof pkgValue === 'string') {
if (pkgValue.startsWith('.')) {
return load(path.resolve(path.dirname(pkgPath), pkgValue));
Expand Down

0 comments on commit 8da8ca8

Please sign in to comment.