From e5002234d053c2bda0317cab668c2f6d6a6e88d7 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 30 Nov 2018 17:56:07 +0100 Subject: [PATCH] Transpile imports if module has module.exports (#5780) Fixes #5778 Fixes #3650 --- .../next/build/webpack/loaders/next-babel-loader.js | 7 ++++++- test/integration/basic/lib/async-function.js | 3 +++ .../basic/pages/read-only-object-error.js | 12 ++++++++++++ test/integration/basic/test/client-navigation.js | 12 ++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/integration/basic/lib/async-function.js create mode 100644 test/integration/basic/pages/read-only-object-error.js diff --git a/packages/next/build/webpack/loaders/next-babel-loader.js b/packages/next/build/webpack/loaders/next-babel-loader.js index eb84cc6d..ec40dcf8 100644 --- a/packages/next/build/webpack/loaders/next-babel-loader.js +++ b/packages/next/build/webpack/loaders/next-babel-loader.js @@ -21,7 +21,7 @@ module.exports = babelLoader.custom(babel => { return { loader, custom } }, - config (cfg, {customOptions: {isServer, dev}}) { + config (cfg, {source, customOptions: {isServer, dev}}) { const options = Object.assign({}, cfg.options) if (cfg.hasFilesystemConfig()) { for (const file of [cfg.babelrc, cfg.config]) { @@ -37,6 +37,11 @@ module.exports = babelLoader.custom(babel => { options.presets = [...options.presets, presetItem] } + if (source.match(/module\.exports/)) { + options.plugins = options.plugins || [] + options.plugins.push(commonJsItem) + } + options.overrides = [ ...(options.overrides || []), { diff --git a/test/integration/basic/lib/async-function.js b/test/integration/basic/lib/async-function.js new file mode 100644 index 00000000..7d702fba --- /dev/null +++ b/test/integration/basic/lib/async-function.js @@ -0,0 +1,3 @@ +module.exports = async function () { + return 'test' +} diff --git a/test/integration/basic/pages/read-only-object-error.js b/test/integration/basic/pages/read-only-object-error.js new file mode 100644 index 00000000..d5cc8d8a --- /dev/null +++ b/test/integration/basic/pages/read-only-object-error.js @@ -0,0 +1,12 @@ +import test from '../lib/async-function' + +function ReadOnlyObjectError () { + return 'this is just a placeholder component' +} + +ReadOnlyObjectError.getInitialProps = async () => { + const result = await test() + return {result} +} + +export default ReadOnlyObjectError diff --git a/test/integration/basic/test/client-navigation.js b/test/integration/basic/test/client-navigation.js index fc1ff14d..fbf5fdc4 100644 --- a/test/integration/basic/test/client-navigation.js +++ b/test/integration/basic/test/client-navigation.js @@ -732,5 +732,17 @@ export default (context) => { } }) }) + + it('should not error on module.exports + polyfills', async () => { + let browser + try { + browser = await webdriver(context.appPort, '/read-only-object-error') + expect(await browser.elementByCss('body').text()).toBe('this is just a placeholder component') + } finally { + if (browser) { + browser.close() + } + } + }) }) }