简体   繁体   中英

Loading different module for Webpack than Server Script

The best way I can think to explain my question is with an example:

Say I have a small single page app with server side rendering. On the client side new page requests resolve using XHR, but on the server I'd like to just run the server-side resolvers directly.

The components of the front end call a getter on a data object eg: Data.page.get() which either polls the server or returns a locally stored value. So I want webpack to pack the relevant client code. But I want to point to a different Data object on the server that calls whatever logic the server uses to resolve the XHR request, be it a db call or a filesystem call or whatever.

At the moment I've got var Data = require(./data) getting the server logic. Is there a way to tell webpack 'Hey, don't use ./data here use ./data-client instead? Or am I going about this backwards somehow?

This seems like a pretty simple concept but I'm still pretty new to javascript and programming in general and I'm a little stumped as to the best way to do it.

Or should I be processing the node script with webpack too like described here: can webpack output separate script and module files for browser and node?

I'm currently using webpack 1, but happy to solve the problem using webpack 2 instead.

It turned out to be fairly simple.

Webpack resolves modules with an extension priority: 1) .webpack.js 2) .web.js 3) .js

So in my case require('./data') will resolve to data.js with node, and so long as I have a data.web.js` it will resolve to that in webpack.

At least to my understanding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM