mattdesl / mattdesl/esmify

Running in a monorepo

Open
#2 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
119
Forks
11
PR merge metrics
No merged PRs in 30d

Description

Hi @mattdesl,

Thanks for making this transform! It's cool to be able to make use of `common-shake` with `browserify`!

I ran into an issue when I was trying to run `esmify` in a [lerna hoisted](https://github.com/lerna/lerna/blob/master/doc/hoist.mdv) monorepo with a package dependency symlinked into `node_modules`. Here's what the simplified project structure looks like:

```bash
package.json
node_modules/
pkg1 -> ../packages/pkg1
packages/
pkg1/
index.js
fn.js
package.json
pkg2/
package.json
index.js # import {fn} from 'pkg1'
```

Because the `node_modules/pkg1` symlink ends up being resolved into `packages/pkg1`, the `isNodeModule` function returns `false` for the `packages/pkg1/fn.js`, and since it is actually outside of the `pkg2` package I'm trying to compile, the transforms are never run on `packages/pkg1/fn.js`.

Additionally, when I run `browserify` from `pkg2/` folder, the `isNodeModule` returns false for `../node_modules/pkg1/index.js` as well. The error I get is:

```
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
```

This is because neither global nor local transforms run on `pkg1/fn.js`. I was able to hack a solution by adding two if clauses (`packages/` is standard for lerna, but is not a requirement), but it doesn't seem bullet-proof:

```
const isNodeModule = (file, cwd) => {
const dir = path.dirname(file);
const relative = relativePath(cwd, dir);
const result = relative.startsWith(`node_modules${path.sep}`)
|| relative.indexOf(`${path.sep}node_modules${path.sep}`) > -1
|| dir.indexOf(`${path.sep}packages${path.sep}`) > -1;
console.log('isNodeModule', file, relative, result)
return result
};
```

Perhaps there could be an option like `projectPathRegex` so projects could specify it separately? Alternatively, we could check if the relative path starts with `../`. Do you maybe have a better suggestion?

Thanks!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the isNodeModule function and reproducing the lerna-hoisted layout described in the issue, including the symlinked node_modules/pkg1 dependency. Trace why transforms are skipped for packages/pkg1/fn.js and ../node_modules/pkg1/index.js; done means the relevant transforms run without requiring a packages-specific assumption.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.