Skip to content

Commit

Permalink
#597: finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
petermasking committed Jan 10, 2025
1 parent a9f2fe8 commit e9303e5
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 409 deletions.
6 changes: 6 additions & 0 deletions packages/build/src/definitions/Defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export const Defaults =
{
ACCESS_LEVEL: 'private',
VERSION_NUMBER: '0.0.0'
} as const;
6 changes: 6 additions & 0 deletions packages/build/src/definitions/Files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export const Files =
{
INDEX: 'index.js',
JSON: '.json',
} as const;
5 changes: 5 additions & 0 deletions packages/build/src/definitions/Keywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export const Keywords =
{
DEFAULT: 'default'
} as const;
6 changes: 6 additions & 0 deletions packages/build/src/definitions/Patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export const Patterns =
{
IMPORT: /import\s(?:["'\s]*([\w*{}\n, ]+)from\s*)?["'\s]*([@\w/._-]+)["'\s].*/g,
EXPORT: /export\s(?:["'\s]*([\w*{}\n, ]+)from\s*)?["'\s]*([@\w/._-]+)["'\s].*/g
} as const;
5 changes: 5 additions & 0 deletions packages/build/src/definitions/Values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export const Values =
{
ASTERISK: '*'
} as const;
6 changes: 6 additions & 0 deletions packages/build/src/definitions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export * from './Defaults';
export * from './Files';
export * from './Keywords';
export * from './Patterns';
export * from './Values';
10 changes: 5 additions & 5 deletions packages/build/src/source/module/LocationRewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { Parser } from '@jitar/analysis';
import type { FileManager } from '@jitar/sourcing';

import { FileHelper, LocationReplacer } from '../../utils';
import { Files, Patterns } from '../../definitions';
import { FileHelper } from '../../utils';

// The location rewriter ensures the '.js' for all application imports and re-exports
// and appends the 'index.js' reference for all directory imports.
Expand All @@ -13,7 +14,6 @@ export default class LocationRewriter

readonly #parser = new Parser();
readonly #fileHelper = new FileHelper();
readonly #locationReplacer = new LocationReplacer();

constructor(sourceFileManager: FileManager)
{
Expand All @@ -31,14 +31,14 @@ export default class LocationRewriter
{
const replacer = (statement: string) => this.#replaceImport(filename, statement);

return this.#locationReplacer.replaceImports(code, replacer);
return code.replaceAll(Patterns.IMPORT, replacer);
}

#rewriteExports(filename: string, code: string): string
{
const replacer = (statement: string) => this.#replaceExport(filename, statement);

return this.#locationReplacer.replaceExports(code, replacer);
return code.replaceAll(Patterns.EXPORT, replacer);
}

#replaceImport(filename: string, statement: string): string
Expand Down Expand Up @@ -83,7 +83,7 @@ export default class LocationRewriter
const translated = this.#fileHelper.makePathAbsolute(from, callingModulePath);

return this.#sourceFileManager.isDirectory(translated)
? `${from}/index.js`
? `${from}/${Files.INDEX}`
: this.#fileHelper.assureExtension(from);
}
}
6 changes: 4 additions & 2 deletions packages/build/src/source/resource/Reader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import type { FileManager } from '@jitar/sourcing';

import { Files } from '../../definitions';
import { FileHelper } from '../../utils';

import ResourcesList from './models/ResourcesList';
import FileNotLoaded from './errors/FileNotLoaded';
import type ResourceFile from './types/File';
import { FileHelper } from '../../utils';

export default class ResourceReader
{
Expand Down Expand Up @@ -52,7 +54,7 @@ export default class ResourceReader
// if the given filename is a directory.

const fullFilename = this.#sourceFileManager.isDirectory(filename)
? `${filename}/index.js`
? `${filename}/${Files.INDEX}`
: this.#fileHelper.assureExtension(filename);

if (fullFilename.startsWith('./')) return fullFilename.substring(2);
Expand Down
15 changes: 6 additions & 9 deletions packages/build/src/source/segment/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ESFunction, ESClass } from '@jitar/analysis';
import type { FileManager } from '@jitar/sourcing';

import { Defaults, Files, Keywords } from '../../definitions';
import { FileHelper, IdGenerator } from '../../utils';
import type { ModuleRepository } from '../module';

Expand Down Expand Up @@ -37,10 +38,6 @@ type MemberProperties =
fqn: string;
}

const SEGMENT_FILE_EXTENSION = '.json';
const DEFAULT_ACCESS_LEVEL = 'private';
const DEFAULT_VERSION_NUMBER = '0.0.0';

export default class SegmentReader
{
readonly #segmentsFileManager: FileManager;
Expand Down Expand Up @@ -86,7 +83,7 @@ export default class SegmentReader
throw new InvalidFilename(filename);
}

return file.replace(SEGMENT_FILE_EXTENSION, '');
return file.replace(Files.JSON, '');
}

async #loadSegmentDefinition(filename: string): Promise<SegmentFile>
Expand Down Expand Up @@ -131,7 +128,7 @@ export default class SegmentReader
// if the given filename is a directory.

const fullFilename = this.#sourceFileManager.isDirectory(filename)
? `${filename}/index.js`
? `${filename}/${Files.INDEX}`
: this.#fileHelper.assureExtension(filename);

if (fullFilename.startsWith('./')) return fullFilename.substring(2);
Expand Down Expand Up @@ -172,8 +169,8 @@ export default class SegmentReader
const properties = module.imports[importKey];

const name = properties.as ?? model.name;
const access = properties.access ?? DEFAULT_ACCESS_LEVEL;
const version = properties.version ?? DEFAULT_VERSION_NUMBER;
const access = properties.access ?? Defaults.ACCESS_LEVEL;
const version = properties.version ?? Defaults.VERSION_NUMBER;

const fqn = this.#constructFqn(module, name, importKey);

Expand Down Expand Up @@ -204,7 +201,7 @@ export default class SegmentReader
return name;
}

if (module.filename.endsWith('index.js') && importKey === 'default')
if (module.filename.endsWith(Files.INDEX) && importKey === Keywords.DEFAULT)
{
return module.location;
}
Expand Down
17 changes: 3 additions & 14 deletions packages/build/src/target/module/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,13 @@ export default class Builder

const moduleSegments = segmentation.getSegments(module.filename);

// For resource files we don't want to delete the file, because it is not renamed

if (resources.isResourceModule(module.filename))
{
return;
}

// If the module is not part of any segment, it is an application module
// and these are also not renamed, therefore we don't want to delete them

if (moduleSegments.length === 0)
{
// For unsegmented modules we only need to build the common module.

return;
}

// Otherwise, it is a segment module that can be called remotely
// these are renamed and we need to delete the original file that we copied


const segmentBuilds = moduleSegments.map(segment => this.#buildSegmentModule(module, resources, segment, segmentation));

const firstModuleSegment = moduleSegments[0];
Expand Down
Loading

0 comments on commit e9303e5

Please sign in to comment.