-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support dependencies in cy.origin() callback (#23283)
Co-authored-by: Bill Glesias <bglesias@gmail.com>
- Loading branch information
1 parent
6984a1a
commit c48b80a
Showing
37 changed files
with
1,260 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
npm/webpack-preprocessor/lib/cross-origin-callback-compile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import _ from 'lodash' | ||
import Debug from 'debug' | ||
import * as path from 'path' | ||
import webpack from 'webpack' | ||
import { CrossOriginCallbackStoreFile } from './cross-origin-callback-store' | ||
|
||
const VirtualModulesPlugin = require('webpack-virtual-modules') | ||
|
||
const debug = Debug('cypress:webpack') | ||
|
||
interface Entry { | ||
[key: string]: string | ||
} | ||
|
||
interface VirtualConfig { | ||
[key: string]: string | ||
} | ||
|
||
interface EntryConfig { | ||
entry: Entry | ||
virtualConfig: VirtualConfig | ||
} | ||
|
||
// takes the files stored by the cross-origin-callback-loader and turns | ||
// them into config we can pass to webpack to compile all the files. the | ||
// virtual config allows us to just use the source we have in memory without | ||
// needing to write it to file | ||
const getConfig = ({ files, originalFilePath }): EntryConfig => { | ||
const dir = path.dirname(originalFilePath) | ||
|
||
return files.reduce((memo, file) => { | ||
const { inputFileName, source } = file | ||
const inputPath = path.join(dir, inputFileName) | ||
|
||
memo.entry[inputFileName] = inputPath | ||
memo.virtualConfig[inputPath] = source | ||
|
||
return memo | ||
}, { entry: {}, virtualConfig: {} }) | ||
} | ||
|
||
interface ConfigProperties { | ||
webpackOptions: webpack.Configuration | ||
entry: Entry | ||
virtualConfig: VirtualConfig | ||
outputDir: string | ||
} | ||
|
||
const getWebpackOptions = ({ webpackOptions, entry, virtualConfig, outputDir }: ConfigProperties): webpack.Configuration => { | ||
const modifiedWebpackOptions = _.extend({}, webpackOptions, { | ||
entry, | ||
output: { | ||
path: outputDir, | ||
}, | ||
}) | ||
const plugins = modifiedWebpackOptions.plugins || [] | ||
|
||
modifiedWebpackOptions.plugins = plugins.concat( | ||
new VirtualModulesPlugin(virtualConfig), | ||
) | ||
|
||
return modifiedWebpackOptions | ||
} | ||
|
||
interface CompileOptions { | ||
originalFilePath: string | ||
webpackOptions: webpack.Configuration | ||
} | ||
|
||
// the cross-origin-callback-loader extracts any cy.origin() callback functions | ||
// that contains Cypress.require() and stores their sources in the | ||
// CrossOriginCallbackStore. this sends those sources through webpack again | ||
// to process any dependencies and create bundles for each callback function | ||
export const compileCrossOriginCallbackFiles = (files: CrossOriginCallbackStoreFile[], options: CompileOptions): Promise<void> => { | ||
debug('compile cross-origin callback files: %o', files) | ||
|
||
const { originalFilePath, webpackOptions } = options | ||
const outputDir = path.dirname(files[0].outputFilePath) | ||
const { entry, virtualConfig } = getConfig({ files, originalFilePath }) | ||
const modifiedWebpackOptions = getWebpackOptions({ | ||
webpackOptions, | ||
entry, | ||
virtualConfig, | ||
outputDir, | ||
}) | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
const compiler = webpack(modifiedWebpackOptions) | ||
|
||
const handle = (err: Error) => { | ||
if (err) { | ||
debug('errored compiling cross-origin callback files with: %s', err.stack) | ||
|
||
return reject(err) | ||
} | ||
|
||
debug('successfully compiled cross-origin callback files') | ||
|
||
resolve() | ||
} | ||
|
||
compiler.run(handle) | ||
}) | ||
} |
Oops, something went wrong.
c48b80a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux arm64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
c48b80a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin x64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
c48b80a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin arm64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
c48b80a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
win32 x64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally: