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 = { ...@@ -47,6 +47,7 @@ type NextBabelPresetOptions = {
type BabelPreset = { type BabelPreset = {
presets?: PluginItem[] | null presets?: PluginItem[] | null
plugins?: PluginItem[] | null plugins?: PluginItem[] | null
sourceType?: 'script' | 'module' | 'unambiguous'
overrides?: any[] overrides?: any[]
} }
...@@ -87,6 +88,7 @@ module.exports = ( ...@@ -87,6 +88,7 @@ module.exports = (
} }
return { return {
sourceType: 'unambiguous',
presets: [ presets: [
[require('@babel/preset-env').default, presetEnvConfig], [require('@babel/preset-env').default, presetEnvConfig],
[ [
......
...@@ -69,6 +69,7 @@ export default async function getBaseWebpackConfig( ...@@ -69,6 +69,7 @@ export default async function getBaseWebpackConfig(
loader: 'next-babel-loader', loader: 'next-babel-loader',
options: { options: {
isServer, isServer,
hasModern: !!config.experimental.modern,
distDir, distDir,
cwd: dir, cwd: dir,
cache: !selectivePageBuilding, cache: !selectivePageBuilding,
......
...@@ -2,8 +2,8 @@ import hash from 'string-hash' ...@@ -2,8 +2,8 @@ import hash from 'string-hash'
import { join, basename } from 'path' import { join, basename } from 'path'
import babelLoader from 'babel-loader' import babelLoader from 'babel-loader'
// increment 'c' to invalidate cache // increment 'd' to invalidate cache
const cacheKey = 'babel-cache-' + 'c' + '-' const cacheKey = 'babel-cache-' + 'd' + '-'
const nextBabelPreset = require('../../babel/preset') const nextBabelPreset = require('../../babel/preset')
const getModernOptions = (babelOptions = {}) => { const getModernOptions = (babelOptions = {}) => {
...@@ -54,7 +54,8 @@ module.exports = babelLoader.custom(babel => { ...@@ -54,7 +54,8 @@ module.exports = babelLoader.custom(babel => {
const custom = { const custom = {
isServer: opts.isServer, isServer: opts.isServer,
asyncToPromises: opts.asyncToPromises, asyncToPromises: opts.asyncToPromises,
isModern: opts.isModern isModern: opts.isModern,
hasModern: opts.hasModern
} }
const filename = join(opts.cwd, 'noop.js') const filename = join(opts.cwd, 'noop.js')
const loader = Object.assign( const loader = Object.assign(
...@@ -66,6 +67,7 @@ module.exports = babelLoader.custom(babel => { ...@@ -66,6 +67,7 @@ module.exports = babelLoader.custom(babel => {
cacheKey + cacheKey +
(opts.isServer ? '-server' : '') + (opts.isServer ? '-server' : '') +
(opts.isModern ? '-modern' : '') + (opts.isModern ? '-modern' : '') +
(opts.hasModern ? '-has-modern' : '') +
JSON.stringify( JSON.stringify(
babel.loadPartialConfig({ babel.loadPartialConfig({
filename, filename,
...@@ -85,13 +87,14 @@ module.exports = babelLoader.custom(babel => { ...@@ -85,13 +87,14 @@ module.exports = babelLoader.custom(babel => {
delete loader.cache delete loader.cache
delete loader.distDir delete loader.distDir
delete loader.isModern delete loader.isModern
delete loader.hasModern
return { loader, custom } return { loader, custom }
}, },
config ( config (
cfg, cfg,
{ {
source, source,
customOptions: { isServer, asyncToPromises, isModern } customOptions: { isServer, asyncToPromises, isModern, hasModern }
} }
) { ) {
const { cwd } = cfg.options const { cwd } = cfg.options
...@@ -191,7 +194,7 @@ module.exports = babelLoader.custom(babel => { ...@@ -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 // 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 // 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 = options.plugins || []
options.plugins.push(applyCommonJs) 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