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

Unable to override WebpackDevServer configuration #86

Closed
bdimitrijoski opened this issue Oct 4, 2018 · 8 comments
Closed

Unable to override WebpackDevServer configuration #86

bdimitrijoski opened this issue Oct 4, 2018 · 8 comments
Labels
bug Something isn't working

Comments

@bdimitrijoski
Copy link

Hello,
I am upgrading big project that runs on AngularJS with Typescript and I am trying to customize the webpack configuration so everything can run as it worked previously.
I have managed to create all the configration that I need, thanks to the configuration provided here.

My only problem that I can't figure out how to do is to modify the webpackDevServer configuration.
I am using web sockets in my application and in my previous webpack configuration I was using the setup function to access to the Express app object and attach the web sockets there.

I have tried to put the "devServer" in my custom webpack configuration, but it seems that is not used.

Here is my part of my angular.cli configuration:

//... some stuff...
		"build": {
		  "builder": "@angular-builders/custom-webpack:browser",
		  "options": {
			"customWebpackConfig": {
			   "path": "./webpack.config.js",
				"mergeStrategies": {
					"entry": "prepend",
					"rules": "prepend",
					"plugins": "replace",
					"devServer": "replace"
				}
			},
 //... some stuff...
		"serve": {
		  "builder": "@angular-builders/dev-server:generic",
		  "options": {
			"browserTarget": "myapp:build"
		  },
//... some stuff...

Webpack configuration:

//... some stuff...
let CHUNK_HASH = webpackConfigurationUtils.generateBundleHash();

module.exports = {
    entry: {
        "main": [
            "./src/main.ts"
        ],
        "polyfills": [
            "./src/polyfills.ts"
        ],
        "styles": [
            "./src/styles.css"
        ]
    },
    output: {
        path: path.join(process.cwd(), "dist"),
        filename: `[name].bundle${CHUNK_HASH}.js`,
        chunkFilename: `[id].bundle${CHUNK_HASH}.js`
    },
    plugins: plugins,
	devServer: {
      port: METADATA.port,
      host: METADATA.host,
      hot: METADATA.HMR,
      public: METADATA.PUBLIC,
      historyApiFallback: true,
      watchOptions: {
        ignored: /node_modules/
      },
      stats: {
        assets: true,
        children: false,
        chunks: false,
        hash: false,
        modules: false,
        publicPath: false,
        timings: true,
        version: false,
        warnings: true,
        colors: {
          green: '\u001b[32m',
        }
      },
	  https: {
		cert: fs.readFileSync("keystore.crt"),
		key: fs.readFileSync("store.key")
		},

      /**
       * Here you can access the Express app object and add your own custom middleware to it.
       *
       * See: https://webpack.js.org/configuration/dev-server/
       */
      setup: function (app) {
        	var server = https.createServer({
                key: fs.readFileSync('store.key'),
                cert: fs.readFileSync('keystore.crt')
            }, appServer);

            server.listen(WEBPACK_CONFIG.WEB_SOCKETS_PORT);

            var socketManager = require('./socket.config')(server, WEBPACK_CONFIG.API_PORT);
            socketManager.connectToAPISocketServer();
      }
    }
}

Is it possible to use angular-builders to customize the webpackDevServer somehow?

@just-jeb
Copy link
Owner

just-jeb commented Oct 4, 2018

In general the answer is yes, it should be possible. But it's very hard to tell what's the problem here. Could you create a minimal reproduction repo so that I could take a look?

@bdimitrijoski
Copy link
Author

Hi, sorry for the late response.
Here is minimum reproduction repo:
https://github.com/bdimitrijoski/angular-cli-custom-webpack-config

@just-jeb
Copy link
Owner

just-jeb commented Oct 5, 2018

It is a bug, apparently.
If you look into their source code you'll see that they are not building devServer configuration as part of webpack configuration but rather doing that in a different method - _buildServerConfig.
In order to fix the bug the builder has to override that method and merge devServer config from the custom webpack configuration.
The problem is that this method is private, so you can't just override it by inheritance.
I'll think about possible solutions.
Unfortunately I can't think of any workaround to provide you with for the time being.

@bdimitrijoski
Copy link
Author

OK, thanks for the answer. Probably for now the only solution as I can see is to create new custom webpack configuration that will work without angular CLI.
If you find some other more elegant solution, please let me know :)

@just-jeb
Copy link
Owner

just-jeb commented Oct 5, 2018

Just implemented this small bit of functionality, will be available in the next version (which I hope will be released today).

@bdimitrijoski
Copy link
Author

Awesome, thanks.

@just-jeb
Copy link
Owner

just-jeb commented Oct 5, 2018

Implemented in 2.3.5, please confirm that it works for you.

@bdimitrijoski
Copy link
Author

Yes, I confirm that the issue is fixed in the 2.3.5 version.
Thank you.

@just-jeb just-jeb added the bug Something isn't working label Oct 5, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants