Skip to content

Commit

Permalink
feat(nf): support assets for dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Sep 9, 2023
1 parent 4b88da8 commit af94e15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
4 changes: 4 additions & 0 deletions libs/native-federation-core/src/lib/core/default-skip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const DEFAULT_SKIP_LIST: SkipList = [
'@angular/localize',
'@angular/localize/init',
'@angular/localize/tools',
'@angular/platform-server',
'@angular/platform-server/init',
'@angular/ssr',
'express',
/\/schematics(\/|$)/,
(pkg) => pkg.startsWith('@angular/') && !!pkg.match(/\/testing(\/|$)/),
(pkg) => pkg.startsWith('@types/'),
Expand Down
13 changes: 5 additions & 8 deletions libs/native-federation/src/builders/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,14 @@ import {
} from '../../utils/dev-server';
import { RebuildHubs } from '../../utils/rebuild-events';
import { updateIndexHtml } from '../../utils/updateIndexHtml';
import { appendFileSync, existsSync, mkdirSync } from 'fs';
import { existsSync, mkdirSync } from 'fs';
import {
EsBuildResult,
MemResults,
NgCliAssetResult,
} from '../../utils/mem-resuts';
import { JsonObject } from '@angular-devkit/core';

function log(...args) {
const msg = args.join(' ');
appendFileSync('c:/temp/log.txt', msg + '\n');
console.log(args);
}

export async function* runBuilder(
nfOptions: NfBuilderSchema,
context: BuilderContext
Expand Down Expand Up @@ -123,7 +117,6 @@ export async function* runBuilder(
write,
})) {
lastResult = output;
yield output;

if (!output.success) {
setError('Compilation Error');
Expand Down Expand Up @@ -159,6 +152,10 @@ export async function* runBuilder(
reloadBrowser();
}

if (!runServer) {
yield output;
}

if (!first && watch) {
setTimeout(async () => {
logger.info('Rebuilding federation artefacts ...');
Expand Down
4 changes: 4 additions & 0 deletions libs/native-federation/src/utils/angular-esbuild-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ function writeResult(
writtenFiles.push(filePath);
}

if (!memOnly) {
// for (const asset of result.outputFiles)
}

return writtenFiles;
}

Expand Down
11 changes: 2 additions & 9 deletions libs/native-federation/src/utils/dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import * as browserSync from 'browser-sync';
import { NfBuilderSchema } from '../builders/build/schema';
import { BuildResult, MemResults } from './mem-resuts';
import { appendFileSync } from 'fs';
import { basename, extname } from 'path';
import { extname } from 'path';
import { lookup } from 'mrmime';
import { updateScriptTags } from './updateIndexHtml';

let server: browserSync.BrowserSyncInstance;

function log(...args) {
const msg = args.join(' ');
appendFileSync('c:/temp/log.txt', msg + '\n');
console.log(args);
}

export function startServer(
options: NfBuilderSchema,
path: string,
Expand All @@ -33,7 +26,7 @@ export function startServer(
open: options.open,
middleware: [
function (req, res, next) {
const key = basename(req.url || '');
const key = req.url.startsWith('/') ? req.url.substring(1) : req.url;
const result = memResults.get(key);

if (result) {
Expand Down
16 changes: 10 additions & 6 deletions libs/native-federation/src/utils/mem-resuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export interface BuildResult {
}

export class EsBuildResult implements BuildResult {
public fileName: string;

constructor(private outputFile: OutputFile) {
this.fileName = outputFile.path;
get fileName() {
return basename(this.outputFile.path);
}

constructor(private outputFile: OutputFile) {}

get(): Uint8Array {
return this.outputFile.contents;
}
Expand All @@ -26,7 +26,7 @@ export interface NgCliAssetFile {

export class NgCliAssetResult implements BuildResult {
public get fileName(): string {
return this.file.source;
return unify(this.file.destination);
}

private file: NgCliAssetFile;
Expand All @@ -45,7 +45,7 @@ export class MemResults {

public add(result: BuildResult[]): void {
for (const file of result) {
this.map.set(basename(file.fileName), file);
this.map.set(file.fileName, file);
}
}

Expand All @@ -57,3 +57,7 @@ export class MemResults {
return [...this.map.keys()];
}
}

function unify(path) {
return path?.replace(/\\/g, '/');
}

0 comments on commit af94e15

Please sign in to comment.