Skip to content

Commit

Permalink
Merge pull request #15616 from cypress-io/refactor/use-public-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaSachs authored Mar 23, 2021
2 parents 36f26d9 + 2d88a43 commit a97318c
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as React from 'react'

const LazyDog = React.lazy(() => import(/* webpackChunkName: "Dog" */ './Dog'))
const LazyDog = React.lazy(() => {
return import(/* webpackChunkName: "Dog" */ './Dog')
.then((comp) => new Promise((resolve) => setTimeout(() => resolve(comp), 10)))
})

interface LazyComponentProps {}

Expand Down
1 change: 1 addition & 0 deletions npm/rollup-dev-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Options {
specs: Cypress.Cypress['spec'][] // Why isn't this working? It works for webpack-dev-server
config: Record<string, string>
devServerEvents: EventEmitter
devServerPublicPathRoute: string
[key: string]: unknown
}

Expand Down
13 changes: 7 additions & 6 deletions npm/rollup-dev-server/src/makeHtmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@ const readIndexHtml = () => readFileSync(indexHtmlPath).toString()
* Example usage:
* formatSpecName('/cypress/component/foo.spec.tsx') //=> 'foo.spec.js'
*/
function formatSpecName (filename: string) {
function formatSpecName (publicPath: string, filename: string) {
const split = filename.split('/')
const name = split[split.length - 1]
const pos = name.lastIndexOf('.')
const newName = `${name.substr(0, pos < 0 ? name.length : pos)}.js`

return `/${newName}`
return `${publicPath}/${newName}`
}

function handleIndex (indexHtml: string, projectRoot: string, supportFilePath: string, cypressSpecPath: string) {
function handleIndex (indexHtml: string, publicPath: string, supportFilePath: string, cypressSpecPath: string) {
const specPath = `/${cypressSpecPath}`

console.log(supportFilePath)
const supportFile = readFileSync(supportFilePath).toString()

return render(indexHtml, {
supportFile,
specPath: formatSpecName(specPath),
specPath: formatSpecName(publicPath, specPath),
})
}

export const makeHtmlPlugin = (
projectRoot: string,
supportFilePath: string,
server: Express,
publicPath: string,
) => {
const indexHtml = readIndexHtml()

server.use('/index.html', (req, res) => {
server.use(`${publicPath}/index.html`, (req, res) => {
const html = handleIndex(
indexHtml,
projectRoot,
publicPath,
supportFilePath,
req.headers.__cypress_spec_path as string,
)
Expand Down
12 changes: 8 additions & 4 deletions npm/rollup-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface NollupDevServer {
}

export async function start (devServerOptions: StartDevServer): Promise<NollupDevServer> {
console.log('OBject', Object.keys(devServerOptions.options))
const rollupConfigObj = typeof devServerOptions.rollupConfig === 'string'
? await loadConfigFile(devServerOptions.rollupConfig).then((configResult) => configResult.options)
: devServerOptions.rollupConfig
Expand All @@ -58,25 +59,28 @@ export async function start (devServerOptions: StartDevServer): Promise<NollupDe
}
})

const { devServerPublicPathRoute, projectRoot, supportFile } = devServerOptions.options.config

const app = express()
const server = http.createServer(app)
const contentBase = resolve(__dirname, devServerOptions.options.config.projectRoot)
const contentBase = resolve(__dirname, projectRoot)
/* random port between 3000 and 23000 */
const port = parseInt(((Math.random() * 20000) + 3000).toFixed(0), 10)

const nollup = NollupDevMiddleware(app, config, {
contentBase,
port,
publicPath: '/',
publicPath: devServerPublicPathRoute,
hot: true,
}, server)

app.use(nollup)

makeHtmlPlugin(
devServerOptions.options.config.projectRoot,
devServerOptions.options.config.supportFile,
projectRoot,
supportFile,
app,
devServerPublicPathRoute,
)

return {
Expand Down
7 changes: 4 additions & 3 deletions npm/vite-dev-server/src/makeCypressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const pluginName = 'cypress-transform-html'
const indexHtmlPath = resolve(__dirname, '../index-template.html')
const readIndexHtml = () => readFileSync(indexHtmlPath).toString()

function handleIndex (indexHtml, projectRoot, supportFilePath, req, res) {
const specPath = `/${req.headers.__cypress_spec_path}`
function handleIndex (indexHtml, projectRoot, publicPath, supportFilePath, req, res) {
const specPath = `${publicPath}/${req.headers.__cypress_spec_path}`
const supportPath = supportFilePath ? `/${relative(projectRoot, supportFilePath)}` : null

res.end(render(indexHtml, { supportPath, specPath }))
Expand All @@ -18,6 +18,7 @@ function handleIndex (indexHtml, projectRoot, supportFilePath, req, res) {
export const makeCypressPlugin = (
projectRoot: string,
supportFilePath: string,
publicPath: string,
devServerEvents: EventEmitter,
): Plugin => {
return {
Expand All @@ -26,7 +27,7 @@ export const makeCypressPlugin = (
configureServer: (server: ViteDevServer) => {
const indexHtml = readIndexHtml()

server.middlewares.use('/index.html', (req, res) => handleIndex(indexHtml, projectRoot, supportFilePath, req, res))
server.middlewares.use(`${publicPath}/index.html`, (req, res) => handleIndex(indexHtml, projectRoot, publicPath, supportFilePath, req, res))
},
handleHotUpdate: () => {
devServerEvents.emit('dev-server:compile:success')
Expand Down
27 changes: 0 additions & 27 deletions npm/vite-dev-server/src/makeHtmlPlugin.ts

This file was deleted.

5 changes: 3 additions & 2 deletions npm/vite-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { EventEmitter } from 'events'
const debug = Debug('cypress:vite-dev-server:start')

// TODO: Pull in types for Options so we can infer these
const serverConfig = (projectRoot: string, supportFilePath: string, devServerEvents: EventEmitter): InlineConfig => {
const serverConfig = (projectRoot: string, supportFilePath: string, publicPath: string, devServerEvents: EventEmitter): InlineConfig => {
return {
root: resolve(__dirname, projectRoot),
base: '/__cypress/src/',
plugins: [makeCypressPlugin(projectRoot, supportFilePath, devServerEvents)],
plugins: [makeCypressPlugin(projectRoot, supportFilePath, publicPath, devServerEvents)],
server: {
port: 0,
},
Expand All @@ -23,6 +23,7 @@ const resolveServerConfig = ({ viteConfig, options }: StartDevServer) => {
const defaultServerConfig = serverConfig(
options.config.projectRoot,
options.config.supportFile,
options.config.devServerPublicPathRoute,
options.devServerEvents,
)

Expand Down
6 changes: 3 additions & 3 deletions npm/webpack-dev-server/src/makeWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface UserWebpackDevServerOptions {
}

interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions, UserWebpackDevServerOptions {
webpackDevServerPublicPathRoute: string
devServerPublicPathRoute: string
isOpenMode: boolean
}

Expand All @@ -42,7 +42,7 @@ function getLazyCompilationWebpackConfig (options: MakeWebpackConfigOptions): we
}

export async function makeWebpackConfig (userWebpackConfig: webpack.Configuration, options: MakeWebpackConfigOptions): Promise<webpack.Configuration> {
const { projectRoot, webpackDevServerPublicPathRoute, files, supportFile, devServerEvents } = options
const { projectRoot, devServerPublicPathRoute, files, supportFile, devServerEvents } = options

debug(`User passed in webpack config with values %o`, userWebpackConfig)

Expand All @@ -52,7 +52,7 @@ export async function makeWebpackConfig (userWebpackConfig: webpack.Configuratio

const entry = path.resolve(__dirname, './browser.js')

const publicPath = mergePublicPath(webpackDevServerPublicPathRoute, userWebpackConfig?.output?.publicPath)
const publicPath = mergePublicPath(devServerPublicPathRoute, userWebpackConfig?.output?.publicPath)

const dynamicWebpackConfig = {
output: {
Expand Down
7 changes: 4 additions & 3 deletions npm/webpack-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export async function start ({ webpackConfig: userWebpackConfig, options, ...use
debug('User did not pass in any webpack configuration')
}

// @ts-expect-error ?? webpackDevServerPublicPathRoute is not a valid option of Cypress.Config
const { projectRoot, webpackDevServerPublicPathRoute, isTextTerminal } = options.config
// @ts-expect-error ?? devServerPublicPathRoute is not a valid option of Cypress.Config
const { projectRoot, devServerPublicPathRoute, isTextTerminal } = options.config

const webpackConfig = await makeWebpackConfig(userWebpackConfig || {}, {
files: options.specs,
projectRoot,
webpackDevServerPublicPathRoute,
devServerPublicPathRoute,
devServerEvents: options.devServerEvents,
supportFile: options.config.supportFile as string,
isOpenMode: !isTextTerminal,
Expand Down Expand Up @@ -57,6 +57,7 @@ export async function start ({ webpackConfig: userWebpackConfig, options, ...use
...userWebpackConfig.devServer,
hot: false,
inline: false,
publicPath: devServerPublicPathRoute,
}

return new WebpackDevServer(compiler, webpackDevServerConfig)
Expand Down
2 changes: 1 addition & 1 deletion npm/webpack-dev-server/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const config = {
projectRoot: root,
supportFile: '',
isTextTerminal: true,
webpackDevServerPublicPathRoute: root,
devServerPublicPathRoute: root,
} as any as Cypress.ResolvedConfigOptions & Cypress.RuntimeConfigOptions

describe('#startDevServer', () => {
Expand Down
8 changes: 2 additions & 6 deletions packages/server-ct/src/routes-ct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const createRoutes = ({
// to properly intercept and serve assets from the correct src root
// TODO: define a contract for dev-server plugins to configure this behavior
req.headers.__cypress_spec_path = req.params[0]
req.url = '/index.html'
req.url = `${config.devServerPublicPathRoute}/index.html`

// user the node proxy here instead of the network proxy
// to avoid the user accidentally intercepting and modifying
Expand All @@ -60,11 +60,7 @@ export const createRoutes = ({

// user app code + spec code
// default mounted to /__cypress/src/*
app.get(`${config.webpackDevServerPublicPathRoute}*`, (req, res) => {
// strip out the webpackDevServerPublicPath from the URL
// and forward the remaining params
req.url = `/${req.params[0]}`

app.get(`${config.devServerPublicPathRoute}*`, (req, res) => {
// user the node proxy here instead of the network proxy
// to avoid the user accidentally intercepting and modifying
// their own app.js files + spec.js files
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/config_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export const options = [
defaultValue: '/__socket.io',
isInternal: true,
}, {
name: 'webpackDevServerPublicPathRoute',
name: 'devServerPublicPathRoute',
defaultValue: '/__cypress/src',
isInternal: true,
}, {
Expand Down

4 comments on commit a97318c

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a97318c Mar 23, 2021

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 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:

npm install https://cdn.cypress.io/beta/npm/6.9.0/circle-develop-a97318c4ad07278a93b26c1fdfa01eac20d058fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a97318c Mar 23, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 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:

npm install https://cdn.cypress.io/beta/npm/6.7.2/appveyor-develop-a97318c4ad07278a93b26c1fdfa01eac20d058fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a97318c Mar 23, 2021

Choose a reason for hiding this comment

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

AppVeyor 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:

npm install https://cdn.cypress.io/beta/npm/6.7.2/appveyor-develop-a97318c4ad07278a93b26c1fdfa01eac20d058fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a97318c Mar 23, 2021

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:

npm install https://cdn.cypress.io/beta/npm/6.9.0/circle-develop-a97318c4ad07278a93b26c1fdfa01eac20d058fb/cypress.tgz

Please sign in to comment.