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): Add babel-plugin-dynamic-import-node for tests #15657

Merged
merged 5 commits into from Jul 12, 2019
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
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),
}
}