Files
cypress/npm/react/plugins/utils/babel-helpers.js
T
Dmitriy Kovalenko fc30118252 feat(@cypress/react): Make correct plugins for different adapters/bundlers (#15337)
* Remove unused stuff

* Fix eslint errors

* Use local cypress running script

* Fix dependency resolution

* Revert "Fix dependency resolution"

This reverts commit 01a70be211.

* Add @cypress/react CI

* Properly resolve @types pacakges

* Run tests with mocha

* Fix cypress tests

* Fix or skip some specs

* Add retries to the card-spec.js

* Remove jest mocks

* Run e2e examples on CI

* Fix yarn cache key

* Fix e2e examples jobs

* Rename cypress-react-unit-test with @cypress/react

* Fix circleci.yml

* Revert "Fix e2e examples jobs"

This reverts commit efcc7c4d19.

* Revert " Rename cypress-react-unit-test with @cypress/react"

This reverts commit 4febfcc82b.

* Replce cypress-react-unit-test with @cypress/react

* Persist build artifacts

* Fix working directory paths circle.yml

* Fix more CI

* adding yarn.lock files

* Make package.json for  folder

* Remove .npmrc

* Update circle.yml

* Remove unused files

* Copy plugins files to the "dist" folder

* Fix links to the github repos

* Move init wizard from npm/react/init to npm/wizard

* Move init wizard from npm/react/init to npm/wizard

* Implement initial vue template

* Run wizard tests on CI

* Refactor continue: bool to success: bool for better code readability

* Fix circleci.yml

* Use only absolute paths for tests

* Stub process.exit calls

* Remove useless comments

* Add installation cypress to the wizard logic

* More improvements

* Include packages/examples/cypress into git

* Commit new files

* Use packages/example as SST for generated files

* Last improvements for wizard

* Update packages/server/lib/scaffold.js

* Rename wizard to create-cypress-tests

* Fix circleci config

* Fix snapshot tests

* Run all create-cypress-tests on ci

* Do not install plugins and scaffold files from kitchensink

* Ignore integration/examples folder for packages/example

* Run create-cypress-tests-tests on CI

* Add copy example cross-platform script

* Use copy example script in tests

* feat: create-cypress-tests auto-inject config (#9116)

* Implement the basic babel code transformation for configs

* Add more tests for edge-cases

* Add snapshot tests for autogenerated code for each template

* Add git status guard

* Add git status guard

* Fix last test

* Fix tests

* Revert changes for packages/example

* Revert changes for packages/example/tsconfig.json

* Prepeare package for the release

* Fix inquirer name

* v0.0.125

* v0.0.127

* v0.0.128

* v0.0.130

* v0.0.131

* Add more UX features

* Add vue-cli template

* Make src as default folder for vue-cli template

* Revert dev-time changes

* Run appveyour windows build

* Run full appveyour build

* Fix circle.yml

* Update plugins

* Join paths for windows

* Revert example/lib changes

* Fix tests

* Revert unnecessary changes

* Implement dev-server dependency installation

* Update npm/create-cypress-tests/README.md

* Properly exit process if react tests failed

* Fix circleci reporter not found

* Update nextjs plugin

* react-scripts plugin for dev-server

* Implement webpack-file plugin support

* v0.0.502

* v0.0.503

* Implement rollup support

* Implement babel plugin

* Update webpack options

* Fix nextjs plugin

* Unmount components in beforeEach to prevent side-effects

* Rename preprocessor to injectDevServer

* Fix next.js example

* Fix webpack-options exampel

* Reproduce rollup problem

* Update create-cypress-tests snapshots

* Do not run examples/rollup

* Uncomment test

* Change paralllelism

* Remove useless checks

* Update snapshots

Co-authored-by: Jessica Sachs <jess@jessicasachs.io>
2021-03-05 18:16:22 +02:00

225 lines
5.0 KiB
JavaScript

// @ts-check
const debug = require('debug')('find-webpack')
const path = require('path')
/**
* Returns true if the provided loader path includes "babel-loader".
* Uses current OS path separator to split the loader path correctly.
*/
const isBabelLoader = (loaderPath) => {
if (!loaderPath) {
return false
}
const loaderPathParts = loaderPath.split(path.sep)
return loaderPathParts.some((pathPart) => pathPart === 'babel-loader')
}
const findBabelRule = (webpackOptions) => {
if (!webpackOptions) {
return
}
if (!webpackOptions.module) {
return
}
debug('webpackOptions.module %o', webpackOptions.module)
if (!Array.isArray(webpackOptions.module.rules)) {
return
}
const oneOfRule = webpackOptions.module.rules.find((rule) => {
return Array.isArray(rule.oneOf)
})
if (!oneOfRule) {
debug('could not find oneOfRule')
return
}
debug('looking through oneOf rules')
debug('oneOfRule.oneOf %o', oneOfRule.oneOf)
oneOfRule.oneOf.forEach((rule) => debug('rule %o', rule))
const babelRule = oneOfRule.oneOf.find(
(rule) => rule.loader && isBabelLoader(rule.loader),
)
return babelRule
}
// see https://github.com/bahmutov/find-webpack/issues/7
const findBabelLoaderRule = (webpackOptions) => {
debug('looking for babel-loader rule')
if (!webpackOptions) {
return
}
if (!webpackOptions.module) {
return
}
debug('webpackOptions.module %o', webpackOptions.module)
if (!Array.isArray(webpackOptions.module.rules)) {
return
}
debug('webpack module rules')
webpackOptions.module.rules.forEach((rule) => {
debug('rule %o', rule)
})
const babelRule = webpackOptions.module.rules.find(
(rule) => rule.loader === 'babel-loader',
)
if (!babelRule) {
debug('could not find babel rule')
return
}
debug('found Babel rule that applies to %s', babelRule.test.toString())
return babelRule
}
const findBabelLoaderUseRule = (webpackOptions) => {
debug('looking for babel-loader rule with use property')
if (!webpackOptions) {
return
}
if (!webpackOptions.module) {
return
}
debug('webpackOptions.module %o', webpackOptions.module)
if (!Array.isArray(webpackOptions.module.rules)) {
return
}
debug('webpack module rules')
webpackOptions.module.rules.forEach((rule) => {
debug('rule %o', rule)
})
const isBabelLoader = (rule) => rule.use && rule.use.loader === 'babel-loader'
const isNextBabelLoader = (rule) => {
return rule.use && rule.use.loader === 'next-babel-loader'
}
const babelRule = webpackOptions.module.rules.find(
(rule) => isBabelLoader(rule) || isNextBabelLoader(rule),
)
if (!babelRule) {
debug('could not find babel rule')
return
}
debug('found Babel use rule that applies to %s', babelRule.test.toString())
return babelRule.use
}
const findBabelRuleWrap = (webpackOptions) => {
let babelRule = findBabelRule(webpackOptions)
if (!babelRule) {
debug('could not find Babel rule using oneOf')
babelRule = findBabelLoaderRule(webpackOptions)
}
if (!babelRule) {
debug('could not find Babel rule directly')
babelRule = findBabelLoaderUseRule(webpackOptions)
}
if (!babelRule) {
debug('could not find Babel rule')
return
}
return babelRule
}
/**
* Searches through the given Webpack config file to find Babel
* loader and its options, then returns the plugins array reference.
* If not found, returns undefined.
* @returns {Array|undefined}
*/
const findBabelPlugins = (webpackOptions) => {
const babelRule = findBabelRuleWrap(webpackOptions)
if (!babelRule) {
debug('could not find Babel rule')
return
}
debug('babel rule %o', babelRule)
if (!babelRule.options) {
debug('babel rule does not have options, inserting')
babelRule.options = {}
}
if (!Array.isArray(babelRule.options.plugins)) {
debug('babel rule options does not have plugins, inserting')
babelRule.options.plugins = []
}
return babelRule.options.plugins
}
const addFolderToBabelLoaderTranspileInPlace = (addFolderToTranspile, webpackOptions) => {
if (!addFolderToTranspile) {
debug('no extra folder to transpile using Babel')
return
}
debug(
'trying to transpile additional folder %s using Babel',
addFolderToTranspile,
)
const babelRule = findBabelRuleWrap(webpackOptions)
if (!babelRule) {
debug('could not find Babel rule')
return
}
debug('babel rule %o', babelRule)
if (!babelRule.include) {
debug('could not find Babel include condition')
return
}
if (typeof babelRule.include === 'string') {
babelRule.include = [babelRule.include]
}
if (babelRule.include.includes(addFolderToTranspile)) {
// do not double include the same folder
debug('babel includes rule for folder %s', addFolderToTranspile)
return
}
babelRule.include.push(addFolderToTranspile)
debug('added folder %s to babel rules', addFolderToTranspile)
}
module.exports = { findBabelRuleWrap, addFolderToBabelLoaderTranspileInPlace, findBabelPlugins }