Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(babel-preset-gatsby): allow setting importSource on preset-react #29260

Merged
merged 5 commits into from
Feb 25, 2021
20 changes: 20 additions & 0 deletions packages/babel-preset-gatsby/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,24 @@ describe(`babel-preset-gatsby`, () => {
}),
])
})

it(`Allows to configure react importSource`, () => {
const { presets } = preset(null, {
reactImportSource: `@emotion/react`,
reactRuntime: `automatic`,
})

expect(presets[1]).toEqual([
expect.stringContaining(path.join(`@babel`, `preset-react`)),
expect.objectContaining({
importSource: `@emotion/react`,
}),
])
})

it(`Fails to configure react importSource if source is classic`, () => {
expect(() => preset(null, { reactImportSource: `@emotion/react` })).toThrow(
`@babel/preset-react\` requires reactRuntime \`automatic\` in order to use \`importSource\`.`
)
})
})
9 changes: 8 additions & 1 deletion packages/babel-preset-gatsby/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function loadCachedConfig() {
}

export default function preset(_, options = {}) {
let { targets = null } = options
let { targets = null, reactImportSource = null } = options

const stage = options.stage || `test`
const pluginBabelConfig = loadCachedConfig()
Expand All @@ -54,6 +54,12 @@ export default function preset(_, options = {}) {
}
}

if (reactImportSource && options.reactRuntime !== `automatic`) {
throw Error(
`\`@babel/preset-react\` requires reactRuntime \`automatic\` in order to use \`importSource\`.`
)
}

return {
presets: [
[
Expand Down Expand Up @@ -87,6 +93,7 @@ export default function preset(_, options = {}) {
: `React.createElement`,
development: stage === `develop`,
runtime: options.reactRuntime || `classic`,
...(reactImportSource && { importSource: reactImportSource }),
},
],
],
Expand Down