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

fix(babel-preset-gatsby): remove prop-types in production for dependencies #23609

Merged
merged 4 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/babel-preset-gatsby/src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

import path from "path"

interface IPresetOptions {
stage?: "build-javascript" | "build-html" | "develop" | "develop-html"
}

// export default is required here because it is passed directly to webpack
// via require.resolve
// This function has a better inference than would be beneficial to type, and it's relatively easy to grok.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default () => {
export default (_?: unknown, options: IPresetOptions = {}) => {
const absoluteRuntimePath = path.dirname(
require.resolve(`@babel/runtime/package.json`)
)

// TODO(v3): Remove process.env.GATSBY_BUILD_STAGE, needs to be passed as an option
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`

return {
// Babel assumes ES Modules, which isn't safe until CommonJS
// dies. This changes the behavior to assume CommonJS unless
Expand Down Expand Up @@ -53,6 +60,13 @@ export default () => {
],
// Adds syntax support for import()
require.resolve(`@babel/plugin-syntax-dynamic-import`),
],
stage === `build-javascript` && [
// Remove PropTypes from production build
require.resolve(`babel-plugin-transform-react-remove-prop-types`),
{
removeImport: true,
},
],
].filter(Boolean),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ Object {
"compact": false,
"configFile": false,
"presets": Array [
"<PROJECT_ROOT>/packages/babel-preset-gatsby/dependencies.js",
Array [
"<PROJECT_ROOT>/packages/babel-preset-gatsby/dependencies.js",
Object {
"stage": "develop",
},
],
],
"sourceMaps": false,
},
Expand Down
9 changes: 8 additions & 1 deletion packages/gatsby/src/utils/webpack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,14 @@ export const createWebpackUtils = (
babelrc: false,
configFile: false,
compact: false,
presets: [require.resolve(`babel-preset-gatsby/dependencies`)],
presets: [
[
require.resolve(`babel-preset-gatsby/dependencies`),
{
stage,
},
],
],
// If an error happens in a package, it's possible to be
// because it was compiled. Thus, we don't want the browser
// debugger to show the original code. Instead, the code
Expand Down