"Fossies" - the Fresh Open Source Software Archive

Member "angular-cli-15.2.4/packages/angular_devkit/build_angular/src/builders/browser-esbuild/tests/options/allowed-common-js-dependencies_spec.ts" (16 Mar 2023, 5027 Bytes) of package /linux/www/angular-cli-15.2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * @license
    3  * Copyright Google LLC All Rights Reserved.
    4  *
    5  * Use of this source code is governed by an MIT-style license that can be
    6  * found in the LICENSE file at https://angular.io/license
    7  */
    8 
    9 import { logging } from '@angular-devkit/core';
   10 import { buildEsbuildBrowser } from '../../index';
   11 import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
   12 
   13 describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
   14   describe('Option: "allowedCommonJsDependencies"', () => {
   15     describe('given option is not set', () => {
   16       for (const aot of [true, false]) {
   17         it(`should show warning when depending on a Common JS bundle in ${
   18           aot ? 'AOT' : 'JIT'
   19         } Mode`, async () => {
   20           // Add a Common JS dependency
   21           await harness.appendToFile('src/app/app.component.ts', `import 'buffer';`);
   22 
   23           harness.useTarget('build', {
   24             ...BASE_OPTIONS,
   25             allowedCommonJsDependencies: [],
   26             optimization: true,
   27             aot,
   28           });
   29 
   30           const { result, logs } = await harness.executeOnce();
   31 
   32           expect(result?.success).toBe(true);
   33           expect(logs).toContain(
   34             jasmine.objectContaining<logging.LogEntry>({
   35               message: jasmine.stringMatching(
   36                 /Module 'buffer' used by 'src\/app\/app\.component\.ts' is not ESM/,
   37               ),
   38             }),
   39           );
   40           expect(logs).toContain(
   41             jasmine.objectContaining<logging.LogEntry>({
   42               message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
   43             }),
   44           );
   45           expect(logs).not.toContain(
   46             jasmine.objectContaining<logging.LogEntry>({
   47               message: jasmine.stringMatching('base64-js'),
   48             }),
   49             'Should not warn on transitive CommonJS packages which parent is also CommonJS.',
   50           );
   51         });
   52       }
   53     });
   54 
   55     it('should not show warning when depending on a Common JS bundle which is allowed', async () => {
   56       // Add a Common JS dependency
   57       await harness.appendToFile(
   58         'src/app/app.component.ts',
   59         `
   60         import 'buffer';
   61       `,
   62       );
   63 
   64       harness.useTarget('build', {
   65         ...BASE_OPTIONS,
   66         allowedCommonJsDependencies: ['buffer', 'base64-js', 'ieee754'],
   67         optimization: true,
   68       });
   69 
   70       const { result, logs } = await harness.executeOnce();
   71 
   72       expect(result?.success).toBe(true);
   73       expect(logs).not.toContain(
   74         jasmine.objectContaining<logging.LogEntry>({
   75           message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
   76         }),
   77       );
   78     });
   79 
   80     it('should not show warning when depending on zone.js', async () => {
   81       // Add a Common JS dependency
   82       await harness.appendToFile(
   83         'src/app/app.component.ts',
   84         `
   85         import 'zone.js';
   86       `,
   87       );
   88 
   89       harness.useTarget('build', {
   90         ...BASE_OPTIONS,
   91         allowedCommonJsDependencies: [],
   92         optimization: true,
   93       });
   94 
   95       const { result, logs } = await harness.executeOnce();
   96 
   97       expect(result?.success).toBe(true);
   98       expect(logs).not.toContain(
   99         jasmine.objectContaining<logging.LogEntry>({
  100           message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
  101         }),
  102       );
  103     });
  104 
  105     it(`should not show warning when importing non global local data '@angular/common/locale/fr'`, async () => {
  106       await harness.appendToFile(
  107         'src/app/app.component.ts',
  108         `import '@angular/common/locales/fr';`,
  109       );
  110 
  111       harness.useTarget('build', {
  112         ...BASE_OPTIONS,
  113         allowedCommonJsDependencies: [],
  114         optimization: true,
  115       });
  116 
  117       const { result, logs } = await harness.executeOnce();
  118 
  119       expect(result?.success).toBe(true);
  120       expect(logs).not.toContain(
  121         jasmine.objectContaining<logging.LogEntry>({
  122           message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
  123         }),
  124       );
  125     });
  126 
  127     it('should not show warning in JIT for templateUrl and styleUrl when using paths', async () => {
  128       await harness.modifyFile('tsconfig.json', (content) => {
  129         return content.replace(
  130           /"baseUrl": ".\/",/,
  131           `
  132             "baseUrl": "./",
  133             "paths": {
  134               "@app/*": [
  135                 "src/app/*"
  136               ]
  137             },
  138           `,
  139         );
  140       });
  141 
  142       await harness.modifyFile('src/app/app.module.ts', (content) =>
  143         content.replace('./app.component', '@app/app.component'),
  144       );
  145 
  146       harness.useTarget('build', {
  147         ...BASE_OPTIONS,
  148         allowedCommonJsDependencies: [],
  149         optimization: true,
  150         aot: false,
  151       });
  152 
  153       const { result, logs } = await harness.executeOnce();
  154 
  155       expect(result?.success).toBe(true);
  156       expect(logs).not.toContain(
  157         jasmine.objectContaining<logging.LogEntry>({
  158           message: jasmine.stringMatching(/CommonJS or AMD dependencies/),
  159         }),
  160       );
  161     });
  162   });
  163 });