Unverified Commit a4692d40 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add support for jsxImportSource in tsconfig/jsconfig (#31358)

parent 06f3d398
...@@ -9,6 +9,7 @@ function getBaseSWCOptions({ ...@@ -9,6 +9,7 @@ function getBaseSWCOptions({
styledComponents, styledComponents,
paths, paths,
baseUrl, baseUrl,
importSource,
}) { }) {
const isTSFile = filename.endsWith('.ts') const isTSFile = filename.endsWith('.ts')
const isTypeScript = isTSFile || filename.endsWith('.tsx') const isTypeScript = isTSFile || filename.endsWith('.tsx')
...@@ -30,6 +31,7 @@ function getBaseSWCOptions({ ...@@ -30,6 +31,7 @@ function getBaseSWCOptions({
transform: { transform: {
react: { react: {
importSource: importSource || 'react',
runtime: 'automatic', runtime: 'automatic',
pragma: 'React.createElement', pragma: 'React.createElement',
pragmaFrag: 'React.Fragment', pragmaFrag: 'React.Fragment',
...@@ -107,6 +109,7 @@ export function getLoaderSWCOptions({ ...@@ -107,6 +109,7 @@ export function getLoaderSWCOptions({
isPageFile, isPageFile,
hasReactRefresh, hasReactRefresh,
styledComponents, styledComponents,
importSource,
}) { }) {
let baseOptions = getBaseSWCOptions({ let baseOptions = getBaseSWCOptions({
filename, filename,
...@@ -114,6 +117,7 @@ export function getLoaderSWCOptions({ ...@@ -114,6 +117,7 @@ export function getLoaderSWCOptions({
globalWindow: !isServer, globalWindow: !isServer,
hasReactRefresh, hasReactRefresh,
styledComponents, styledComponents,
importSource,
}) })
const isNextDist = nextDistPath.test(filename) const isNextDist = nextDistPath.test(filename)
......
...@@ -321,6 +321,10 @@ export default async function getBaseWebpackConfig( ...@@ -321,6 +321,10 @@ export default async function getBaseWebpackConfig(
runWebpackSpan: Span runWebpackSpan: Span
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const { useTypeScript, jsConfig, resolvedBaseUrl } = await loadJsConfig(
dir,
config
)
const supportedBrowsers = await getSupportedBrowsers(dir, dev) const supportedBrowsers = await getSupportedBrowsers(dir, dev)
const hasRewrites = const hasRewrites =
rewrites.beforeFiles.length > 0 || rewrites.beforeFiles.length > 0 ||
...@@ -438,6 +442,7 @@ export default async function getBaseWebpackConfig( ...@@ -438,6 +442,7 @@ export default async function getBaseWebpackConfig(
pagesDir, pagesDir,
hasReactRefresh: !isMiddleware && hasReactRefresh, hasReactRefresh: !isMiddleware && hasReactRefresh,
styledComponents: config.experimental.styledComponents, styledComponents: config.experimental.styledComponents,
importSource: jsConfig?.compilerOptions?.jsxImportSource,
}, },
} }
: { : {
...@@ -521,11 +526,6 @@ export default async function getBaseWebpackConfig( ...@@ -521,11 +526,6 @@ export default async function getBaseWebpackConfig(
} as ClientEntries) } as ClientEntries)
: undefined : undefined
const { useTypeScript, jsConfig, resolvedBaseUrl } = await loadJsConfig(
dir,
config
)
function getReactProfilingInProduction() { function getReactProfilingInProduction() {
if (reactProductionProfiling) { if (reactProductionProfiling) {
return { return {
......
...@@ -35,8 +35,13 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -35,8 +35,13 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
let loaderOptions = this.getOptions() || {} let loaderOptions = this.getOptions() || {}
const { isServer, pagesDir, hasReactRefresh, styledComponents } = const {
loaderOptions isServer,
pagesDir,
hasReactRefresh,
styledComponents,
importSource,
} = loaderOptions
const isPageFile = filename.startsWith(pagesDir) const isPageFile = filename.startsWith(pagesDir)
const swcOptions = getLoaderSWCOptions({ const swcOptions = getLoaderSWCOptions({
...@@ -47,6 +52,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -47,6 +52,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
development: this.mode === 'development', development: this.mode === 'development',
hasReactRefresh, hasReactRefresh,
styledComponents, styledComponents,
importSource,
}) })
const programmaticOptions = { const programmaticOptions = {
......
import { join } from 'path'
import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
describe('theme-ui SWC option', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'jsconfig.json': new FileRef(join(__dirname, 'theme-ui/jsconfig.json')),
pages: new FileRef(join(__dirname, 'theme-ui/pages')),
'theme.js': new FileRef(join(__dirname, 'theme-ui/theme.js')),
},
dependencies: {
'theme-ui': '0.12.0',
},
})
})
afterAll(() => next.destroy())
it('should have theme provided styling', async () => {
let browser
try {
browser = await webdriver(next.appPort, '/')
const color = await browser.elementByCss('#hello').getComputedCss('color')
expect(color).toBe('rgb(51, 51, 238)')
} finally {
if (browser) {
await browser.close()
}
}
})
})
{
"compilerOptions": {
"jsxImportSource": "theme-ui"
}
}
// basic usage
import { ThemeProvider } from 'theme-ui'
import theme from '../theme'
function MyApp({ Component, pageProps }) {
return (
<ThemeProvider theme={theme}>
<Component {...pageProps} />
</ThemeProvider>
)
}
export default MyApp
export default function Home() {
return (
<div
id="hello"
sx={{
fontWeight: 'bold',
fontSize: 4, // picks up value from `theme.fontSizes[4]`
color: 'primary', // picks up value from `theme.colors.primary`
}}
>
Hello
</div>
)
}
// example theme.js
export default {
fonts: {
body: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif',
heading: '"Avenir Next", sans-serif',
monospace: 'Menlo, monospace',
},
colors: {
text: '#000',
background: '#fff',
primary: '#33e',
},
}
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