Skip to content

Commit

Permalink
Make dynamic import spec compliant. (#2612)
Browse files Browse the repository at this point in the history
* Make dynamic import spec compliant.
Now we simply return the whole module incl. default field
In 'next/dynamic' we pick the default field if there is.
Since modules with default is mostly used next/dynamic, for the enduser, this change has no effect.

* Rename module into m
Using module could be confusing.
  • Loading branch information
arunoda committed Jul 20, 2017
1 parent 89d4e0a commit 2f7e459
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default function dynamicComponent (p, o) {
}

loadComponent () {
promise.then((AsyncComponent) => {
promise.then((m) => {
const AsyncComponent = m.default || m
// Set a readable displayName for the wrapper component
const asyncCompName = getDisplayName(AsyncComponent)
if (asyncCompName) {
Expand All @@ -65,7 +66,7 @@ export default function dynamicComponent (p, o) {
this.setState({ AsyncComponent })
} else {
if (this.isServer) {
registerChunk(AsyncComponent.__webpackChunkName)
registerChunk(m.__webpackChunkName)
}
this.state.AsyncComponent = AsyncComponent
}
Expand Down Expand Up @@ -100,9 +101,10 @@ export default function dynamicComponent (p, o) {

const loadModule = (name) => {
const promise = modulePromiseMap[name]
promise.then((Component) => {
promise.then((m) => {
const Component = m.default || m
if (this.isServer) {
registerChunk(Component.__webpackChunkName)
registerChunk(m.__webpackChunkName)
}
moduleMap[name] = Component
remainingPromises--
Expand Down
4 changes: 1 addition & 3 deletions server/build/babel/plugins/handle-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const buildImport = (args) => (template(`
eval('require.ensure = function (deps, callback) { callback(require) }')
require.ensure([], (require) => {
let m = require(SOURCE)
m = m.default || m
m.__webpackChunkName = '${args.name}.js'
resolve(m);
}, 'chunks/${args.name}.js');
Expand All @@ -23,13 +22,12 @@ const buildImport = (args) => (template(`
const weakId = require.resolveWeak(SOURCE)
try {
const weakModule = __webpack_require__(weakId)
return resolve(weakModule.default || weakModule)
return resolve(weakModule)
} catch (err) {}
require.ensure([], (require) => {
try {
let m = require(SOURCE)
m = m.default || m
resolve(m)
} catch(error) {
reject(error)
Expand Down

0 comments on commit 2f7e459

Please sign in to comment.