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

feat(react): automatically detect npm dependencies as external when building library #6560

Merged
merged 1 commit into from Aug 3, 2021

Conversation

jaysoo
Copy link
Member

@jaysoo jaysoo commented Jul 30, 2021

This MR allows React/Web/Next libraries to be built without manually adding entries to external option.

Current Behavior

If you used say 'lodash' you need to use option "external": ["lodash"] in project configuration.

Expected Behavior

Npm packages should not need manual external entry. Nx already knows this information and can provide it automatically.

@vercel
Copy link

vercel bot commented Jul 30, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nrwl/nx-dev/3CUhgBjm3MYXuq8EJgbWdcPM9M1N
✅ Preview: Canceled

[Deployment for 4d6c09c canceled]

Copy link
Member

@ZackDeRose ZackDeRose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@cyrus-za cyrus-za left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome. Thanks so much. Will this work with pnpm? When I tried your gist, my id value was the full system path

@jaysoo
Copy link
Member Author

jaysoo commented Aug 3, 2021

This looks awesome. Thanks so much. Will this work with pnpm? When I tried your gist, my id value was the full system path

Yes there isn't any different between how we handle npm, yarn, pnpm from a dependency detection perspective.

@jaysoo jaysoo merged commit 30428b9 into nrwl:master Aug 3, 2021
@jaysoo jaysoo deleted the feat-react-lib-npm-externals branch August 3, 2021 17:01
@cyrus-za
Copy link

cyrus-za commented Aug 21, 2021

This looks awesome. Thanks so much. Will this work with pnpm? When I tried your gist, my id value was the full system path

Yes there isn't any different between how we handle npm, yarn, pnpm from a dependency detection perspective.

Then I am not sure what I am doing wrong but I am getting errors

Error during bundle: Error: 'isEmpty' is not exported by node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/fp.js
Bundle failed: ui-hooks-use-resize-observer

isEmpty is imported from lodash in this case.

project.json (example. got multiple ones failing)

{
  "root": "libs/ui",
  "sourceRoot": "libs/ui",
  "projectType": "library",
  "targets": {
    "build": {
      "executor": "@nrwl/web:package",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/libs/ui/package",
        "tsConfig": "libs/ui/tsconfig.lib.json",
        "project": "libs/ui/package.json",
        "entryFile": "libs/ui/index.ts",
        "assets": [
          {
            "glob": "libs/ui/README.md",
            "input": ".",
            "output": "."
          }
        ]
      }
    },
    "release": {
      "executor": "@nrwl/workspace:run-commands",
      "options": {
        "commands": [
          {
            "command": "npx semantic-release"
          }
        ],
        "cwd": "libs/ui"
      }
    }
  },
  "tags": []
}

rollup.config.js that I used to have which worked. Now that I delete it, it fails.

const nxConfig = require('@nrwl/react/plugins/bundle-rollup').default

const { createProjectGraph } = require('@nrwl/workspace/src/core/project-graph')

module.exports = (config, opts) => {
  const projectGraph = createProjectGraph()

  const { tsConfig: tsConfigPath } = opts

  const projectName = Object.keys(projectGraph.nodes).find((name) =>
    projectGraph.nodes[name].data.files.some((f) => f.file === tsConfigPath)
  )

  const npmDeps = projectGraph.dependencies[projectName]
    .filter((d) => d.target.startsWith('npm:'))
    .map((d) => d.target.substr(4))

  return {
    ...config,
    ...nxConfig,
    external: (id) => {
      return (
        config.external(id) ||
        npmDeps.some((name) => id === name || id.startsWith(`${name}/`))
      )
    },
  }
}

Notice how I had config.external(id) and nx default in this PR has externalPackages.includes(id) instead.

ManojBahuguna pushed a commit to ManojBahuguna/nx that referenced this pull request Sep 16, 2021
@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants