Issue 1:
I have a monorepo, containing a create-react-app application, a next.js application and a separate library. This library is linked (with yarn link) to the two applications and has styled-components as a peer dependency, which is installed in both the applications.

The linking works fine in the create-react-app application but in next.js it breaks resulting in the following error:

Error in @scrnhq/eurovision-elements
Module not found: Error: Can't resolve 'styled-components' in '/Users/robert/sites/eurovision/elements/dist'
ModuleNotFoundError: Module not found: Error: Can't resolve 'styled-components' in '/Users/robert/sites/eurovision/elements/dist'
at factoryCallback (/Users/robert/sites/eurovision/pulse/node_modules/webpack/lib/Compilation.js:276:40)
at factory (/Users/robert/sites/eurovision/pulse/node_modules/webpack/lib/NormalModuleFactory.js:235:20)
at resolver (/Users/robert/sites/eurovision/pulse/node_modules/webpack/lib/NormalModuleFactory.js:60:20)
at asyncLib.parallel (/Users/robert/sites/eurovision/pulse/node_modules/webpack/lib/NormalModuleFactory.js:127:20)
at /Users/robert/sites/eurovision/pulse/node_modules/async/dist/async.js:3874:9
at /Users/robert/sites/eurovision/pulse/node_modules/async/dist/async.js:473:16
at iteratorCallback (/Users/robert/sites/eurovision/pulse/node_modules/async/dist/async.js:1048:13)
at /Users/robert/sites/eurovision/pulse/node_modules/async/dist/async.js:958:16
at /Users/robert/sites/eurovision/pulse/node_modules/async/dist/async.js:3871:13
at resolvers.normal.resolve (/Users/robert/sites/eurovision/pulse/node_modules/webpack/lib/NormalModuleFactory.js:119:22)
at onError (/Users/robert/sites/eurovision/pulse/node_modules/enhanced-resolve/lib/Resolver.js:65:10)
at loggingCallbackWrapper (/Users/robert/sites/eurovision/pulse/node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
at runAfter (/Users/robert/sites/eurovision/pulse/node_modules/enhanced-resolve/lib/Resolver.js:158:4)
at innerCallback (/Users/robert/sites/eurovision/pulse/node_modules/enhanced-resolve/lib/Resolver.js:146:3)
at loggingCallbackWrapper (/Users/robert/sites/eurovision/pulse/node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
at next (/Users/robert/sites/eurovision/pulse/node_modules/tapable/lib/Tapable.js:252:11)

Installing styled-components inside this library is not possible as it has to be a peer dependency, see: https://www.styled-components.com/docs/faqs#i-am-a-library-author-should-i-bundle-styledcomponents-with-my-library

I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

next.js should resolve peer dependencies of linked modules in the correct way, meaning from the project's node_modules folder.

Current Behavior

It looks like peer dependencies currently are not resolved from the correct place, as it looks like it's only looking at the folder (and parents) of the linked module itself.

Steps to Reproduce (for bugs)

As this is a closed source project I can't share the source code or set up, but if necessary I can add a similar reproducible example later.

Your Environment

Tech
Version

next
5.1.0

node
9.6.1

OS
10.13.2

Issue 2:
The next/dynamic import is failing in handle-import.js due to a preprocessed module name evaluating to undefined. Boiled down version of code:

// index.js
const preval_modulename = preval`
module.exports = './hello-' + 'world';
`;

console.log(preval_modulename); // prints './hello-world'
const DynamicComponent = dynamic(import(preval_modulename)); // reduces to import(undefined)

// .babelrc
{
"presets": [
"next/babel"
],
"plugins": [
"preval"
]
}

I have searched the issues of this repository and believe that this is not a duplicate.

Previous issues such as #2690 are similar, however I believe that the extra wrinkle of attempting to use preval warrants this as a new issue.

Expected Behavior

According to the babel docs plugins should operate prior to presets. For this reason, I expected the preval plugin to build a static string at build time which the import handler plugin can parse and register chunks as usual.

Current Behavior

Instead, the import call evaluates the preval expression as undefined, which causes the dynamic import to fail. Interestingly, a console.log of the preval value does produce the correct value, not undefined. I expect this is due to a nuance of Babel's AST and the interaction between the plugins which I do not understand.

Cannot read property '0' of undefined at getModulePath (.../node_modules/next/dist/server/build/babel/plugins/handle-import.js:34:30)

Steps to Reproduce (for bugs)

Install preval babel plugin

Pass result of any preval expression to import()

Context

I am building a gallery of components, with a directory of components:

.
├── gallery
| ├── first-component.js
| ├── another-component.js
| ├── yet-another-component.js
| ├── more-component.js
| ├── yet-more-component.js
| // continue indefinitely

Because the gallery will be added to indefinitely, I wish for each to be dynamically loaded independently without manually adding them as separate dynamic imports:

const First = dynamic(import('../gallery/first-component'));
const Another = dynamic(import('../gallery/another-component'));
const YetAnother = dynamic(import('../gallery/yet-another-component'));
const More = dynamic(import('../gallery/more-component'));
const YetMore = dynamic(import('../gallery/yet-more-component'));
// continue indefinitely

// is manual and error prone vs
const DirectoryBundle = dynamic({
modules: props => {
const components = preval`getDirectoryComponents()`;

return components
},
render: (props, components) =>
// ...
})

The Webpack docs discuss requiring with an expression however I have been unsuccessful in my attempts to hybridize this with next/dynamic.

Your Environment

Tech
Version

next
3.2.2

node
8.5.0

OS
Linux 4.10.0-33-generic

browser
Chrome 61.0.3163.91 (Official Build) (64-bit)