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

https://github.com/nikolasleblanc/angular-cli/tree/Fe-Webpack-Overrides

Here's an implementation of my proposed solution. No longer able to access the webpack branch of angular-cli, so can't submit a PR.

nikolasleblanc avatar Aug 12 '16 01:08 nikolasleblanc

The webpack branch is actually merged into master. So you can do that there. Also keep into consideration how we currently have webpack using the nodeAPI and tied into the the cli command process. All of these things should be thought about. This is a pretty complex feature in my opinion, and will take some work, so if we propose this, I want it well vetted.

TheLarkInn avatar Aug 12 '16 02:08 TheLarkInn

Works for me. Done for the night but will submit a revised PR in the morning.

nikolasleblanc avatar Aug 12 '16 02:08 nikolasleblanc

the proposal from @nikolasleblanc sounds really good. I'd love to see this in action.

nigel-dewar avatar Aug 12 '16 05:08 nigel-dewar

Sorry but we will not expose the webpack confit for various reasons. An addon system is being designed that would allow such a thing but for the time being we won't expose it.

hansl avatar Aug 12 '16 05:08 hansl

fair enough. looking forward to it.

nigel-dewar avatar Aug 12 '16 05:08 nigel-dewar

@hansl, can you elaborate on a) your concerns about exposing the config and/or b) your proposal for the add-on system?

This seems to be a feature in high demand and pulling back the curtain on your plans so others could help out or focus their own efforts more to your vision may help accelerate their implementation while taking some of the load off your plate.

nikolasleblanc avatar Aug 12 '16 11:08 nikolasleblanc

I have to say, I personally like webpack for two reasons.

  1. It's really good at bundling, and
  2. it's very configurable.

The webpack CLI implementation currently provides the value of (1) but ignores (2). I'm very happy with the progress that's been made, but the reality for me is that without (2) it's only halfway there.

I like the default config, a good set of defaults is really important. However those defaults should not be locked. Opinionated is fine as long as it's not overly prescriptive.

So to @nikolasleblanc's point, some insight into your goals and rationale for this decision would help us all come up with the design proposal the community needs.

SethDavenport avatar Aug 12 '16 11:08 SethDavenport

I think there is ample good reason for the CLI team to be cautious about webpack configuration extension. This configurability is both a great strength and a great weakness of the tool.

Others above have praised the value of configurability, no need for me to repeat that.

The weakness comes in with what the community does with it. I have worked with various teams at companies large and small who have created monstrosities of webpack configuration. They do this because they must to survive and make their project work, but it is not a good thing.

The configurability leaves too much room for idiosyncrasies where there should be conventions. By keeping this configurability limited, there is an opportunity to create pressure on both project authors, and more importantly library authors, to build their projects and libraries in a way that they "just work" with a reasonable common default configuration. Lacking that pressure, what I most commonly see is that each time another library is added to a project, the project's webpack complexity escalates further, and eventually for a project of substantial size, it is necessary for one or more of the very most capable people on the project team to devote extensive brainpower to somehow wrangling the webpack configuration to support the requirements of a pile of libraries plus project idiosyncrasies all at the same time.

To get a tiny flavor of this, just spend a few minutes searching the web, looking at all the advice out there of mountains of configuration to copy paste into your webpack configuration to make scenario X or library Y work. Then imagine yourself as someone who is not yet a webpack expert, attempting to divine which bits to copypaste, which bits to ignore, and how to combine those results for six different libraries you are trying to use together on a project.

I don't know if this problem is solvable, I don't know if it is feasible to create enough conventions to avoid the need for each project to have an intricate snowflake of webpack configuration. But it is valuable and worthwhile and wise of the Angular CLI team to at least have the goal of taming it.

kylecordes avatar Aug 12 '16 12:08 kylecordes

To assume that one webpack config is the right one seems a disservice to it, and is it really angular-cli's responsibility to save people from their own configuration?

The defaults can certainly ensure that those unfamiliar with webpack can be protected from themselves, but the ability to override those defaults ensures that those that are familiar with webpack can still reap the benefits of their particular config along with those offered by the cli.

nikolasleblanc avatar Aug 12 '16 12:08 nikolasleblanc

@kylecordes Perhaps. But at what cost? Flexibility is what allows a healthy dev community to innovate. Angular-CLI is in a privileged position where its choices may end up constraining a lot of people. This is a concern to me.

For instance - I now am forced to consider going back to SASS, which we abandoned a year ago, simply because the CLI makes me. Why is a JS tool effectively dictating my CSS strategy to me?

But ultimately that's just a symptom. I am cool with opinionated defaults. I am not as cool with prescriptivity.

SethDavenport avatar Aug 12 '16 12:08 SethDavenport

I'm not saying there should be no configurability, I'm just acknowledging that the team is wise to think about some ideas other than just opening it up wide.

But realistically, if we want full webpack configurability, why use CLI? Why not just use Webpack? We can go grab a webpack starter off the web, copy it into a project, and go. I looked at one of them a couple of weeks ago (sorry, I forget which) and with it a project starts out with about 800 lines of webpack configuration, before we start editing it for project needs and to accommodate the needs additional libraries.

