Unverified Commit 0088083f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Check if esmodules is supported before enabling useESModules (#6534)

* Check if esmodules is supported before enabling useESModules

* Remove console.log
parent e10096cd
...@@ -47,11 +47,17 @@ type BabelPreset = { ...@@ -47,11 +47,17 @@ type BabelPreset = {
plugins?: PluginItem[] | null plugins?: PluginItem[] | null
} }
module.exports = (context: any, options: NextBabelPresetOptions = {}): BabelPreset => { // Taken from https://github.com/babel/babel/commit/d60c5e1736543a6eac4b549553e107a9ba967051#diff-b4beead8ad9195361b4537601cc22532R158
function supportsStaticESM(caller: any) {
return !!(caller && caller.supportsStaticESM);
}
module.exports = (api: any, options: NextBabelPresetOptions = {}): BabelPreset => {
const supportsESM = api.caller(supportsStaticESM)
const presetEnvConfig = { const presetEnvConfig = {
// In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option // In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
// In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking // In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
modules: isDevelopment || isProduction ? false : 'auto', modules: 'auto',
...options['preset-env'] ...options['preset-env']
} }
return { return {
...@@ -74,7 +80,7 @@ module.exports = (context: any, options: NextBabelPresetOptions = {}): BabelPres ...@@ -74,7 +80,7 @@ module.exports = (context: any, options: NextBabelPresetOptions = {}): BabelPres
corejs: 2, corejs: 2,
helpers: true, helpers: true,
regenerator: true, regenerator: true,
useESModules: !isTest && presetEnvConfig.modules !== 'commonjs', useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs',
...options['transform-runtime'] ...options['transform-runtime']
}], }],
[require('styled-jsx/babel'), styledJsxOptions(options['styled-jsx'])], [require('styled-jsx/babel'), styledJsxOptions(options['styled-jsx'])],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment