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

chore: correctly resolve url sass bundle in Angular CT #25191

Merged
merged 4 commits into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions npm/webpack-dev-server/src/helpers/angularHandler.ts
@@ -1,10 +1,13 @@
import * as fs from 'fs-extra'
import { tmpdir } from 'os'
import * as path from 'path'
import type { Configuration } from 'webpack'
import type { Configuration, RuleSetRule } from 'webpack'
import type { PresetHandlerResult, WebpackDevServerConfig } from '../devServer'
import { dynamicAbsoluteImport, dynamicImport } from '../dynamic-import'
import { sourceDefaultWebpackDependencies } from './sourceRelativeWebpackModules'
import debugLib from 'debug'

const debug = debugLib('cypress:webpack-dev-server:angularHandler')

export type BuildOptions = Record<string, any>

Expand Down Expand Up @@ -250,7 +253,33 @@ async function getAngularCliWebpackConfig (devServerConfig: AngularWebpackDevSer
const { config } = await generateBrowserWebpackConfigFromContext(
buildOptions,
context,
(wco: any) => [getCommonConfig(wco), getStylesConfig(wco)],
(wco: any) => {
const stylesConfig = getStylesConfig(wco)

// We modify resolve-url-loader and set `root` to be `projectRoot` + `sourceRoot` to ensure
// imports in scss, sass, etc are correctly resolved.
// https://github.com/cypress-io/cypress/issues/24272
stylesConfig.module.rules.forEach((rule: RuleSetRule) => {
rule.rules?.forEach((ruleSet) => {
if (!Array.isArray(ruleSet.use)) {
return
}

ruleSet.use.map((loader) => {
if (typeof loader !== 'object' || typeof loader.options !== 'object' || !loader.loader?.includes('resolve-url-loader')) {
return
}

const root = path.join(devServerConfig.cypressConfig.projectRoot, projectConfig.sourceRoot)

debug('Adding root %s to resolve-url-loader options', root)
loader.options.root = path.join(devServerConfig.cypressConfig.projectRoot, projectConfig.sourceRoot)
})
})
})

return [getCommonConfig(wco), stylesConfig]
},
)

delete config.entry.main
Expand Down
@@ -1,3 +1,4 @@
// Keep this test very simple as "@cypress/schematic" relies on it to run smoke tests
import { AppComponent } from './app.component'

it('should', () => {
Expand Down
@@ -1 +1 @@
<h1>Hello World your app is running!</h1>
<h1>Hello World your app is running!</h1>
@@ -1,3 +1,3 @@
h1 {
color: red
}
}
@@ -0,0 +1,6 @@
.test-img {
background-image: url("/assets/test.png");
background-size: contain;
height: 100px;
width: 100px;
}
@@ -0,0 +1,8 @@
import { Component } from '@angular/core'

@Component({
selector: 'app-url-image',
template: `<div class="test-img"></div>`,
styleUrls: ['./url-image.component.scss'],
})
export class UrlImageComponent {}
38 changes: 25 additions & 13 deletions system-tests/project-fixtures/angular/src/app/mount.cy.ts
Expand Up @@ -20,6 +20,7 @@ import { LogoComponent } from './components/logo.component'
import { TransientService, TransientServicesComponent } from './components/transient-services.component'
import { ComponentProviderComponent, MessageService } from './components/component-provider.component'
import { Cart, ProductComponent } from './components/cart.component'
import { UrlImageComponent } from './components/url-image.component'

@Component({
template: `<app-projection>Hello World</app-projection>`,
Expand Down Expand Up @@ -360,9 +361,20 @@ describe('angular mount', () => {
cy.get('p').should('have.text', 'Hi . ngOnInit fired: true and ngOnChanges fired: false and conditionalName: false')
})

it('can load static assets', () => {
cy.mount(LogoComponent)
cy.get('img').should('be.visible').and('have.prop', 'naturalWidth').should('be.greaterThan', 0)
context('assets', () => {
it('can load static assets from <img src="..." />', () => {
cy.mount(LogoComponent)
cy.get('img').should('be.visible').and('have.prop', 'naturalWidth').should('be.greaterThan', 0)
})

it('can load root relative scss "url()" assets', () => {
cy.mount(UrlImageComponent)
cy.get('.test-img')
.invoke('css', 'background-image')
.then((img) => {
expect(img).to.contain('__cypress/src/test.png')
})
})
})

context('dependency injection', () => {
Expand Down Expand Up @@ -430,27 +442,27 @@ describe('angular mount', () => {

describe('teardown', () => {
beforeEach(() => {
cy.get("[id^=root]").should("not.exist");
});
cy.get('[id^=root]').should('not.exist')
})

it("should mount", () => {
cy.mount(ButtonOutputComponent);
});
it('should mount', () => {
cy.mount(ButtonOutputComponent)
})

it('should remove previous mounted component', () => {
cy.mount(ChildComponent, {componentProperties: { msg: 'Render 1' }})
cy.mount(ChildComponent, { componentProperties: { msg: 'Render 1' } })
cy.contains('Render 1')
cy.mount(ChildComponent, {componentProperties: { msg: 'Render 2' }})
cy.mount(ChildComponent, { componentProperties: { msg: 'Render 2' } })
cy.contains('Render 2')

cy.contains('Render 1').should('not.exist')
cy.get('[id^=root]').children().should('have.length', 1)
})
});
})

it('should error when passing in undecorated component', () => {
Cypress.on('fail', (err) => {
expect(err.message).contain("Please add a @Pipe/@Directive/@Component");
expect(err.message).contain('Please add a @Pipe/@Directive/@Component')

return false
})
Expand All @@ -459,4 +471,4 @@ describe('angular mount', () => {

cy.mount(MyClass)
})
});
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.