I suspect that CLI is going to be extremely popular among small to medium-size projects, particularly those that are willing to adopt its conventions, and that it may need to optimize for these cases. I think for larger projects, enterprise projects, many of them will start with CLI, build some good project momentum, then eventually dropped it and switch to their own build process, webpack configuration, etc. I don't think that is a failure - it would mean CLI helps projects adopt Angular and become successful with it.

Anyway, these are just my thoughts, inferred from watching all of this from the outside. I'm not on the core Angular or CLI team, I'm just out here in the woods working on training material, example projects, and production customer projects. Maybe one of the core guys can chime in with a more official stance, when they are ready.

kylecordes avatar Aug 12 '16 12:08 kylecordes

But realistically, if we want full webpack configurability, why use CLI?

Well, for all the other benefits that CLI offers. Lack of configurability of the webpack config, on a cli that moved from systemJS to webpack, is what ultimately will force those on larger projects to branch off.

My first inclination upon running ng create was to update the webpack config, so imagine, I didn't even make a small or medium sized project before I considered branching off. I hadn't even started.

It's not necessarily a failure that people may branch off, but it shouldn't be an acceptable outcome either. The community seems eager to adopt CLI for their entire workflow, and the lack of a customizable webpack config is really the only thing preventing them from doing so.

nikolasleblanc avatar Aug 12 '16 13:08 nikolasleblanc

But realistically, if we want full webpack configurability, why use CLI? Why not just use Webpack?

Because of the features the CLI provides ...

  • Generating Components, Directives, Pipes and Services
  • Generating a Route
  • Creating a Build
  • Build Targets and Environment Files
  • Bundling
  • Running Unit Tests
  • Running End-to-End Tests
  • Deploying the App via GitHub Pages
  • Linting and formatting code
  • Support for offline applications
  • Commands autocompletion

Some pretty compelling reasons to want to use the CLI.

The CLI is using postcss, and one of the nice features about postcss is their plugin system, as there is a rather extensive set of plugins that you could use.

The postcss plugins that we frequently use are postcss-import, postcss-cssnext and cssnano.

If there is a reservation about allowing configuring the webpack config and exposing it, what about a means of allowing people to register the postcss plugins that they would like to use?

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

Or is the use of postcss an implementation detail that you are looking to protect developers from?

e-schultz avatar Aug 12 '16 13:08 e-schultz

For instance - I now am forced to consider going back to SASS, which we abandoned a year ago, simply because the CLI makes me. Why is a JS tool effectively dictating my CSS strategy to me?

@SethDavenport you also can use Less, Stylus, PostCSS (no plugins yet) out of the box just like you use SASS. Unless there is something i've missed. By default right now all of the above are possible.

Also I'll try to followup with some of this discussion later today.

TheLarkInn avatar Aug 12 '16 13:08 TheLarkInn

Thanks @TheLarkInn - yes I took a look at the existing source. SCSS, Less and Stylus are brought in as PostCSS plugins. What I'm looking for is a different set of PostCSS plugins: CSSNext & family.

@kylecordes you raise reasonable points. On the topic of starting with the CLI and then rolling your own config once the project gets large, that's not a bad idea. I can imagine something along the lines of create-react-app's "eject" feature. In my case however I run into an issue from the first line of CSS that I write.

To be fair, I have been rolling my own webpack configs since Angular2 beta.1 and will likely continue in the short term.

However increasingly I'm fielding questions from clients along the lines of "why aren't you using that official angular-cli thing"?

If I'm wrong, and the angular-cli is not intended to be the 'official way to do Ng2' then of course they should implement whatever they like :)

On the other hand, angular-cli's apparent positioning as the 'official way to do Ng2' incurs a certain level of responsibility for flexibility IMO. The alternative is undesirable constraints on the community.

SethDavenport avatar Aug 12 '16 14:08 SethDavenport

I totally hear you on the part about "official". This CLI is not just "a" CLI. It is "the" CLI... It lives in the official repository, and resides online at CLI.angular.io. This means it is in a privileged position; there is little reason for anyone else to go about making a competing tool. Such a tool would stand almost no chance of gaining popularity.

I imagine the team probably feels this responsibility pretty heavily: whatever they can come up with, is what the Angular community is going to get. Whatever problems they don't solve, are not going to be solved in the tool "everyone" uses. To the extent Angular succeeds, it will be partly because of their efforts. To the extent people wander instead over to competing non-Angular libraries, it will be partly because of their efforts.

kylecordes avatar Aug 12 '16 14:08 kylecordes

One of the largest reasons I can see for keeping the Webpack config hidden is keeping the option open to migrate off of Webpack or takes on some of the functionality in the future. Exposing the internal build tool dependency to end users is a proven path to endless difficulties.

dpsthree avatar Aug 12 '16 16:08 dpsthree

Not only that but the less we expose to you, the easier it is for us to help you migrate. Just throwing those two cents in. Don't take that statement as me swaying one way or the other officially.

TheLarkInn avatar Aug 12 '16 17:08 TheLarkInn

This would address https://github.com/angular/angular-cli/issues/1541. Looking forward to whatever solution that's landed on!

adamduren avatar Aug 12 '16 17:08 adamduren