Skip to content

Commit

Permalink
Add sassOption implementation to support sass-embedded (#64577)
Browse files Browse the repository at this point in the history
This PR adds an option to use `sass-embedded`.

For large projects sass-embedded improves SCSS compilation time by over
50%.
See also #36160

Added a sassOption `implementation` to configure the sass-loader which
Sass implementation to use.
See also https://www.npmjs.com/package/sass-loader#implementation

I think this a similar approach to what is done for `additionalData`.

In order to support `sass-embedded`, `sass-loader` is upgraded to
12.6.0.

---------

Co-authored-by: Zack Tanner <[email protected]>
  • Loading branch information
2 people authored and ForsakenHarmony committed Aug 16, 2024
1 parent 2018e6a commit 26c9c63
Show file tree
Hide file tree
Showing 44 changed files with 1,051 additions and 602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub async fn maybe_add_sass_loader(
loader: "next/dist/compiled/sass-loader".into(),
options: take(
serde_json::json!({
"implementation": sass_options.get("implementation"),
"sourceMap": true,
"sassOptions": sass_options,
"additionalData": additional_data
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
"react-is": "18.2.0",
"react-refresh": "0.12.0",
"regenerator-runtime": "0.13.4",
"sass-loader": "12.4.0",
"sass-loader": "12.6.0",
"schema-utils2": "npm:[email protected]",
"schema-utils3": "npm:[email protected]",
"semver": "7.3.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/build/webpack/config/blocks/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const css = curry(async function css(
const {
prependData: sassPrependData,
additionalData: sassAdditionalData,
implementation: sassImplementation,
...sassOptions
} = ctx.sassOptions

Expand All @@ -164,6 +165,7 @@ export const css = curry(async function css(
{
loader: require.resolve('next/dist/compiled/sass-loader'),
options: {
implementation: sassImplementation,
// Source maps are required so that `resolve-url-loader` can locate
// files original to their source directory.
sourceMap: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/compiled/sass-loader/cjs.js

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 46 additions & 34 deletions test/e2e/app-dir/scss/3rd-party-module/3rd-party-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,54 @@
import { nextTestSetup } from 'e2e-utils'
import { colorToRgb } from 'next-test-utils'

describe('3rd Party CSS Module Support', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
sass: '1.54.0',
describe.each([
{ dependencies: { sass: '1.54.0' }, nextConfig: undefined },
{
dependencies: { 'sass-embedded': '1.75.0' },
nextConfig: {
sassOptions: {
implementation: 'sass-embedded',
},
},
})
},
])(
'3rd Party CSS Module Support ($dependencies)',
({ dependencies, nextConfig }) => {
const { next } = nextTestSetup({
files: __dirname,
dependencies,
nextConfig,
})

it('should render the module', async () => {
const browser = await next.browser('/')
// Bar
expect(
await browser
.elementByCss('#verify-div .bar')
.getComputedCss('background-color')
).toBe(colorToRgb('blue'))
it('should render the module', async () => {
const browser = await next.browser('/')
// Bar
expect(
await browser
.elementByCss('#verify-div .bar')
.getComputedCss('background-color')
).toBe(colorToRgb('blue'))

// Baz
expect(
await browser
.elementByCss('#verify-div .baz')
.getComputedCss('background-color')
).toBe(colorToRgb('blue'))
// Baz
expect(
await browser
.elementByCss('#verify-div .baz')
.getComputedCss('background-color')
).toBe(colorToRgb('blue'))

// Lol
expect(
await browser
.elementByCss('#verify-div .lol')
.getComputedCss('background-color')
).toBe(colorToRgb('red'))
// Lol
expect(
await browser
.elementByCss('#verify-div .lol')
.getComputedCss('background-color')
).toBe(colorToRgb('red'))

// Lel
expect(
await browser
.elementByCss('#verify-div .lel')
.getComputedCss('background-color')
).toBe(colorToRgb('green'))
})
})
// Lel
expect(
await browser
.elementByCss('#verify-div .lel')
.getComputedCss('background-color')
).toBe(colorToRgb('green'))
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,37 @@
import { nextTestSetup } from 'e2e-utils'
import { colorToRgb } from 'next-test-utils'

describe('Basic Module Additional Data Support', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
sass: '1.54.0',
const sassOptions = {
additionalData: `
$var: red;
`,
}

describe.each([
{ dependencies: { sass: '1.54.0' }, nextConfig: { sassOptions } },
{
dependencies: { 'sass-embedded': '1.75.0' },
nextConfig: {
sassOptions: {
...sassOptions,
implementation: 'sass-embedded',
},
},
})
},
])(
'Basic Module Additional Data Support ($dependencies)',
({ dependencies, nextConfig }) => {
const { next } = nextTestSetup({
files: __dirname,
dependencies,
nextConfig,
})

it('should render the module', async () => {
const browser = await next.browser('/')
expect(
await browser.elementByCss('#verify-red').getComputedCss('color')
).toBe(colorToRgb('red'))
})
})
it('should render the module', async () => {
const browser = await next.browser('/')
expect(
await browser.elementByCss('#verify-red').getComputedCss('color')
).toBe(colorToRgb('red'))
})
}
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
/* eslint-env jest */

import { nextTestSetup } from 'e2e-utils'
import { colorToRgb } from 'next-test-utils'

describe('Basic Module Include Paths Support', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
sass: '1.54.0',
const sassOptions = {
includePaths: ['./styles'],
}

describe.each([
{ dependencies: { sass: '1.54.0' }, nextConfig: { sassOptions } },
{
dependencies: { 'sass-embedded': '1.75.0' },
nextConfig: {
sassOptions: {
...sassOptions,
implementation: 'sass-embedded',
},
},
})
},
])(
'Basic Module Include Paths Support ($dependencies)',
({ dependencies, nextConfig }) => {
const { next } = nextTestSetup({
files: __dirname,
dependencies,
nextConfig,
})

it('should render the module', async () => {
const browser = await next.browser('/')
expect(
await browser.elementByCss('#verify-red').getComputedCss('color')
).toBe(colorToRgb('red'))
})
})
it('should render the module', async () => {
const browser = await next.browser('/')
expect(
await browser.elementByCss('#verify-red').getComputedCss('color')
).toBe(colorToRgb('red'))
})
}
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,37 @@
import { nextTestSetup } from 'e2e-utils'
import { colorToRgb } from 'next-test-utils'

describe('Basic Module Prepend Data Support', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
sass: '1.54.0',
const sassOptions = {
prependData: `
$var: red;
`,
}

describe.each([
{ dependencies: { sass: '1.54.0' }, nextConfig: { sassOptions } },
{
dependencies: { 'sass-embedded': '1.75.0' },
nextConfig: {
sassOptions: {
...sassOptions,
implementation: 'sass-embedded',
},
},
})
},
])(
'Basic Module Prepend Data Support ($dependencies)',
({ dependencies, nextConfig }) => {
const { next } = nextTestSetup({
files: __dirname,
dependencies,
nextConfig,
})

it('should render the module', async () => {
const browser = await next.browser('/')
expect(
await browser.elementByCss('#verify-red').getComputedCss('color')
).toBe(colorToRgb('red'))
})
})
it('should render the module', async () => {
const browser = await next.browser('/')
expect(
await browser.elementByCss('#verify-red').getComputedCss('color')
).toBe(colorToRgb('red'))
})
}
)

This file was deleted.

Loading

0 comments on commit 26c9c63

Please sign in to comment.