Unverified Commit 878ee56b authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix New CSS Support Media Paths (#9916)

* Fix CSS Media Paths

* update test

* Use absolute path

* Add new tests

* fix import
parent fbcc0e81
...@@ -869,6 +869,7 @@ export default async function getBaseWebpackConfig( ...@@ -869,6 +869,7 @@ export default async function getBaseWebpackConfig(
isServer, isServer,
hasSupportCss: !!config.experimental.css, hasSupportCss: !!config.experimental.css,
hasExperimentalData: !!config.experimental.ampBindInitData, hasExperimentalData: !!config.experimental.ampBindInitData,
assetPrefix: config.assetPrefix || '',
}) })
if (typeof config.webpack === 'function') { if (typeof config.webpack === 'function') {
......
...@@ -14,8 +14,10 @@ import { getPostCssPlugins } from './plugins' ...@@ -14,8 +14,10 @@ import { getPostCssPlugins } from './plugins'
function getClientStyleLoader({ function getClientStyleLoader({
isDevelopment, isDevelopment,
assetPrefix,
}: { }: {
isDevelopment: boolean isDevelopment: boolean
assetPrefix: string
}): webpack.RuleSetUseItem { }): webpack.RuleSetUseItem {
return isDevelopment return isDevelopment
? { ? {
...@@ -62,7 +64,7 @@ function getClientStyleLoader({ ...@@ -62,7 +64,7 @@ function getClientStyleLoader({
} }
: { : {
loader: MiniCssExtractPlugin.loader, loader: MiniCssExtractPlugin.loader,
options: {}, options: { publicPath: `${assetPrefix}/_next/` },
} }
} }
...@@ -150,7 +152,10 @@ export const css = curry(async function css( ...@@ -150,7 +152,10 @@ export const css = curry(async function css(
// Add appropriate development more or production mode style // Add appropriate development more or production mode style
// loader // loader
ctx.isClient && ctx.isClient &&
getClientStyleLoader({ isDevelopment: ctx.isDevelopment }), getClientStyleLoader({
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
}),
// Resolve CSS `@import`s and `url()`s // Resolve CSS `@import`s and `url()`s
{ {
...@@ -227,7 +232,10 @@ export const css = curry(async function css( ...@@ -227,7 +232,10 @@ export const css = curry(async function css(
use: [ use: [
// Add appropriate development more or production mode style // Add appropriate development more or production mode style
// loader // loader
getClientStyleLoader({ isDevelopment: ctx.isDevelopment }), getClientStyleLoader({
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
}),
// Resolve CSS `@import`s and `url()`s // Resolve CSS `@import`s and `url()`s
{ {
......
...@@ -13,6 +13,7 @@ export async function build( ...@@ -13,6 +13,7 @@ export async function build(
isServer, isServer,
hasSupportCss, hasSupportCss,
hasExperimentalData, hasExperimentalData,
assetPrefix,
}: { }: {
rootDirectory: string rootDirectory: string
customAppFile: string | null customAppFile: string | null
...@@ -20,6 +21,7 @@ export async function build( ...@@ -20,6 +21,7 @@ export async function build(
isServer: boolean isServer: boolean
hasSupportCss: boolean hasSupportCss: boolean
hasExperimentalData: boolean hasExperimentalData: boolean
assetPrefix: string
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const ctx: ConfigurationContext = { const ctx: ConfigurationContext = {
...@@ -29,6 +31,11 @@ export async function build( ...@@ -29,6 +31,11 @@ export async function build(
isProduction: !isDevelopment, isProduction: !isDevelopment,
isServer, isServer,
isClient: !isServer, isClient: !isServer,
assetPrefix: assetPrefix
? assetPrefix.endsWith('/')
? assetPrefix.slice(0, -1)
: assetPrefix
: '',
} }
const fn = pipe( const fn = pipe(
......
...@@ -9,6 +9,8 @@ export type ConfigurationContext = { ...@@ -9,6 +9,8 @@ export type ConfigurationContext = {
isServer: boolean isServer: boolean
isClient: boolean isClient: boolean
assetPrefix: string
} }
export type ConfigurationFn = ( export type ConfigurationFn = (
......
<?xml version="1.0" encoding="UTF-8"?>
<svg width="113px" height="100px" viewBox="0 0 113 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
<title>Logotype - White</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="196.572434%" y1="228.815483%" x2="50%" y2="50%" id="linearGradient-1">
<stop stop-color="#000000" offset="0%"></stop>
<stop stop-color="#FFFFFF" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="White-Triangle" transform="translate(-294.000000, -150.000000)" fill="url(#linearGradient-1)">
<polygon id="Logotype---White" points="350.5 150 407 250 294 250"></polygon>
</g>
</g>
</svg>
const config = require('../next.config.js')
module.exports = {
...config,
assetPrefix: '/foo/',
}
import React from 'react'
import App from 'next/app'
import '../styles/global1.css'
import '../styles/global2.css'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}
export default MyApp
export default function Home() {
return <div className="red-text">This text should be red.</div>
}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="114px" height="100px" viewBox="0 0 114 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
<title>Logotype - Black</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="100.929941%" y1="181.283245%" x2="41.7687834%" y2="100%" id="linearGradient-1">
<stop stop-color="#FFFFFF" offset="0%"></stop>
<stop stop-color="#000000" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Black-Triangle" transform="translate(-293.000000, -150.000000)" fill="url(#linearGradient-1)">
<polygon id="Logotype---Black" points="350 150 407 250 293 250"></polygon>
</g>
</g>
</svg>
<?xml version="1.0" encoding="UTF-8"?>
<svg width="114px" height="100px" viewBox="0 0 114 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
<title>Logotype - Black</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="100.929941%" y1="181.283245%" x2="41.7687834%" y2="100%" id="linearGradient-1">
<stop stop-color="#FFFFFF" offset="0%"></stop>
<stop stop-color="#000000" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Black-Triangle" transform="translate(-293.000000, -150.000000)" fill="url(#linearGradient-1)">
<polygon id="Logotype---Black" points="350 150 407 250 293 250"></polygon>
</g>
</g>
</svg>
.red-text {
color: red;
background-image: url('./dark.svg') url(dark2.svg);
}
@import './global2b.css';
.blue-text {
color: blue;
}
.blue-text {
color: orange;
font-weight: bolder;
background-image: url(../assets/light.svg);
}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="113px" height="100px" viewBox="0 0 113 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
<title>Logotype - White</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="196.572434%" y1="228.815483%" x2="50%" y2="50%" id="linearGradient-1">
<stop stop-color="#000000" offset="0%"></stop>
<stop stop-color="#FFFFFF" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="White-Triangle" transform="translate(-294.000000, -150.000000)" fill="url(#linearGradient-1)">
<polygon id="Logotype---White" points="350.5 150 407 250 294 250"></polygon>
</g>
</g>
</svg>
const config = require('../next.config.js')
module.exports = {
...config,
assetPrefix: '/foo',
}
import React from 'react'
import App from 'next/app'
import '../styles/global1.css'
import '../styles/global2.css'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}
export default MyApp
export default function Home() {
return <div className="red-text">This text should be red.</div>
}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="114px" height="100px" viewBox="0 0 114 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
<title>Logotype - Black</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="100.929941%" y1="181.283245%" x2="41.7687834%" y2="100%" id="linearGradient-1">
<stop stop-color="#FFFFFF" offset="0%"></stop>
<stop stop-color="#000000" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Black-Triangle" transform="translate(-293.000000, -150.000000)" fill="url(#linearGradient-1)">
<polygon id="Logotype---Black" points="350 150 407 250 293 250"></polygon>
</g>
</g>
</svg>
<?xml version="1.0" encoding="UTF-8"?>
<svg width="114px" height="100px" viewBox="0 0 114 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
<title>Logotype - Black</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="100.929941%" y1="181.283245%" x2="41.7687834%" y2="100%" id="linearGradient-1">
<stop stop-color="#FFFFFF" offset="0%"></stop>
<stop stop-color="#000000" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Black-Triangle" transform="translate(-293.000000, -150.000000)" fill="url(#linearGradient-1)">
<polygon id="Logotype---Black" points="350 150 407 250 293 250"></polygon>
</g>
</g>
</svg>
.red-text {
color: red;
background-image: url('./dark.svg') url(dark2.svg);
}
@import './global2b.css';
.blue-text {
color: blue;
}
.blue-text {
color: orange;
font-weight: bolder;
background-image: url(../assets/light.svg);
}
...@@ -469,7 +469,7 @@ describe('CSS Support', () => { ...@@ -469,7 +469,7 @@ describe('CSS Support', () => {
}) })
}) })
describe('CSS URL via `file-loader', () => { describe('CSS URL via `file-loader`', () => {
const appDir = join(fixturesDir, 'url-global') const appDir = join(fixturesDir, 'url-global')
beforeAll(async () => { beforeAll(async () => {
...@@ -490,7 +490,97 @@ describe('CSS Support', () => { ...@@ -490,7 +490,97 @@ describe('CSS Support', () => {
expect(cssFiles.length).toBe(1) expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8') const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim()).toMatch( expect(cssContent.replace(/\/\*.*?\*\//g, '').trim()).toMatch(
/^\.red-text\{color:red;background-image:url\(static\/media\/dark\.[a-z0-9]{32}\.svg\) url\(static\/media\/dark2\.[a-z0-9]{32}\.svg\)\}\.blue-text\{color:orange;font-weight:bolder;background-image:url\(static\/media\/light\.[a-z0-9]{32}\.svg\);color:#00f\}$/ /^\.red-text\{color:red;background-image:url\(\/_next\/static\/media\/dark\.[a-z0-9]{32}\.svg\) url\(\/_next\/static\/media\/dark2\.[a-z0-9]{32}\.svg\)\}\.blue-text\{color:orange;font-weight:bolder;background-image:url\(\/_next\/static\/media\/light\.[a-z0-9]{32}\.svg\);color:#00f\}$/
)
const mediaFiles = await readdir(mediaFolder)
expect(mediaFiles.length).toBe(3)
expect(
mediaFiles
.map(fileName =>
/^(.+?)\..{32}\.(.+?)$/
.exec(fileName)
.slice(1)
.join('.')
)
.sort()
).toMatchInlineSnapshot(`
Array [
"dark.svg",
"dark2.svg",
"light.svg",
]
`)
})
})
describe('CSS URL via `file-loader` and asset prefix (1)', () => {
const appDir = join(fixturesDir, 'url-global-asset-prefix-1')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should build successfully', async () => {
await nextBuild(appDir)
})
it(`should've emitted expected files`, async () => {
const cssFolder = join(appDir, '.next/static/css')
const mediaFolder = join(appDir, '.next/static/media')
const files = await readdir(cssFolder)
const cssFiles = files.filter(f => /\.css$/.test(f))
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim()).toMatch(
/^\.red-text\{color:red;background-image:url\(\/foo\/_next\/static\/media\/dark\.[a-z0-9]{32}\.svg\) url\(\/foo\/_next\/static\/media\/dark2\.[a-z0-9]{32}\.svg\)\}\.blue-text\{color:orange;font-weight:bolder;background-image:url\(\/foo\/_next\/static\/media\/light\.[a-z0-9]{32}\.svg\);color:#00f\}$/
)
const mediaFiles = await readdir(mediaFolder)
expect(mediaFiles.length).toBe(3)
expect(
mediaFiles
.map(fileName =>
/^(.+?)\..{32}\.(.+?)$/
.exec(fileName)
.slice(1)
.join('.')
)
.sort()
).toMatchInlineSnapshot(`
Array [
"dark.svg",
"dark2.svg",
"light.svg",
]
`)
})
})
describe('CSS URL via `file-loader` and asset prefix (2)', () => {
const appDir = join(fixturesDir, 'url-global-asset-prefix-2')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should build successfully', async () => {
await nextBuild(appDir)
})
it(`should've emitted expected files`, async () => {
const cssFolder = join(appDir, '.next/static/css')
const mediaFolder = join(appDir, '.next/static/media')
const files = await readdir(cssFolder)
const cssFiles = files.filter(f => /\.css$/.test(f))
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim()).toMatch(
/^\.red-text\{color:red;background-image:url\(\/foo\/_next\/static\/media\/dark\.[a-z0-9]{32}\.svg\) url\(\/foo\/_next\/static\/media\/dark2\.[a-z0-9]{32}\.svg\)\}\.blue-text\{color:orange;font-weight:bolder;background-image:url\(\/foo\/_next\/static\/media\/light\.[a-z0-9]{32}\.svg\);color:#00f\}$/
) )
const mediaFiles = await readdir(mediaFolder) const mediaFiles = await readdir(mediaFolder)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment