Commit bd20deb9 authored by PrudviGali's avatar PrudviGali Committed by Arunoda Susiripala

dynamic imports fix for non-webpack environments. (#3414)

* Added condition to check whether method exists or not

* Conditional loop based on feature detection

* Added code comments as requested by the author
parent 5ce05c2d
...@@ -7,9 +7,16 @@ import Crypto from 'crypto' ...@@ -7,9 +7,16 @@ import Crypto from 'crypto'
const TYPE_IMPORT = 'Import' const TYPE_IMPORT = 'Import'
/*
Added "typeof require.resolveWeak !== 'function'" check instead of
"typeof window === 'undefined'" to support dynamic impports in non-webpack environments.
"require.resolveWeak" and "require.ensure" are webpack specific methods.
They would fail in Node/CommonJS environments.
*/
const buildImport = (args) => (template(` const buildImport = (args) => (template(`
( (
typeof window === 'undefined' ? typeof require.resolveWeak !== 'function' ?
new (require('next/dynamic').SameLoopPromise)((resolve, reject) => { new (require('next/dynamic').SameLoopPromise)((resolve, reject) => {
eval('require.ensure = function (deps, callback) { callback(require) }') eval('require.ensure = function (deps, callback) { callback(require) }')
require.ensure([], (require) => { require.ensure([], (require) => {
......
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