Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module exports are not preserved when using dev-server with Webpack 5 beta #2484

Closed
1 of 2 tasks
csvn opened this issue Mar 26, 2020 · 33 comments
Closed
1 of 2 tasks

Comments

@csvn
Copy link

csvn commented Mar 26, 2020

  • Operating System: Windows 10
  • Node Version: v13.10.0
  • NPM Version: 6.13.7
  • webpack Version: 5.0.0-beta.14
  • webpack-dev-server Version: 3.10.13
  • Browser: Chrome v82
  • This is a bug
  • This is a modification request

Code

Reproduction link: https://github.com/csvn/webpack-dev-server-export-issue-reproduction

// webpack.config.js
module.exports = {
  mode: 'development',
  entry: './src/main.js',
  output: {
    ecmaVersion: 2015,
    library: 'main',
    libraryTarget: 'window'
  },
  devServer: {
    port: 3001,
    contentBase: require('path').join(__dirname, 'public'),
    open: false
  }
};
// src/main.js
export const greeting = 'Hello World!';

Additional details

The entry point is messed up when when running webpack-dev-server. The exports will always be empty ({}) due to webpack-dev-server prepending it's entry points, which does not seem to yield the correct results with Webpack 5 beta.

When using webpack-dev-server
/******/ 	// Load entry module and return exports
/******/ 	__webpack_require__("./src/main.js");
/******/ 	return __webpack_require__("./node_modules/webpack-dev-server/client/index.js?http://localhost:3001");
When using webpack build
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__("./src/main.js");

Expected Behavior

Values exported from main.js should be available on window.main.

Actual Behavior

The exported object on window.main is {}

How can we reproduce the behavior?

  1. Clone the repo from the link above
  2. Run npm start (or npx webpack-dev-server
  3. Open http://localhost:3001
  4. There are error in console due to export from main.js missing
@alexander-akait
Copy link
Member

Pr welcome

@hmnd
Copy link

hmnd commented Jul 8, 2020

A workaround for now at least is to set config.devServer.injectClient: false; this prevents dev-server from prepending its entry.

@alexander-akait
Copy link
Member

/cc @Loonride Can you look at this?

@knagaitsev
Copy link
Collaborator

/cc @Loonride Can you look at this?

I will take a look

@kamilic
Copy link

kamilic commented Oct 14, 2020

this problem still existed on webpack@5.1.0.

@sprilukin
Copy link

this problem is still present in webpack@5.3.2

@sprilukin
Copy link

sprilukin commented Nov 1, 2020

I still want liveReloading capability during developing of my library so i found a workaround.
original webpack config:

const config = (env, args) => {
    ...
    entry: './src/myLibrary'
    ...
}

modified webpack config:

const config = (env, args) => {
    const {devServer = false} = env;
    ...
    entry: devServer ? ['webpack-dev-server/client', './src/myLibrary'] : './src/myLibrary'
    devServer: {
       ...
       injectClient: false
    }
}

and run dev server as npx webpack serve --env devServer

works fine despite the fact that correct injection of the dev server client should be the following:
webpack-dev-server/client?http://<webpackConfig.devServer.host>:<webpackConfig.devServer.port>

But i'm looking forward to see this issue fixed.

@erikwatson
Copy link

This is still an issue for me.

"devDependencies": {
    "webpack": "^5.4.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0",
}

@ylemkimon
Copy link
Contributor

Duplicate of #2692 (it's a newer issue, but has more details).

@alexander-akait
Copy link
Member

@ylemkimon yep, but it is different issues, but have the same origin

@alexander-akait
Copy link
Member

@ylemkimon just check, do we have test for:

@ylemkimon
Copy link
Contributor

@evilebottnawi 😄

it('should use the last entry export', async () => {
const { text, statusCode } = await req.get('/main.js');
expect(statusCode).toEqual(200);
expect(text).toContain('entry1');
let exports;
expect(() => {
exports = requireFromString(text);
}).not.toThrow();
expect(exports).toEqual('entry2');
});
if (title === 'object multi-entry config') {
it('should support the named entry export', async () => {
const { text, statusCode } = await req.get('/foo.js');
expect(statusCode).toEqual(200);
expect(text).not.toContain('entry2');
let exports;
expect(() => {
exports = requireFromString(text);
}).not.toThrow();
expect(exports).toEqual('entry1');
});
}

@alexander-akait
Copy link
Member

@ylemkimon good, for library?

@ylemkimon
Copy link
Contributor

@evilebottnawi It tests for libraryTarget: 'umd':

@alexander-akait
Copy link
Member

@ylemkimon I mean testing libraryTarget without module federation, sorry for misleading

Garrick-G added a commit to Garrick-G/Travel-APP that referenced this issue Dec 17, 2020
Integrated functionality with Geonames, Weatherbit, and Pixabay APIs.

-Geonames takes in the city name and responds with a list of places. The
App will chose the first option.

-Weatherbit will get historical weather data for the longitude and
latitude provided from Geonames because it doesn't serve predictions too
far out. Since the Weatherbit account is on the free version, it needs
to call the API 7 times for a weeks worth of historical data.

-Pixabay takes in the city and responds with an image for that location.

As for the WebPack bug, there is an issue with wp5, webpack-dev-server
and hot module replacement. Essentially it causes the library to empty,
not allowing access to imported functions.
The issue is found here:
webpack/webpack-dev-server#2484
and offers a fix setting config.devServer.injectClient: false.
This ends up disabling hot reload it seems but I can access the library
and reload the page manually to see changes.

I also added miscellaneous style changes as well as a countdown in days
for the trips.
@cyulin
Copy link

cyulin commented Dec 22, 2020

this problem is still present in webpack@5.10.3

jmcker added a commit to TRACE-Digital/TRACE-search that referenced this issue Feb 18, 2021
Compatability issues with Webpack 5 were giving us an empty object
for traceSearch when the test page was served via the dev server.
See webpack/webpack-dev-server#2484
@Mustang-Galaxy
Copy link

webpack-dev-server@4.0.0-beta.0 is work

@nicmosc
Copy link

nicmosc commented Mar 31, 2021

This doesn't seem to be fully fixed. While upgrading to webpack-dev-server@4 fixes the library export problem, it completely breaks hot reload (unless someone has managed to find a configuration that works, i'm still looking...) and also doesn't support Fast Reload since those plugins are still only supporting up to version 3.

