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

Since 1.6.4 Angular Material CSS import does not work anymore (breaking change)

Open Martin-Luft opened this issue 8 years ago • 45 comments

Versions

Angular CLI: 1.6.4
Node: 9.4.0
OS: linux x64
Angular: 5.2.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cdk: 5.0.4
@angular/cli: 1.6.4
@angular/material: 5.0.4
@angular-devkit/build-optimizer: 0.0.37
@angular-devkit/core: 0.0.24
@angular-devkit/schematics: 0.0.45
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.4
@schematics/angular: 0.1.12
typescript: 2.6.2
webpack: 3.10.0

Repro steps

A new Angular CLI and Angular Material project like mentioned here: https://material.angular.io/guide/getting-started

Observed behavior

The import @import "~@angular/material/prebuilt-themes/indigo-pink.css"; in the styles.css fails:

ERROR in ./node_modules/css-loader?{"sourceMap":false,"import":false}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./src/styles.css
Module build failed: Error: Can't resolve '~@angular/material/prebuilt-themes/indigo-pink.css' in '/.../src'
    at onError (/.../node_modules/enhanced-resolve/lib/Resolver.js:61:15)
    at loggingCallbackWrapper (/.../node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
    at runAfter (/.../node_modules/enhanced-resolve/lib/Resolver.js:158:4)
    at innerCallback (/.../node_modules/enhanced-resolve/lib/Resolver.js:146:3)
    at loggingCallbackWrapper (/.../node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
    at next (/.../node_modules/tapable/lib/Tapable.js:252:11)
    at /.../node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:40:4
    at loggingCallbackWrapper (/.../node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
    at runAfter (/.../node_modules/enhanced-resolve/lib/Resolver.js:158:4)
    at innerCallback (/.../node_modules/enhanced-resolve/lib/Resolver.js:146:3)
    at loggingCallbackWrapper (/.../node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
    at next (/.../node_modules/tapable/lib/Tapable.js:252:11)
    at innerCallback (/.../node_modules/enhanced-resolve/lib/Resolver.js:144:11)
    at loggingCallbackWrapper (/.../node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
    at next (/.../node_modules/tapable/lib/Tapable.js:249:35)
    at resolver.doResolve.createInnerCallback (/.../node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:44:6)
 @ ./src/styles.css 4:14-138
 @ multi ./src/styles.css

Desired behavior

No error or update Angular Material Getting started section.

Mention any other details that might be useful (optional)

Worked until 1.6.3 :)

Martin-Luft avatar Jan 12 '18 13:01 Martin-Luft

Same problem here The postcss-url callbacks are never invoked

jolafrite avatar Jan 12 '18 13:01 jolafrite

This is indeed a defect and will be corrected in the next patch release. The tilde can actually be removed to retain existing behavior.

clydin avatar Jan 12 '18 14:01 clydin

Same problem here

ERROR in ./node_modules/css-loader?{"sourceMap":false,"import":false}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./src/styles.css Module build failed: Error: Can't resolve '~@angular/material/prebuilt-themes/indigo-pink.css'

pferretti avatar Jan 12 '18 14:01 pferretti

Thanks @clydin ! removing the tilde correct the behaviour for Angular Material theme import. In my project I also have an import for

@import "~material-design-icons/iconfont/material-icons.css";

and removing the tilde actually give the error


ERROR in ./node_modules/css-loader?{"sourceMap":false,"import":false}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./src/styles.css
Module not found: Error: Can't resolve './MaterialIcons-Regular.woff2'

It worked until angular cli version 1.6.3 :-)

pferretti avatar Jan 12 '18 14:01 pferretti

@pferretti I provided further details over here on that particular problem: https://github.com/angular/angular-cli/issues/9166#issuecomment-357249253

Also, unless the application does not have access to the internet or has restrictions thereof, adding this to index.html should typically provide a performance benefit over the package import method:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

clydin avatar Jan 12 '18 14:01 clydin

Thank you @clydin , but unfortunately my app has restrictions and I have to rely on font self-hosting.

For reference, I temporarily copied Material Icon fonts in my assets folder and added to style.css the styles specified at https://google.github.io/material-design-icons/#setup-method-2-self-hosting

Probably not elegant, but it's a temporary solution that works :-)

pferretti avatar Jan 12 '18 14:01 pferretti

@pferretti You can also add the file to the .angular-cli.json styles array. That should copy it to the source and make a style link to it in head (just like it does for src/styles.css).

Edit: Nevermind, you need multiple files, then that wont work

LosD avatar Jan 12 '18 14:01 LosD

Is this related to the issue with the updated autoprefixer? The autoprefixer version that was inhibiting use of [email protected] still does not appear to be functioning. (See 975f45a)

aarongranade avatar Jan 12 '18 15:01 aarongranade

@aarongranade it's not related and that should be working. If it is not, ensure that the project's version of the CLI is updated (not just the global) and if the problem persists check which versions of autoprefixer are installed via npm ls autoprefixer and please respond in this issue: https://github.com/angular/angular-cli/issues/9029

clydin avatar Jan 12 '18 15:01 clydin

the solution I do to fix this issue is to change the part in https://github.com/angular/angular-cli/blob/1.6.x/packages/%40angular/cli/models/webpack-configs/styles.ts#L70 to

postcssImports({
        resolve: (url: string, context: string) => {
          return new Promise<string>((resolve, reject) => {
            if(url.startsWith('~')) {
              url = url.substr(1);
            }
            loader.resolve(context, url, (err: Error, result: string) => {
              if (err) {
                reject(err);
                return;
              }

              resolve(result);
            });
          });
        },

jolafrite avatar Jan 12 '18 17:01 jolafrite

Regarding the release notes for Angular 5.2

I'm frankly getting sick of reading things like 'This is a minor release that is a drop-in replacement for 5.1 containing many bugfixes.' and then having to spend 2 hours getting it to work until i stumble upon the right issue after having gone round and round in circles.

I just cannot fathom how nobody involved in releasing 5.2 would stop to think 'hmm perhaps this update of our main project might cause a conflict with the latest version of our other main project'.

It just wastes probably literally millions of hours and all you need is to add a Known Issues section that is kept updated. I'm sorry but it's pathetic.

simeyla avatar Jan 12 '18 22:01 simeyla

I moved the @imports from my css styles to my scss styles and they worked again. Note that I was only having the issue for --prod builds so not sure if it's a different issue.

In SCSS: @import url('https://fonts.googleapis.com/css?family=Material+Icons');

ravishivt avatar Jan 13 '18 01:01 ravishivt

I am also having this issue. Is there an easy way to roll back to angular-cli 1.6.0?

skalpin avatar Jan 13 '18 02:01 skalpin

I am also having this issue.

zahmee avatar Jan 13 '18 08:01 zahmee

Had the same issue, but for now I just downgraded CLI back to 1.6.3 (exact), and everything is fine.

sniederb avatar Jan 13 '18 09:01 sniederb

@sniederb not work with me :(

zahmee avatar Jan 13 '18 10:01 zahmee

It's fixed, we just have to wait for a new release.

cyrilletuzi avatar Jan 13 '18 10:01 cyrilletuzi

anyone know how to downgrade i've tried :(after uninstall the leatest and clean cache verifed) npm install -g @angular/[email protected] but it installed 1.6.4

seffyfisher avatar Jan 13 '18 12:01 seffyfisher

I did exactly what @seffyfischer posted, plus put the exact 1.6.3 version in packages. json. Worked for me

sniederb avatar Jan 13 '18 12:01 sniederb

@sniederb i changed the "@angular/cli": "1.6.3" in the package.json still no good . im still getting

"ERROR in ./node_modules/css-loader?{"sourceMap":false,"import":false}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./src/styles.css
Module build failed: Error: Can't resolve '~@angular/material/prebuilt-themes/indigo-pink.css' in 
...."

after putting @import "~@angular/material/prebuilt-themes/indigo-pink.css"; inside style.css

// if you enter in the terminal ng -v what do you get ? (im still getting 1.6.4)

seffyfisher avatar Jan 13 '18 12:01 seffyfisher