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): add babel-plugin-transform-react-remove-prop-types for production builds #14987

Merged
merged 12 commits into from Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions packages/babel-preset-gatsby/README.md
Expand Up @@ -12,6 +12,7 @@ For more information on how to customize the Babel configuration of your Gatsby
- [`@babel/plugin-syntax-dynamic-import`](https://babeljs.io/docs/en/babel-plugin-syntax-dynamic-import)
- [`@babel/plugin-transform-runtime`](https://babeljs.io/docs/en/babel-plugin-transform-runtime#docsNav)
- [`babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros)
- [`babel-plugin-transform-react-remove-prop-types`](https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types)

## Usage

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-preset-gatsby/package.json
Expand Up @@ -8,7 +8,8 @@
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.4.1",
"@babel/preset-react": "^7.0.0",
"babel-plugin-macros": "^2.4.2"
"babel-plugin-macros": "^2.4.2",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
62 changes: 62 additions & 0 deletions packages/babel-preset-gatsby/src/__tests__/index.js
Expand Up @@ -119,3 +119,65 @@ it(`Allows to configure browser targets`, () => {
},
])
})

describe(`in production mode`, () => {
beforeEach(() => {
process.env.BABEL_ENV = `production`
process.env.GATSBY_BUILD_STAGE = `test`
})

it(`specifies proper presets`, () => {
const targets = `last 1 version`
const { presets, plugins } = preset(null, { targets })

expect(presets).toEqual([
[
expect.stringContaining(path.join(`@babel`, `preset-env`)),
{
corejs: 2,
loose: true,
modules: `commonjs`,
useBuiltIns: `usage`,
targets,
},
],
[
expect.stringContaining(path.join(`@babel`, `preset-react`)),
{
development: false,
pragma: `React.createElement`,
useBuiltIns: true,
},
],
])
expect(plugins).toEqual([
[
expect.stringContaining(
path.join(`@babel`, `plugin-proposal-class-properties`)
),
{ loose: true },
],
expect.stringContaining(`babel-plugin-macros`),
expect.stringContaining(
path.join(`@babel`, `plugin-syntax-dynamic-import`)
),
[
expect.stringContaining(
path.join(`@babel`, `plugin-transform-runtime`)
),
{
helpers: true,
regenerator: true,
},
],
[
expect.stringContaining(
path.join(`babel-plugin-transform-react-remove-prop-types`)
),
{
removeImport: true,
},
],
])
})
})
18 changes: 17 additions & 1 deletion packages/babel-preset-gatsby/src/index.js
Expand Up @@ -28,7 +28,10 @@ module.exports = function preset(_, options = {}) {
let { targets = null } = options

const pluginBabelConfig = loadCachedConfig()
const stage = process.env.GATSBY_BUILD_STAGE || `test`
const { NODE_ENV, BABEL_ENV, GATSBY_BUILD_STAGE } = process.env
const stage = GATSBY_BUILD_STAGE || `test`

const PRODUCTION = (BABEL_ENV || NODE_ENV) === `production`

if (!targets) {
if (stage === `build-html` || stage === `test`) {
Expand All @@ -40,6 +43,18 @@ module.exports = function preset(_, options = {}) {
}
}

const plugins = []
pieh marked this conversation as resolved.
Show resolved Hide resolved

if (PRODUCTION) {
pieh marked this conversation as resolved.
Show resolved Hide resolved
plugins.push([
// Remove PropTypes from production build
resolve(`babel-plugin-transform-react-remove-prop-types`),
{
removeImport: true,
},
])
}

return {
presets: [
[
Expand Down Expand Up @@ -77,6 +92,7 @@ module.exports = function preset(_, options = {}) {
regenerator: true,
},
],
...plugins,
],
}
}