angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

[Enhancement] Overridable webpack config

Open nikolasleblanc opened this issue 10 years ago • 58 comments

Angular-cli@webpack is awesome

With webpack on board, angular-cli is looking like a seriously viable toolset for developing and building production ready applications.

And a default webpack config is a life-saver

Webpack's not exactly a breeze. Having default webpack configs ready to go the second a developer creates a project saves them a lot of time and energy.

But webpack needs to be configurable

Once that project's spun up and begins to take form, that developer may want to tweak the webpack config. They may want to add this loader, and that loader, and they may have opinions about how it should be bundled for production.

You can't please everybody

To bloat the base webpack configs, one PR after another, with each individual's particular project need is unsustainable.

What then?

When a developer needs something that the default won't be able to provide, they might be inclined to break off on their own, leaving the benefits of angular-cli aside.

Why not allow the ease of angular-cli to coexist with the configurability of webpack?

One possible solution

webpack-config.js

webpack-config.js looks for the following config files, based on the current environment.

  • weback-build-common.ts
  • webpack-build-development.ts
  • webpack-build-production.ts
  • webpack-build-mobile.ts

Using angular-cli.json, the developer could point to environment-specific webpack configuration overrides.

angular-cli.json

angular-cli.json is already storing paths for karma configs, protractor configs, and tsconfig, why not let it also store webpack configs?

{
  ...,
  "e2e": {
    "protractor": {
      "config": "config/protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "config/karma.conf.js"
    }
  },
  "webpack": {
    "development": {
      "config": "config/webpack.dev.conf.js"
    },
    ...
}  

If webpack-config.js then checked angular-cli.json to see:

  • if an override path has been provided, and
  • if a file exists at that path

...it could load the override config instead of the default config.

Summary

This solution offers the developer the option of using default webpack configs to get rolling, as well as the ability to override those defaults once their project begins to show unique requirements, with the smallest possible footprint on the base library.

It also allows the developer to stay in sync with the angular-cli library without having to eject from it, and allows the default configs to continue to stay relevant.

nikolasleblanc avatar Aug 12 '16 01:08 nikolasleblanc

Any vague ETA on this? Most of my issues are currently blocked by still be using RC4. It looks like that I have no other choice than to hack the cli. Can somebody suggest a step process on what I should do? I might not be the first trying this. Is @Iverson 's guide still working with current beta?

Kunepro avatar Sep 21 '16 22:09 Kunepro

Once we release final, we'll have a clearer list of things to do and roadmap. The earliest ETA I can give is somewhere in 2017. Before the new year is highly unlikely.

hansl avatar Sep 21 '16 23:09 hansl

@Kunepro there is another option depending on your circumstances.

I had an older project created with [email protected] or so. Despite my familiarity with webpack it was significantly easier to upgrade the project's Angular version to 2.0 than it was to update to webpack or "hack the CLI".

I'm not really sure that "hacking the CLI" is a viable solution for anyone who's working on a collaborative project due to the complexities it imposes on project setup.

bennett000 avatar Sep 21 '16 23:09 bennett000

Also, Im curious to know what's so special about your setup that you need access to webpack. What do you need that's not provided?

hansl avatar Sep 21 '16 23:09 hansl

@hansl in my case the project I used yesterday contains multiple SPA's so webpack will probably never be a good fit. (BTW I do love webpack and the fact that it's abstracted out of my projects with ng-cli)

In other cases the need is to tap into postCSS so to run tools like CSSNext

bennett000 avatar Sep 21 '16 23:09 bennett000

@hansl my project also contains multiple SPA and we need to exclude commons libs like angular from bundle (by using CDN for libs). How can I do that without using webpack config?

martinbelanger avatar Sep 22 '16 00:09 martinbelanger

@hansl

  1. Thanks, at least I know that waiting is not a viable option.
  2. PostCSS. If you can tell me how to use PostCSS (and plugins with config) and compile a sass file with imports and move it into the dist folder during compilation without editing a webpack config all the better. But I've searched and asked for this in Gitter, Reddit and Stackoverflow for weeks and I was unsuccessful.

@bennett000 I'm not following perfectly what you are saying. Simply put, for me everything is fine as long as I can keep working with PostCSS. Updating from beta.10 to beta.1X is not the issue (although the more I wait to get back on track the more it will be an headache).

Kunepro avatar Sep 22 '16 01:09 Kunepro

@Kunepro I had a pretty specific case where that made sense for me. Seemed like it would also make sense for other projects that had modified sys-js configs from that era of the cli.

bennett000 avatar Sep 22 '16 02:09 bennett000

@bennett000 Can you explain the solution you used?

EDIT: Ok, Now I see what you meant. You are still using system-js instead of webpack, you just updated from angular.RCX to angular2 and updated the necessary files to keep working with system-js.

Kunepro avatar Sep 22 '16 03:09 Kunepro

@kunepro seeking this functionality myself.

richpeck avatar Sep 22 '16 08:09 richpeck

@hansl Is there any possibility of opening up the PostCSS plugins before that? (Rather than full Webpack configuration) Since the CLI is already using PostCSS, it seems like it should be fairly easy and non-intrusive, and not having those CSS tools is a major pain point.

wulfsberg avatar Sep 22 '16 08:09 wulfsberg

PostCss plugin access would help for those of us who have moved away from sass in favour of cssnext.

Even if I go back to sass I would still need the PostCss autoprefixer plugin - I honestly don't know how anyone does modern css without it :)

SethDavenport avatar Sep 22 '16 13:09 SethDavenport

Seeing how the new "style.css" works the possibility to add PostCSS plugins + configuration at this point would perfectly satisfy all my needs.

Kunepro avatar Sep 23 '16 08:09 Kunepro

Guys, is there any way to customize webpack config before 2017?

alexsorokoletov avatar Sep 26 '16 21:09 alexsorokoletov

For anyone desperate for a configurable webpack setup, I'd recommend this seed project: https://github.com/qdouble/angular2webpack2-starter . This seed also has AOT compilation, which is what is keeping the cli team from spending time adding the webpack config I guess.

Matmo10 avatar Sep 26 '16 21:09 Matmo10

@hansl My use cases for a customized webpack config are:

  • Adding some more loaders (pug-loader)
  • Using HtmlWebpackPlugin to use pug template instead of index.html
  • Using git-revision-webpack-plugin and `definePlugin' to inject some version data to my logger automatically.
  • Using proxy of webpack dev server.

muratcorlu avatar Sep 27 '16 16:09 muratcorlu

proxy works already. pug is still being considered but will be after the final 1.0, if anything comes out of it. Custom plugins are a no go for now as well. So really you should be using your own configuration.

hansl avatar Sep 27 '16 16:09 hansl

Everyone, I'm going to lock this conversation and close this issue as we already said everything there is to say.

Please refer to my comment above (and cf here as well) for the official word from the CLI team.

hansl avatar Sep 27 '16 16:09 hansl