Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running in a monorepo #2

Open
jeremija opened this issue Apr 8, 2019 · 0 comments
Open

Running in a monorepo #2

jeremija opened this issue Apr 8, 2019 · 0 comments

Comments

@jeremija
Copy link

jeremija commented Apr 8, 2019

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 monorepo with a package dependency symlinked into node_modules. Here's what the simplified project structure looks like:

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant