Unverified Commit 6b516f6c authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Remove accidental support for invalid JavaScript (#8077)

* Remove accidental support for invalid JavaScript
Next.js inadvertently supported users mixing ES Modules and CommonJS due to a Babel bug that was fixed in v7.
This removes the hack in favor of the new Babel `sourceType` feature.

**This will result in broken code for users who relied on this non-standard conforming behavior.**

* Fix comment

* Toggle feature behind modern flag
parent 77c5e36b
......@@ -47,6 +47,7 @@ type NextBabelPresetOptions = {
type BabelPreset = {
presets?: PluginItem[] | null
plugins?: PluginItem[] | null
sourceType?: 'script' | 'module' | 'unambiguous'
overrides?: any[]
}
......@@ -87,6 +88,7 @@ module.exports = (
}
return {
sourceType: 'unambiguous',
presets: [
[require('@babel/preset-env').default, presetEnvConfig],
[
......
......@@ -69,6 +69,7 @@ export default async function getBaseWebpackConfig(
loader: 'next-babel-loader',
options: {
isServer,
hasModern: !!config.experimental.modern,
distDir,
cwd: dir,
cache: !selectivePageBuilding,
......
......@@ -2,8 +2,8 @@ import hash from 'string-hash'
import { join, basename } from 'path'
import babelLoader from 'babel-loader'
// increment 'c' to invalidate cache
const cacheKey = 'babel-cache-' + 'c' + '-'
// increment 'd' to invalidate cache
const cacheKey = 'babel-cache-' + 'd' + '-'
const nextBabelPreset = require('../../babel/preset')
const getModernOptions = (babelOptions = {}) => {
......@@ -54,7 +54,8 @@ module.exports = babelLoader.custom(babel => {
const custom = {
isServer: opts.isServer,
asyncToPromises: opts.asyncToPromises,
isModern: opts.isModern
isModern: opts.isModern,
hasModern: opts.hasModern
}
const filename = join(opts.cwd, 'noop.js')
const loader = Object.assign(
......@@ -66,6 +67,7 @@ module.exports = babelLoader.custom(babel => {
cacheKey +
(opts.isServer ? '-server' : '') +
(opts.isModern ? '-modern' : '') +
(opts.hasModern ? '-has-modern' : '') +
JSON.stringify(
babel.loadPartialConfig({
filename,
......@@ -85,13 +87,14 @@ module.exports = babelLoader.custom(babel => {
delete loader.cache
delete loader.distDir
delete loader.isModern
delete loader.hasModern
return { loader, custom }
},
config (
cfg,
{
source,
customOptions: { isServer, asyncToPromises, isModern }
customOptions: { isServer, asyncToPromises, isModern, hasModern }
}
) {
const { cwd } = cfg.options
......@@ -191,7 +194,7 @@ module.exports = babelLoader.custom(babel => {
// If the file has `module.exports` we have to transpile commonjs because Babel adds `import` statements
// That break webpack, since webpack doesn't support combining commonjs and esmodules
if (source.indexOf('module.exports') !== -1) {
if (!hasModern && source.indexOf('module.exports') !== -1) {
options.plugins = options.plugins || []
options.plugins.push(applyCommonJs)
}
......
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