Skip to content

Commit 34e55a0

Browse files
committed
build: enable raw css files consumption via styled-jsx
1 parent 4beba43 commit 34e55a0

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

globals.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,23 @@ declare module '*.css' {
1212
const value: Record<string, string>
1313
export default value
1414
}
15+
16+
declare module '@zeit/next-css' {
17+
type Api = any
18+
export = Api
19+
}
20+
declare module 'styled-jsx/webpack' {
21+
type Api = { loader?: string }
22+
export = Api
23+
}
24+
25+
declare module 'styled-jsx/webpack-config' {
26+
type ValidTypes = 'scoped' | 'global' | 'resolve'
27+
type FileNameQuery = { query: { type: ValidTypes } }
28+
29+
export type Options = {
30+
type:
31+
| ValidTypes
32+
| ((fileName: string, options: FileNameQuery) => ValidTypes)
33+
}
34+
}

next.config.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,29 @@ const cfg = {
77
* @type {import('./next-config').WebpackConfig}
88
*/
99
webpack: (config, options) => {
10-
// config.module.rules.push({
11-
// test: /\.raw.css$/,
12-
// use: 'raw-loader',
13-
// })
10+
/**
11+
* @type {import('webpack').Rule}
12+
* @see https://github.com/zeit/styled-jsx#styles-in-regular-css-files
13+
*/
14+
const styledJsxRule = {
15+
test: /\.css$/,
16+
use: [
17+
options.defaultLoaders.babel,
18+
{
19+
loader: require('styled-jsx/webpack').loader,
20+
21+
options: /** @type {import('styled-jsx/webpack-config').Options} */ ({
22+
type: (fileName, options) =>
23+
fileName.endsWith('.module.css')
24+
? 'scoped'
25+
: options.query.type || 'global',
26+
}),
27+
},
28+
],
29+
}
30+
31+
config.module.rules.push(styledJsxRule)
32+
1433
return config
1534
},
1635
}
@@ -20,4 +39,4 @@ const config = withCSS({
2039
...cfg,
2140
})
2241

23-
module.exports = config
42+
module.exports = cfg

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@types/node": "12.12.7",
2828
"@types/react": "16.9.11",
2929
"@types/styled-jsx": "2.2.8",
30+
"@types/webpack": "4.41.0",
3031
"@zeit/next-css": "1.0.1",
3132
"husky": "3.0.9",
3233
"lint-staged": "9.4.2",

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"next-env.d.ts",
2626
"typings",
2727
"**/*.ts",
28-
"**/*.tsx"
28+
"**/*.tsx",
29+
"next.config.js"
2930
]
3031
}

yarn.lock

+43
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,11 @@
843843
dependencies:
844844
any-observable "^0.3.0"
845845

846+
"@types/anymatch@*":
847+
version "1.3.1"
848+
resolved "https://registry.yarnpkg.com/@types/anymatch/-/anymatch-1.3.1.tgz#336badc1beecb9dacc38bea2cf32adf627a8421a"
849+
integrity sha512-/+CRPXpBDpo2RK9C68N3b2cOvO0Cf5B9aPijHsoDQTHivnGSObdOF2BRQOYjojWTDy6nQvMjmqRXIxH55VjxxA==
850+
846851
"@types/events@*":
847852
version "3.0.0"
848853
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
@@ -893,13 +898,51 @@
893898
"@types/prop-types" "*"
894899
csstype "^2.2.0"
895900

901+
"@types/source-list-map@*":
902+
version "0.1.2"
903+
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
904+
integrity sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==
905+
896906
897907
version "2.2.8"
898908
resolved "https://registry.yarnpkg.com/@types/styled-jsx/-/styled-jsx-2.2.8.tgz#b50d13d8a3c34036282d65194554cf186bab7234"
899909
integrity sha512-Yjye9VwMdYeXfS71ihueWRSxrruuXTwKCbzue4+5b2rjnQ//AtyM7myZ1BEhNhBQ/nL/RE7bdToUoLln2miKvg==
900910
dependencies:
901911
"@types/react" "*"
902912

913+
"@types/tapable@*":
914+
version "1.0.4"
915+
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.4.tgz#b4ffc7dc97b498c969b360a41eee247f82616370"
916+
integrity sha512-78AdXtlhpCHT0K3EytMpn4JNxaf5tbqbLcbIRoQIHzpTIyjpxLQKRoxU55ujBXAtg3Nl2h/XWvfDa9dsMOd0pQ==
917+
918+
"@types/uglify-js@*":
919+
version "3.0.4"
920+
resolved "https://registry.yarnpkg.com/@types/uglify-js/-/uglify-js-3.0.4.tgz#96beae23df6f561862a830b4288a49e86baac082"
921+
integrity sha512-SudIN9TRJ+v8g5pTG8RRCqfqTMNqgWCKKd3vtynhGzkIIjxaicNAMuY5TRadJ6tzDu3Dotf3ngaMILtmOdmWEQ==
922+
dependencies:
923+
source-map "^0.6.1"
924+
925+
"@types/webpack-sources@*":
926+
version "0.1.5"
927+
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-0.1.5.tgz#be47c10f783d3d6efe1471ff7f042611bd464a92"
928+
integrity sha512-zfvjpp7jiafSmrzJ2/i3LqOyTYTuJ7u1KOXlKgDlvsj9Rr0x7ZiYu5lZbXwobL7lmsRNtPXlBfmaUD8eU2Hu8w==
929+
dependencies:
930+
"@types/node" "*"
931+
"@types/source-list-map" "*"
932+
source-map "^0.6.1"
933+
934+
935+
version "4.41.0"
936+
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.0.tgz#b813a044d8b0dec7dfcd7622fdbe327bde06eb9a"
937+
integrity sha512-tWkdf9nO0zFgAY/EumUKwrDUhraHKDqCPhwfFR/R8l0qnPdgb9le0Gzhvb7uzVpouuDGBgiE//ZdY+5jcZy2TA==
938+
dependencies:
939+
"@types/anymatch" "*"
940+
"@types/node" "*"
941+
"@types/tapable" "*"
942+
"@types/uglify-js" "*"
943+
"@types/webpack-sources" "*"
944+
source-map "^0.6.0"
945+
903946
"@webassemblyjs/[email protected]":
904947
version "1.8.5"
905948
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.8.5.tgz#51b1c5fe6576a34953bf4b253df9f0d490d9e359"

0 commit comments

Comments
 (0)