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: custom webpack config for react/plugins/babel #16597

Merged
merged 7 commits into from
May 21, 2021

Conversation

penx
Copy link
Contributor

@penx penx commented May 19, 2021

User facing changelog

Allow wepback config in @cypress/react/plugins/babel to be configurable to support running within a monorepo.

Additional details

If running Cypress within a monorepo, babel will not search upwards beyond the working directory to find the babel config, and will fail to transpile most code including JSX.

https://babeljs.io/docs/en/config-files#monorepos

How has the user experience changed?

Usage:

// plugins/index.js
module.exports = (on, config) => {
  if (config.testingType === 'component') {
    require('@cypress/react/plugins/babel')(on, config, {
      setWebpackConfig: (webpackConfig) => {
        webpackConfig.resolve.alias = {
          '@my-monorepo/my-package': '../../my-package/src',
        }
        webpackConfig.module.rules.find(({ loader }) => loader === 'babel-loader').options = {
          rootMode: 'upward',
        }
        return webpackConfig
      },
    });
  }
  return config
};

PR Tasks

penx added 2 commits May 19, 2021 22:30
Allow babel loader options to be configurable to support running within a monorepo.

Fixes cypress-io#16596
@penx penx requested a review from a team as a code owner May 19, 2021 21:32
@penx penx requested review from chrisbreiding and kuceb and removed request for a team May 19, 2021 21:32
@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 19, 2021

Thanks for taking the time to open a PR!

@penx penx changed the title React babel loader options feat: React babel loader options May 19, 2021
@penx penx marked this pull request as draft May 19, 2021 21:45
@penx penx marked this pull request as ready for review May 19, 2021 21:50
@penx
Copy link
Contributor Author

penx commented May 19, 2021

cc @bahmutov

Copy link
Contributor

@lmiller1990 lmiller1990 left a comment

Choose a reason for hiding this comment

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

Nicely done, just need to revert the change in cypress.schema.json and we can merge this up. It'll be available immediately after merging via semantic release.

@@ -278,6 +278,11 @@
"type": "boolean",
"default": false,
"description": "Enables including elements within the shadow DOM when using querying commands (e.g. cy.get(), cy.find()). Can be set globally in cypress.json, per-suite or per-test in the test configuration object, or programmatically with Cypress.config()"
},
"babelLoaderOptions": {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not needed, this file is for the options in cypress.json. Docs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lmiller1990 ah ok, I had assumed that it could also be passed in via cypress.json (as the config would get forwarded) but it appears it is filtered out.

Is this still the right way to pass non global config? Or should it be in a separate param?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted the change and pushed

@penx
Copy link
Contributor Author

penx commented May 20, 2021

@lmiller1990 I have also realised it may be handy to configure other parameters, such as adding alias to resolve (line 6)

Will have a think if there's a better way of doing this that allows a little more flexibility.

@penx penx marked this pull request as draft May 20, 2021 18:49
@penx
Copy link
Contributor Author

penx commented May 20, 2021

Ok so react/plugins/load-webpack/index accepts a 3rd argument for its config.

I have mirrored this by adding a third argument to react/plugins/babel/index.js that allows an optional function to be specified to make edits to the webpack config.

@penx penx marked this pull request as ready for review May 20, 2021 19:27
@penx penx changed the title feat: React babel loader options feat: custom webpack config for react/plugins/babel May 20, 2021
lmiller1990
lmiller1990 previously approved these changes May 21, 2021
@lmiller1990 lmiller1990 merged commit 75c753b into cypress-io:master May 21, 2021
Brookke pushed a commit to Brookke/cypress that referenced this pull request May 22, 2021
* Allow babel loader options to be configurable

Allow babel loader options to be configurable to support running within a monorepo.

Fixes cypress-io#16596

* new configuration option in cypress.schema.json

* Revert "new configuration option in cypress.schema.json"

This reverts commit 0a39322.

* support custom webpack configuration via config function

* fix linting issue

* Update getBabelWebpackConfig.js

* linting

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
pashidlos pushed a commit to pashidlos/cypress that referenced this pull request May 24, 2021
* Allow babel loader options to be configurable

Allow babel loader options to be configurable to support running within a monorepo.

Fixes cypress-io#16596

* new configuration option in cypress.schema.json

* Revert "new configuration option in cypress.schema.json"

This reverts commit 0a39322.

* support custom webpack configuration via config function

* fix linting issue

* Update getBabelWebpackConfig.js

* linting

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
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

Successfully merging this pull request may close these issues.

None yet

2 participants