For reference, here's the config i have for version 4 which still doesn't provide hot reload (i.e. the whole page refreshes every time a change occurs)

config.devServer = {
    historyApiFallback: true,
    host: '0.0.0.0',
    port: WEBPACK_PORT,
    liveReload: true,
    injectClient: true,
    client: {
      logging: 'none',
      progress: false,
      overlay: false,
      host: '0.0.0.0',
      port: WEBPACK_PORT,
    },
    static: [path.join(__dirname, '/'), path.join(__dirname, 'node_modules')],
  };
  config.infrastructureLogging = {
    level: 'none',
  };
  config.stats = 'errors-only';

Using @ncastaldo 's fix the library object is correctly exposed, however only if i disable the hot and inline options in the dev server, which again, removes hot reload. If i enable them, hot reload works, but the library is not exposed correctly anymore.

So essentially right now it's impossible to have a correctly exposed library with hot reload, since in version 3 hot reload works, but the library cannot be exposed properly, and in version 4, it's exposed properly but hot reload is broken...

Anyone else still encountering this?

@alexander-akait
Copy link
Member

remove client.host, you should not touch this option if you don't use proxy server above dev, i.e. nginx -> dev server

@bturner1273
Copy link

bturner1273 commented Apr 6, 2021

still an issue for me as well in webpack 5.28.0 with dev-server 4.0.0-beta.1 and cli 4.6.0. Solved this by just setting the window library explicitly in my index.js i.e.

window["export_name"] = {
    function_to_export: your_function
}

then any js script can now access window.export_name.function_to_export

edit: this seems to be an issue when running hot updates on the bundle after the initial build. It seems like webpack-dev-server just stops taking into account your output configuration

@alexander-akait
Copy link
Member

We have tests on it, double check your version or provide example of the problem

@turansky
Copy link

Works for me with webpack-dev-server:4.0.0-beta.2

symbioquine added a commit to symbioquine/farmOS-map that referenced this issue Apr 28, 2021
**Why?** Webpack 5 has some important features for controlling how
  library code gets exposed in different environments. In our case,
  we will want to control how the behaviors are made available for
  use when each behavior bundle gets loaded. More on that later.
  This change is just updating the dependencies to make that
  possible.

**What?** Update;

* `webpack@5.36.0` to enable better output library features
* `webpack-dev-server@4.0.0-beta.2` to get a version which is
  compatible with Webpack 5 and includes a fix for
  webpack/webpack-dev-server#2484
* `webpack-cli@4.6.0` to get a version of the CLI which works with
  the latest dev server
* `copy-webpack-plugin@8.1.1` to fix a warning when building with
  Webpack 5
symbioquine added a commit to symbioquine/farmOS-map that referenced this issue Apr 28, 2021
**Why?** Webpack 5 has some important features for controlling how
  library code gets exposed in different environments. In our case,
  we will want to control how the behaviors are made available for
  use when each behavior bundle gets loaded. More on that later.
  This change is just updating the dependencies to make that
  possible.

**What?** Update;

* `webpack@5.36.0` to enable better output library features
* `webpack-dev-server@4.0.0-beta.2` to get a version which is
  compatible with Webpack 5 and includes a fix for
  webpack/webpack-dev-server#2484
* `webpack-cli@4.6.0` to get a version of the CLI which works with
  the latest dev server
* `copy-webpack-plugin@8.1.1` to fix a warning when building with
  Webpack 5
ddohler added a commit to azavea/loam that referenced this issue Jul 26, 2021
This was much trickier than anticipated due to
webpack/webpack-dev-server#2484
which necessitated upgrading to webpack-dev-server 4.0.0-rc.0
ddohler added a commit to azavea/loam that referenced this issue Jul 29, 2021
This was much trickier than anticipated due to
webpack/webpack-dev-server#2484
which necessitated upgrading to webpack-dev-server 4.0.0-rc.0
raucao added a commit to remotestorage/remotestorage-widget that referenced this issue Aug 4, 2021
Had to switch to the latest beta version due to a bug in the dev server:

