Skip to content

Commit

Permalink
dynamic imports fix for non-webpack environments. (#3414)
Browse files Browse the repository at this point in the history
* Added condition to check whether method exists or not

* Conditional loop based on feature detection

* Added code comments as requested by the author
  • Loading branch information
PrudviGali authored and arunoda committed Dec 8, 2017
1 parent 5ce05c2 commit bd20deb
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion server/build/babel/plugins/handle-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import Crypto from 'crypto'

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(`
(
typeof window === 'undefined' ?
typeof require.resolveWeak !== 'function' ?
new (require('next/dynamic').SameLoopPromise)((resolve, reject) => {
eval('require.ensure = function (deps, callback) { callback(require) }')
require.ensure([], (require) => {
Expand Down

0 comments on commit bd20deb

Please sign in to comment.