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

Serve styles and assets using .angular-cli webpack configuration #2735

Merged
merged 17 commits into from
Jan 19, 2018

Conversation

Quramy
Copy link
Contributor

@Quramy Quramy commented Jan 12, 2018

Issue: See #2645, #2688

What I did

Add ability to configure webpack using @angular/cli and user's .angular-cli.json.

So @storybook/anguar serves according to the .angular-cli.json:

  • Global styles
  • Static asset files

And if user don't have .angular-cli.json, the minimum configuration is provided as before.

How to test

See example/angular-cli manually 🙏 . I added new component and story under examples/angular-cli/src/stories/component-with-style. Open this story, you can see the following styled DOM:

  • Scoped CSS(with templateURL)
  • Scoped SCSS(with templateURL)
  • Global CSS

And open localhost:9008/assets/common.css, you can confirm that static files listed .angular-cli.json are served.

@Quramy
Copy link
Contributor Author

Quramy commented Jan 12, 2018

@igor-dv @alterx how about this?

@alterx
Copy link
Member

alterx commented Jan 12, 2018

cc @amcdnl :D

}
const appConfig = cliConfig.apps[appIndex];

// FIXME dummy value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which of these are dummy values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's outputPath: 'outPutpath'. I'll amend comment 🙇

import {
getAngularCliWebpackConfigOptions,
applyAngularCliWebpackConfig,
} from './angular-cli_config';

// avoid ESLint errors
const logger = console;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use @storybook/node-logger

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Quramy, I think this is the last comment to take care of

// We should not require('@angular/cli') at the top script level because user project might not have `@angular/cli`.
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
ngcliConfigFactory = require('@angular/cli/models/webpack-configs');
} catch (e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see this changed a bit.

I'd like to extract a function that detects the installment of @angular/cli.

If it's not installed, we just merge the base, we might log that.
If it's installed we try to get their webpack config.
This might fail, since it's a deep file in a npm module.
We should log in either case.

Copy link
Member

@amcdnl amcdnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@codecov
Copy link

codecov bot commented Jan 12, 2018

Codecov Report

Merging #2735 into master will decrease coverage by 0.24%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2735      +/-   ##
==========================================
- Coverage   35.75%   35.51%   -0.25%     
==========================================
  Files         433      434       +1     
  Lines        9441     9501      +60     
  Branches      995     1010      +15     
==========================================
- Hits         3376     3374       -2     
- Misses       5414     5455      +41     
- Partials      651      672      +21
Impacted Files Coverage Δ
app/angular/src/server/babel_config.js 75.86% <ø> (-0.81%) ⬇️
app/angular/src/server/index.js 0% <0%> (ø) ⬆️
app/angular/src/server/build.js 0% <0%> (ø) ⬆️
app/angular/src/server/ts_config.js 38.09% <0%> (-2.82%) ⬇️
app/angular/src/server/angular-cli_config.js 0% <0%> (ø)
app/angular/src/server/config.js 0% <0%> (ø) ⬆️
app/react/src/server/config/babel.js 0% <0%> (-100%) ⬇️
app/react/src/server/utils.js 0% <0%> (-53.58%) ⬇️
lib/ui/src/modules/ui/configs/init_panels.js 25% <0%> (ø) ⬆️
... and 66 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 694d5d9...25d2fdb. Read the comment docs.

@@ -76,10 +76,6 @@ export default function(configDir) {
loader: 'raw-loader',
exclude: /\.async\.css$/,
},
{
test: /\.scss$/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to configure scss in case there is no angular-cli ?

Copy link
Member

@alterx alterx Jan 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do this:

  • angular-cli present -> use whatever's in their config
  • not present? Just fallback to good ol' css.

Technically, if they're not using the cli we aren't officially supporting it. So they should config everything manually. This feels consistent.

@amcdnl what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the vast majority of people will be using the cli so its pretty safe bet. There is the 1% case when they eject from the CLI we might need to think about ( https://github.com/angular/angular-cli/wiki/eject ) but that just nets the 'guts' of the CLI in a basic webpack config file as if you configured it manually yourself.

Personally, I'd fall under the thought process of try to handle as much as you can w/o config (seems to be a trend now i.e. parcel) so leaving the scss wouldn't hurt. Just my 2 cents tho.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so leaving the scss wouldn't hurt

Thanks your feedback, I'll revet this change 😄


const logger = console;

export function isAngularCliInstalled() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exporting it?


const mod = {
...baseConfig.module,
rules: [...cliStyleConfig.module.rules, ...styleRules],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need these styleRules if they should be filtered out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, the variable name styleRules is so bad 🙇
This array includes rules for .jsx, .ts, ..., so we need it.

I'll change the variable's name.

@igor-dv
Copy link
Member

igor-dv commented Jan 17, 2018

@Quramy , you need to update snapshots to fix tests =)

@alterx alterx added the angular label Jan 17, 2018
@Quramy
Copy link
Contributor Author

Quramy commented Jan 18, 2018

@igor-dv storyshots for Angular 🎉 !
I added the shapshot file 😄

@alterx alterx merged commit e6e4a0a into storybookjs:master Jan 19, 2018
@Quramy Quramy deleted the merge-angular-cli-webpack-config branch January 19, 2018 05:38
@igor-dv
Copy link
Member

igor-dv commented Jan 19, 2018

@Quramy , As I understand, after this fix we can't already import global css into config.js, because now I see this in the example app:

image

Having this config:

import { configure } from '@storybook/angular';
import addCssWarning from '../src/cssWarning';
import '../src/assets/common.css' // <--- this

addCssWarning();

function loadStories() {
  // put welcome screen at the top of the list so it's the first one displayed
  require('../src/stories');

  // automatically import all story ts files that end with *.stories.ts
  const req = require.context('../src/stories', true, /\.stories\.ts$/)
  req.keys().forEach((filename) => req(filename))
}

configure(loadStories, module);

If it's ok I'll just put the css from common.css to the style.css

@Quramy
Copy link
Contributor Author

Quramy commented Jan 20, 2018

@igor-dv

Importing global CSS file manually(import '../src/assets/common.css') is no longer needed.

For instance, in example app src/style.css is listed in .angular-cli.json as global style files:

  "apps": [
    {
      :
      "styles": [
        "styles.css"
      ],
      :
      }
    }
  ],

So it's imported automatically as ng serve does . Specifically, this procedure adds src/styles.css to webpack context.

So, I think we can remove the following lines from example's config:

import '../src/assets/common.css' // <--- this

And move the following selector to src/styles.css from assets/common.css :

.css-rules-warning {
  display: none;
}

@therepo90
Copy link

Good job mate. Does it support angular 6?

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

Successfully merging this pull request may close these issues.

7 participants