Skip to content

Commit

Permalink
fix(babel-preset-gatsby): Add babel-plugin-dynamic-import-node for te…
Browse files Browse the repository at this point in the history
…sts (#15657)

* Add babel-plugin-dynamic-import-node for tests

* Add babel-plugin-dynamic-import-node to babel-preset-gatsby-package

* Update snapshots

* Consider BABEL_ENV as well
  • Loading branch information
sidharthachatterjee authored and GatsbyJS Bot committed Jul 12, 2019
1 parent 738cf3b commit ad5e8a0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
Expand Up @@ -64,6 +64,7 @@ Array [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-transform-runtime",
"@babel/plugin-syntax-dynamic-import",
"babel-plugin-dynamic-import-node",
]
`;

Expand Down Expand Up @@ -123,6 +124,7 @@ Array [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-transform-runtime",
"@babel/plugin-syntax-dynamic-import",
"babel-plugin-dynamic-import-node",
]
`;

Expand Down
12 changes: 7 additions & 5 deletions packages/babel-preset-gatsby-package/index.js
Expand Up @@ -4,20 +4,21 @@ function preset(context, options = {}) {
const { browser = false, debug = false, nodeVersion = `8.0` } = options
const { NODE_ENV, BABEL_ENV } = process.env

const PRODUCTION = (BABEL_ENV || NODE_ENV) === `production`
const IS_PRODUCTION = (BABEL_ENV || NODE_ENV) === `production`
const IS_TEST = (BABEL_ENV || NODE_ENV) === `test`

const browserConfig = {
useBuiltIns: false,
targets: {
browsers: PRODUCTION
browsers: IS_PRODUCTION
? [`last 4 versions`, `safari >= 7`, `ie >= 9`]
: [`last 2 versions`, `not ie <= 11`, `not android 4.4.3`],
},
}

const nodeConfig = {
targets: {
node: PRODUCTION ? nodeVersion : `current`,
node: IS_PRODUCTION ? nodeVersion : `current`,
},
}

Expand All @@ -36,15 +37,16 @@ function preset(context, options = {}) {
browser ? browserConfig : nodeConfig
),
],
[r(`@babel/preset-react`), { development: !PRODUCTION }],
[r(`@babel/preset-react`), { development: !IS_PRODUCTION }],
r(`@babel/preset-flow`),
],
plugins: [
r(`@babel/plugin-proposal-class-properties`),
r(`@babel/plugin-proposal-optional-chaining`),
r(`@babel/plugin-transform-runtime`),
r(`@babel/plugin-syntax-dynamic-import`),
],
IS_TEST && r(`babel-plugin-dynamic-import-node`)
].filter(Boolean),
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-preset-gatsby-package/package.json
Expand Up @@ -15,7 +15,8 @@
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0"
"@babel/preset-react": "^7.0.0",
"babel-plugin-dynamic-import-node": "^1.2.0"
},
"license": "MIT",
"main": "index.js",
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-preset-gatsby/package.json
Expand Up @@ -15,7 +15,8 @@
"@babel/preset-env": "^7.4.1",
"@babel/preset-react": "^7.0.0",
"@babel/runtime": "^7.4.5",
"babel-plugin-macros": "^2.4.2"
"babel-plugin-macros": "^2.4.2",
"babel-plugin-dynamic-import-node": "^1.2.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-preset-gatsby/src/__tests__/index.js
Expand Up @@ -56,6 +56,7 @@ it(`Specifies proper presets and plugins for test stage`, () => {
loose: false,
},
],
expect.stringContaining(`babel-plugin-dynamic-import-node`),
])
})

Expand Down Expand Up @@ -123,6 +124,7 @@ it(`Specifies proper presets and plugins for build-html stage`, () => {
loose: false,
},
],
expect.stringContaining(`babel-plugin-dynamic-import-node`),
])
})

Expand Down
7 changes: 5 additions & 2 deletions packages/babel-preset-gatsby/src/index.js
Expand Up @@ -2,9 +2,11 @@ const path = require(`path`)

const resolve = m => require.resolve(m)

const IS_TEST = (process.env.BABEL_ENV || process.env.NODE_ENV) === `test`

const loadCachedConfig = () => {
let pluginBabelConfig = {}
if (process.env.NODE_ENV !== `test`) {
if (!IS_TEST) {
try {
pluginBabelConfig = require(path.join(
process.cwd(),
Expand Down Expand Up @@ -91,6 +93,7 @@ module.exports = function preset(_, options = {}) {
loose: false, // Fixes #14848
},
],
],
IS_TEST && resolve(`babel-plugin-dynamic-import-node`),
].filter(Boolean),
}
}

0 comments on commit ad5e8a0

Please sign in to comment.