|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { after, before, describe, it } from 'node:test'; |
| 7 | +import { fileURLToPath } from 'node:url'; |
| 8 | + |
| 9 | +const preload = fileURLToPath(new URL('./enable-source-locations.mjs', import.meta.url)); |
| 10 | + |
| 11 | +// Stands in for the CLI: prints whether source locations are on in the main thread and in a worker |
| 12 | +// thread, where the CLI compiles templates. |
| 13 | +const CLI = ` |
| 14 | +import { Worker } from 'node:worker_threads'; |
| 15 | +const { enabled } = await import('@angular/compiler'); |
| 16 | +const worker = new Worker(new URL('./worker.mjs', import.meta.url)); |
| 17 | +worker.once('message', (inWorker) => console.log(JSON.stringify({ main: enabled(), worker: inWorker }))); |
| 18 | +`; |
| 19 | +const WORKER = ` |
| 20 | +import { parentPort } from 'node:worker_threads'; |
| 21 | +const { enabled } = await import('@angular/compiler'); |
| 22 | +parentPort.postMessage(enabled()); |
| 23 | +`; |
| 24 | + |
| 25 | +const OLD_COMPILER = ` |
| 26 | +let on = false; |
| 27 | +export const setEnableTemplateSourceLocations = (value) => (on = value); |
| 28 | +export const enabled = () => on; |
| 29 | +`; |
| 30 | +// Since 22.1.4 the setter is gone, replaced by the compiler option. |
| 31 | +const NEW_COMPILER = 'export const enabled = () => false;'; |
| 32 | + |
| 33 | +describe('enable-source-locations preload', () => { |
| 34 | + let workspace; |
| 35 | + |
| 36 | + const useCompiler = (code) => |
| 37 | + writeFileSync(join(workspace, 'node_modules/@angular/compiler/index.mjs'), code); |
| 38 | + |
| 39 | + const ng = (...args) => { |
| 40 | + const { NGX_SOURCE_INSPECTOR_SERVING, ...env } = process.env; |
| 41 | + const output = execFileSync(process.execPath, ['--import', preload, 'ng.mjs', ...args], { |
| 42 | + cwd: workspace, |
| 43 | + env, |
| 44 | + encoding: 'utf8', |
| 45 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 46 | + }); |
| 47 | + return JSON.parse(output); |
| 48 | + }; |
| 49 | + |
| 50 | + before(() => { |
| 51 | + workspace = mkdtempSync(join(tmpdir(), 'ngx-source-inspector-')); |
| 52 | + mkdirSync(join(workspace, 'node_modules/@angular/compiler'), { recursive: true }); |
| 53 | + writeFileSync( |
| 54 | + join(workspace, 'node_modules/@angular/compiler/package.json'), |
| 55 | + JSON.stringify({ name: '@angular/compiler', exports: { '.': { default: './index.mjs' } } }), |
| 56 | + ); |
| 57 | + writeFileSync(join(workspace, 'package.json'), '{}'); |
| 58 | + writeFileSync(join(workspace, 'ng.mjs'), CLI); |
| 59 | + writeFileSync(join(workspace, 'worker.mjs'), WORKER); |
| 60 | + }); |
| 61 | + |
| 62 | + after(() => rmSync(workspace, { recursive: true, force: true })); |
| 63 | + |
| 64 | + it('turns source locations on for the dev server, worker threads included', () => { |
| 65 | + useCompiler(OLD_COMPILER); |
| 66 | + for (const args of [['serve'], ['s', '--port', '4201'], ['dev'], ['run', 'app:serve']]) { |
| 67 | + assert.deepEqual(ng(...args), { main: true, worker: true }, args.join(' ')); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it('leaves every other command alone', () => { |
| 72 | + useCompiler(OLD_COMPILER); |
| 73 | + for (const args of [['build'], ['build', '--configuration', 'production'], ['test'], []]) { |
| 74 | + assert.deepEqual(ng(...args), { main: false, worker: false }, args.join(' ')); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it('does nothing in Angular versions that have the compiler option', () => { |
| 79 | + useCompiler(NEW_COMPILER); |
| 80 | + assert.deepEqual(ng('serve'), { main: false, worker: false }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does nothing outside of an Angular workspace', () => { |
| 84 | + const elsewhere = mkdtempSync(join(tmpdir(), 'ngx-source-inspector-')); |
| 85 | + try { |
| 86 | + writeFileSync(join(elsewhere, 'ng.mjs'), 'console.log("started")'); |
| 87 | + const output = execFileSync(process.execPath, ['--import', preload, 'ng.mjs', 'serve'], { |
| 88 | + cwd: elsewhere, |
| 89 | + encoding: 'utf8', |
| 90 | + }); |
| 91 | + assert.equal(output.trim(), 'started'); |
| 92 | + } finally { |
| 93 | + rmSync(elsewhere, { recursive: true, force: true }); |
| 94 | + } |
| 95 | + }); |
| 96 | +}); |
0 commit comments