Skip to content

Commit ea7c1e1

Browse files
ijjktimneutkensTimer
authored
Add support for SCSS options (#11063)
* Add support for SCSS includePaths * Support sassOptions instead of just includePaths Co-authored-by: Tim Neutkens <[email protected]> Co-authored-by: Joe Haddad <[email protected]>
1 parent a231315 commit ea7c1e1

File tree

10 files changed

+60
-0
lines changed

10 files changed

+60
-0
lines changed

packages/next/build/webpack-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ export default async function getBaseWebpackConfig(
897897
hasSupportCss: !!config.experimental.css,
898898
hasSupportScss: !!config.experimental.scss,
899899
assetPrefix: config.assetPrefix || '',
900+
sassOptions: config.experimental.sassOptions,
900901
})
901902

902903
if (typeof config.webpack === 'function') {

packages/next/build/webpack/config/blocks/css/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const css = curry(async function css(
4343
// Source maps are required so that `resolve-url-loader` can locate
4444
// files original to their source directory.
4545
sourceMap: true,
46+
sassOptions: ctx.sassOptions,
4647
},
4748
},
4849
// Then, `sass-loader` will have passed-through CSS imports as-is instead

packages/next/build/webpack/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export async function build(
1313
hasSupportCss,
1414
hasSupportScss,
1515
assetPrefix,
16+
sassOptions,
1617
}: {
1718
rootDirectory: string
1819
customAppFile: string | null
@@ -21,6 +22,7 @@ export async function build(
2122
hasSupportCss: boolean
2223
hasSupportScss: boolean
2324
assetPrefix: string
25+
sassOptions: any
2426
}
2527
): Promise<webpack.Configuration> {
2628
const ctx: ConfigurationContext = {
@@ -35,6 +37,7 @@ export async function build(
3537
? assetPrefix.slice(0, -1)
3638
: assetPrefix
3739
: '',
40+
sassOptions,
3841
}
3942

4043
const fn = pipe(base(ctx), css(hasSupportCss, hasSupportScss, ctx))

packages/next/build/webpack/config/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type ConfigurationContext = {
1111
isClient: boolean
1212

1313
assetPrefix: string
14+
15+
sassOptions: any
1416
}
1517

1618
export type ConfigurationFn = (

packages/next/next-server/server/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const defaultConfig: { [key: string]: any } = {
5252
reactMode: 'legacy',
5353
workerThreads: false,
5454
basePath: '',
55+
sassOptions: {},
5556
},
5657
future: {
5758
excludeDefaultMomentLocales: false,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('path')
2+
3+
module.exports = {
4+
experimental: {
5+
sassOptions: {
6+
includePaths: [path.join(__dirname, 'styles')],
7+
},
8+
},
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { redText } from './index.module.scss'
2+
3+
export default function Home() {
4+
return (
5+
<div id="verify-red" className={redText}>
6+
This text should be red.
7+
</div>
8+
)
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import '_vars.scss';
2+
3+
.redText {
4+
color: $var;
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$var: red;

test/integration/scss/test/index.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@ describe('SCSS Support', () => {
7878
})
7979
})
8080

81+
describe('Basic Module Include Paths Support', () => {
82+
const appDir = join(fixturesDir, 'basic-module-include-paths')
83+
84+
beforeAll(async () => {
85+
await remove(join(appDir, '.next'))
86+
})
87+
88+
it('should compile successfully', async () => {
89+
const { code, stdout } = await nextBuild(appDir, [], {
90+
stdout: true,
91+
})
92+
expect(code).toBe(0)
93+
expect(stdout).toMatch(/Compiled successfully/)
94+
})
95+
96+
it(`should've emitted a single CSS file`, async () => {
97+
const cssFolder = join(appDir, '.next/static/css')
98+
99+
const files = await readdir(cssFolder)
100+
const cssFiles = files.filter(f => /\.css$/.test(f))
101+
102+
expect(cssFiles.length).toBe(1)
103+
expect(await readFile(join(cssFolder, cssFiles[0]), 'utf8')).toContain(
104+
'color:red'
105+
)
106+
})
107+
})
108+
81109
describe('Basic Global Support with src/ dir', () => {
82110
const appDir = join(fixturesDir, 'single-global-src')
83111

0 commit comments

Comments
 (0)