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

Angular 6 regression on \n (newline) removal in components using <ng-template> -- too aggressive.

Open brianpilati opened this issue 8 years ago • 6 comments

I am going to provide steps for both angular 5 and angular 6.

Versions


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 6.0.1
Node: 8.9.4
OS: darwin x64
Angular: 6.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.1
@angular-devkit/build-angular     0.6.1
@angular-devkit/build-optimizer   0.6.1
@angular-devkit/core              0.6.1
@angular-devkit/schematics        0.6.1
@angular/cli                      6.0.1
@ngtools/webpack                  6.0.1
@schematics/angular               0.6.1
@schematics/update                0.6.1
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.6.0

tsconfig.json

This is my tsconfig.json file

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "preserveWhitespaces": true
  }
}

Repro steps

Observed behavior

In this image there are three examples for how angular 6 handles <pre> elements in templates and in components with angular-6-too-aggressive

The first image is a <pre> and <code> tags without a component. It is formatted correctly.

The second image has a component with ng-template that wraps the already wrapped <pre> and <code> tags in another set of <pre> and <code> tags. It is formatted correctly given the nested tags.

The third image has a component with ng-template that wraps the text in a single set of <pre> and <code> tags. This shows the regression in Angular 6.

This is the code from main.js. You can see in the third example the \n have NOT been preserved. function ms(e){return ri(0,[(e()(),Gr(0,0,null,null,6,"div",[ ["class","example-box"]],null,null,null,null,null)),(e()(),ei(-1,null,[' Angular 6 - with " pre" and "code" '])),(e()(),Gr(2,0,null,null,4,"pre",[],null,null,null,null,null)),(e()(),e i(-1,null,[" "])),(e()(),Gr(4,0,null,null,1,"code",[],null,null,null,null,null)),(e()( ),ei(-1,null,["\n Hello There\n This is\n A formatted test\n {\ n properties: [\n 'a string',\n 'array'\n ],\n objects: {\n key: {\n key2: value\n }\n }\ n }\n "])),(e()(),ei(-1,null,["\n "])),(e()(),Gr(7,0,null,null,8,"div",[["cl ass","example-box"]],null,null,null,null,null)),(e()(),ei(-1,null,[' Angular 6 - with "pre" , "code" and component '])),(e()(),Gr(9,0,null,null,6,"app-pre-example",[],null,null,null,y s,vs)),Ro(10,114688,null,0,hs,[],null,null),(e()(),Gr(11,0,null,0,4,"pre",[],null,null,null ,null,null)),(e()(),ei(-1,null,[" "])),(e()(),Gr(13,0,null,null,1,"code",[],null,null, null,null,null)),(e()(),ei(-1,null,["\n Hello There\n This is\n A form atted test\n {\n properties: [\n 'a string',\n 'array '\n ],\n objects: {\n key: {\n key2: value\n }\n }\n }\n "])),(e()(),ei(-1,null,["\n "])),(e()(),Gr(16,0, null,null,4,"div",[["class","example-box"]],null,null,null,null,null)),(e()(),ei(-1,null,[" Angular 6 - only component "])),(e()(),Gr(18,0,null,null,2,"app-pre-example",[],null,null, null,ys,vs)),Ro(19,114688,null,0,hs,[],null,null),(e()(),ei(-1,0,[" Hello There This is A f ormatted test { properties: [ 'a string', 'array' ], objects: { key: { key2: value } } } "] ))],function(e,t){e(t,10,0),e(t,19,0)},null)}var _s=function(e,t,n,r,o,i)

Versions

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.7.4
Node: 8.9.4
OS: darwin x64
Angular: 5.2.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.7.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0

Repro steps

Observed behavior

In this image there are three examples for how angular 5 handles <pre> elements in templates and in components with angular-5-just-right

The first image is a <pre> and <code> tags without a component. It is formatted correctly.

The second image has a component with ng-template that wraps the already wrapped <pre> and <code> tags in another set of <pre> and <code> tags. It is formatted correctly given the nested tags.

The third image has a component with ng-template that wraps the text in a single set of <pre> and <code> tags. This shows that angular 5 handled it correctly.

This is the code from main.js. You can see in the third example the \n HAVE been preserved. function l(e){return r._4 (0,[(e()(),r.P(0,0,null,null,7,"div",[["class","example-box"]],null,null,null,nu ll,null)),(e()(),r._3(-1,null,['\n Angular 5 - with "pre" and "code"\n '])),(e ()(),r.P(2,0,null,null,4,"pre",[],null,null,null,null,null)),(e()(),r._3(-1,null ,[" "])),(e()(),r.P(4,0,null,null,1,"code",[],null,null,null,null,null)),(e ()(),r._3(-1,null,["\n Hello There\n This is\n A formatted test\n {\n properties: [\n 'a string',\n ' array'\n ],\n objects: {\n key: {\n ke y2: value\n }\n }\n }\n "])),(e()(),r._3(-1,null ,["\n "])),(e()(),r._3(-1,null,["\n"])),(e()(),r._3(-1,null,["\n\n"])),(e()() ,r.P(9,0,null,null,11,"div",[["class","example-box"]],null,null,null,null,null)) ,(e()(),r._3(-1,null,['\n Angular 5 - with "pre", "code" and component \n '])) ,(e()(),r.P(11,0,null,null,8,"app-pre-example",[],null,null,null,a,u)),r.O(12,11 4688,null,0,s,[],null,null),(e()(),r._3(-1,0,["\n "])),(e()(),r.P(14,0,null,0 ,4,"pre",[],null,null,null,null,null)),(e()(),r._3(-1,null,[" "])),(e()(),r .P(16,0,null,null,1,"code",[],null,null,null,null,null)),(e()(),r._3(-1,null,["\ n Hello There\n This is\n A formatted test\n {\n properties: [\n 'a string',\n 'array'\n ],\ n objects: {\n key: {\n key2: value\n }\n }\n }\n "])),(e()(),r._3(-1,null,["\n "])),(e()(), r._3(-1,0,["\n "])),(e()(),r._3(-1,null,["\n"])),(e()(),r._3(-1,null,["\n\n"])) ,(e()(),r.P(22,0,null,null,5,"div",[["class","example-box"]],null,null,null,null ,null)),(e()(),r._3(-1,null,["\n Angular 5 - only component \n "])),(e()(),r.P (24,0,null,null,2,"app-pre-example",[],null,null,null,a,u)),r.O(25,114688,null,0 ,s,[],null,null),(e()(),r._3(-1,0,["\n Hello There\n This is\n A format ted test\n {\n properties: [\n 'a string',\n 'array'\n ],\n objects: {\n key: {\n key2: value\n }\n }\n }\n "])),(e()(),r._3(-1,null,["\n"])),(e()(),r._3(-1,null,["\n"]))]

Observed behavior - Part II

Maybe the flag for preserveWhitespace isn't working. Here is the output with and without the flag.

With preserveWhitespace

$ ng build --prod

Date: 2018-05-16T15:21:28.439Z
Hash: 4545c4c01a068c3834e6
Time: 5554ms
chunk {0} runtime.6afe30102d8fe7337431.js (runtime) 1.05 kB [entry] [rendered]
chunk {1} styles.34c57ab7888ec1573f9c.css (styles) 0 bytes [initial] [rendered]
chunk {2} polyfills.2903ad11212d7d797800.js (polyfills) 59.6 kB [initial] [rendered]
chunk {3} main.59b7cd03fa429a6abf31.js (main) 157 kB [initial] [rendered]
total 472
  0 drwxr-xr-x  9 brianpilati  staff     288 May 16 09:21 .
  0 drwxr-xr-x  3 brianpilati  staff      96 May 16 09:21 ..
  8 -rw-r--r--  1 brianpilati  staff    2179 May 16 09:21 3rdpartylicenses.txt
 16 -rw-r--r--  1 brianpilati  staff    5430 May 16 09:21 favicon.ico
  8 -rw-r--r--  1 brianpilati  staff     597 May 16 09:21 index.html
312 -rw-r--r--  1 brianpilati  staff  156598 May 16 09:21 main.59b7cd03fa429a6abf31.js
120 -rw-r--r--  1 brianpilati  staff   59561 May 16 09:21 polyfills.2903ad11212d7d797800.js
  8 -rw-r--r--  1 brianpilati  staff    1053 May 16 09:21 runtime.6afe30102d8fe7337431.js
  0 -rw-r--r--  1 brianpilati  staff       0 May 16 09:21 styles.34c57ab7888ec1573f9c.css

Without preserveWhitespace

$ ng build --prod


Date: 2018-05-16T15:22:13.520Z
Hash: 4545c4c01a068c3834e6
Time: 5524ms
chunk {0} runtime.6afe30102d8fe7337431.js (runtime) 1.05 kB [entry] [rendered]
chunk {1} styles.34c57ab7888ec1573f9c.css (styles) 0 bytes [initial] [rendered]
chunk {2} polyfills.2903ad11212d7d797800.js (polyfills) 59.6 kB [initial] [rendered]
chunk {3} main.59b7cd03fa429a6abf31.js (main) 157 kB [initial] [rendered]
total 472
  0 drwxr-xr-x  9 brianpilati  staff     288 May 16 09:22 .
  0 drwxr-xr-x  3 brianpilati  staff      96 May 16 09:22 ..
  8 -rw-r--r--  1 brianpilati  staff    2179 May 16 09:22 3rdpartylicenses.txt
 16 -rw-r--r--  1 brianpilati  staff    5430 May 16 09:22 favicon.ico
  8 -rw-r--r--  1 brianpilati  staff     597 May 16 09:22 index.html
312 -rw-r--r--  1 brianpilati  staff  156598 May 16 09:22 main.59b7cd03fa429a6abf31.js
120 -rw-r--r--  1 brianpilati  staff   59561 May 16 09:22 polyfills.2903ad11212d7d797800.js
  8 -rw-r--r--  1 brianpilati  staff    1053 May 16 09:22 runtime.6afe30102d8fe7337431.js
  0 -rw-r--r--  1 brianpilati  staff       0 May 16 09:22 styles.34c57ab7888ec1573f9c.css

Desired behavior

The angular 6 compiler needs to be aware that code in a template is going to be wrapped in a <pre> tag by another component using ng-template. In others words, no regression from 5 to 6.

brianpilati avatar May 15 '18 20:05 brianpilati

It' probably related to change in angular 6 of default value of preserveWhitespaces flag. Add this to tsconfig:

"angularCompilerOptions": {
  "preserveWhitespaces": true
}

And it should work as in angular 5.

dawidgarus avatar May 16 '18 08:05 dawidgarus

@dawidgarus Good call. After researching a bit, I did find the preserveWhitespaces flag. This is my tsconfig.json file. Once that did not work, I filed the bug. Also, even if preserveWhitespaces did fix the problem then I considered still logging the bug because having whitespaces is a big loss just to solve this problem.

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "preserveWhitespaces": true
  }
}

brianpilati avatar May 16 '18 15:05 brianpilati

Heya, I just want to mention that angularCompilerOptions must be set in src/tsconfig.app.json. When extending tsconfig the angularCompilerOptions is not inherited.

If you're doing that already the right place to discuss this behaviour is https://github.com/angular/angular though. That is part of how the Angular Compiler itself works and not part of Angular CLI. I understand the distinction isn't very clear but just wanted to point you in the right direction.

filipesilva avatar May 17 '18 17:05 filipesilva

Moved as https://github.com/angular/angular/issues/23970

filipesilva avatar May 17 '18 17:05 filipesilva

Thanks @filipesilva. I do agree it is a thin line. I wasn't sure where I should put this issue either.

I have been thinking about your comment and "another blurred line" is this is removing "\n" (newline characters) and not whitespace characters though one could be a subset.

brianpilati avatar May 18 '18 16:05 brianpilati

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.