webpack/webpack-dev-server#2484
@lovefishs
Copy link

Works for me with

{
  "@pmmmwh/react-refresh-webpack-plugin": "0.5.0-rc.3",
  "react-refresh": "0.10.0",
  "webpack-cli": "4.8.0",
  "webpack-dev-server": "4.0.0-rc.1"
}

ddohler added a commit to azavea/loam that referenced this issue Sep 14, 2021
This was much trickier than anticipated due to
webpack/webpack-dev-server#2484
which necessitated upgrading to webpack-dev-server 4.0.0-rc.0
@alex-vukov
Copy link

Having the same problem with the latest possible setup + Module Federation Plugin:

{
    "webpack": "^5.64.2",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.5.0",
}

This is in remoteEntry.js and I can't disable it because config.devServer.injectClient: false no longer works in this version of dev server

/************************************************************************/
/******/ 	
/******/ 	// module cache are used so entry inlining is disabled
/******/ 	// startup
/******/ 	// Load entry module and return exports
/******/ 	__webpack_require__.O(undefined, ["vendors-node_modules_webpack-dev-server_client_index_js_protocol_ws_3A_hostname_0_0_0_0_port_-9d6a35","webpack_sharing_provide_default_ccp_utils-webpack_sharing_provide_default_i18next-webpack_sha-7bf979"], () => (__webpack_require__("./node_modules/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&port=5103&pathname=%2Fws&logging=info&reconnect=10")))
/******/ 	__webpack_require__.O(undefined, ["vendors-node_modules_webpack-dev-server_client_index_js_protocol_ws_3A_hostname_0_0_0_0_port_-9d6a35","webpack_sharing_provide_default_ccp_utils-webpack_sharing_provide_default_i18next-webpack_sha-7bf979"], () => (__webpack_require__("./node_modules/webpack/hot/dev-server.js")))
/******/ 	var __webpack_exports__ = __webpack_require__.O(undefined, ["vendors-node_modules_webpack-dev-server_client_index_js_protocol_ws_3A_hostname_0_0_0_0_port_-9d6a35","webpack_sharing_provide_default_ccp_utils-webpack_sharing_provide_default_i18next-webpack_sha-7bf979"], () => (__webpack_require__("webpack/container/entry/onboardingMFE")))
/******/ 	__webpack_exports__ = __webpack_require__.O(__webpack_exports__);
/******/ 	onboardingMFE = __webpack_exports__;
/******/ 	
/******/ })()
;
//# sourceMappingURL=sourcemaps/remoteEntry.js.map

@alex-vukov
Copy link

Having the same problem with the latest possible setup + Module Federation Plugin:

{
    "webpack": "^5.64.2",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.5.0",
}

This is in remoteEntry.js and I can't disable it because config.devServer.injectClient: false no longer works in this version of dev server

/************************************************************************/
/******/ 	
/******/ 	// module cache are used so entry inlining is disabled
/******/ 	// startup
/******/ 	// Load entry module and return exports
/******/ 	__webpack_require__.O(undefined, ["vendors-node_modules_webpack-dev-server_client_index_js_protocol_ws_3A_hostname_0_0_0_0_port_-9d6a35","webpack_sharing_provide_default_ccp_utils-webpack_sharing_provide_default_i18next-webpack_sha-7bf979"], () => (__webpack_require__("./node_modules/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&port=5103&pathname=%2Fws&logging=info&reconnect=10")))
/******/ 	__webpack_require__.O(undefined, ["vendors-node_modules_webpack-dev-server_client_index_js_protocol_ws_3A_hostname_0_0_0_0_port_-9d6a35","webpack_sharing_provide_default_ccp_utils-webpack_sharing_provide_default_i18next-webpack_sha-7bf979"], () => (__webpack_require__("./node_modules/webpack/hot/dev-server.js")))
/******/ 	var __webpack_exports__ = __webpack_require__.O(undefined, ["vendors-node_modules_webpack-dev-server_client_index_js_protocol_ws_3A_hostname_0_0_0_0_port_-9d6a35","webpack_sharing_provide_default_ccp_utils-webpack_sharing_provide_default_i18next-webpack_sha-7bf979"], () => (__webpack_require__("webpack/container/entry/onboardingMFE")))
/******/ 	__webpack_exports__ = __webpack_require__.O(__webpack_exports__);
/******/ 	onboardingMFE = __webpack_exports__;
/******/ 	
/******/ })()
;
//# sourceMappingURL=sourcemaps/remoteEntry.js.map

If anyone is interested, the extra entry points disappeared after I set webSocketServer: false into devServer: {}

@yxw007
Copy link

yxw007 commented Jul 20, 2023

...
output: {
		library: "GlobalName",
		path: path.resolve(__dirname, 'dist'),
		filename: 'index.js',
		libraryTarget: 'umd',
		libraryExport: 'default'
},
...

This is accessed via GlobalName, work's ok in webpack5.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests