milesj / packemon

📦 Build and prepare packages for npm distribution using standardized configurations and practices. Gotta pack 'em all!
https://packemon.dev
MIT License
203 stars 3 forks source link

MJS wrappers of ./cjs/subdir/*.js are incorrectly created at ./cjs/*-wrapper.mjs #253

Open BinToss opened 1 month ago

BinToss commented 1 month ago

The wrappers then try and fail to import their CJS module from './*.cjs' The path of the wrappers depend on their CJS module's InputMap key.

Additionally... The pattern for an InputMap key prohibits '/' characters. image

Removing the '/' characters from the keys causes the "-wrapper.m,js" files to be created at ./cjs/*-wrapper.mjs instead of alongside the CJS files at ./cjs/dotnet/*.cjs This doesn't work. The wrappers' import statements treat the CJS modules as if they're in the same directory and will fail to load them. image

BinToss commented 1 month ago

I tried a quick and easy fix. It didn't work.

Instead of changing the pattern for the InputMap keys, the path of an MJS wrapper should not be affected by the Input Map key at all and should be created alongside the CJS output.


https://github.com/milesj/packemon/blob/a443f150f43e9103ca4f16c22b2c22dbc5972baf/packages/packemon/src/schemas.ts#L89

.keysOf(string().match(/^[/a-zA-Z0-9-_]+$/u)),
                          ^

This change causes the wrapper files to be created in the correct directory, but their import will now add the directories in the InputMap key to the import path, thereby breaking the import path again.

Before this change:

// ./cjs/dotnetHelpers-wrapper.mjs
// Bundled with Packemon: https://packemon.dev
// This is an MJS wrapper for a sibling CJS file

// should be './dotnet/dotnetHelpers.cjs'
import data from './dotnetHelpers.cjs';

export const { configureDotnetNugetPush, configurePrepareCmd, nugetDefault } = data;

After this change and InputMap key prefixed with dotnet/:

// ./cjs/dotnet/dotnetHelpers-wrapper.mjs
// Bundled with Packemon: https://packemon.dev
// This is an MJS wrapper for a sibling CJS file

// should be './dotnetHelpers.cjs'
import data from './dotnet/dotnetHelpers.cjs';

export const { configureDotnetNugetPush, configurePrepareCmd, nugetDefault } = data;
milesj commented 1 month ago

Whats your packemon config look like?