Skip to content

Commit ab9443f

Browse files
Timothy Lindvalltimlindvall
Timothy Lindvall
authored andcommitted
refactor: Establish BaseImporter.
- Establish BaseImporter as an abstract class that other Importers can extend from. This will be where we add common utilities that Importers may need for working with CSS Blocks files. - Update NodeJsImporter to extend from BaseImporter.
1 parent b24a65c commit ab9443f

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Syntax } from '../BlockParser';
2+
import { ResolvedConfiguration } from '../configuration';
3+
import { Importer, FileIdentifier, ImportedFile } from './Importer';
4+
5+
/**
6+
* The BaseImporter is an abstract class that Importer implementations may extend from.
7+
* This follows the Importer interface that must be used for interacting with the BlockFactory.
8+
* We also include additional utility methods that are useful for handling CSS Blocks,
9+
* Compiled CSS, and Definition Files.
10+
*/
11+
export abstract class BaseImporter implements Importer {
12+
/**
13+
* Compute a unique identifier for a given import path. If `fromIdentifier` is provided,
14+
* the importPath can be relative to the file that is identified by it.
15+
*/
16+
abstract identifier(fromIdentifier: FileIdentifier | null, importPath: string, config: ResolvedConfiguration): FileIdentifier;
17+
/**
18+
* Import the file with the given metadata and return a string and meta data for it.
19+
*/
20+
abstract import(identifier: FileIdentifier, config: ResolvedConfiguration): Promise<ImportedFile>;
21+
/**
22+
* The default name of the block used unless the block specifies one itself.
23+
*/
24+
abstract defaultName(identifier: FileIdentifier, configuration: ResolvedConfiguration): string;
25+
/**
26+
* If a file identifier has an on-disk representation, return an absolute path to it.
27+
*/
28+
abstract filesystemPath(identifier: FileIdentifier, config: ResolvedConfiguration): string | null;
29+
/**
30+
* Returns a string meant for human consumption that identifies the file.
31+
* As is used in debug statements and error reporting. Unlike filesystemPath,
32+
* this needn't resolve to an actual file or be an absolute path.
33+
*/
34+
abstract debugIdentifier(identifier: FileIdentifier, config: ResolvedConfiguration): string;
35+
/**
36+
* Returns the syntax the contents are written in.
37+
*/
38+
abstract syntax(identifier: FileIdentifier, config: ResolvedConfiguration): Syntax;
39+
}

packages/@css-blocks/core/src/importing/NodeJsImporter.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as path from "path";
66
import { Syntax } from "../BlockParser";
77
import { ResolvedConfiguration } from "../configuration";
88

9-
import { FileIdentifier, ImportedFile, Importer } from "./Importer";
9+
import { FileIdentifier, ImportedFile } from "./Importer";
10+
import { BaseImporter } from "./BaseImporter";
1011

1112
const debug = debugGenerator("css-blocks:importer");
1213

@@ -27,9 +28,11 @@ export interface Alias {
2728
path: string;
2829
}
2930

30-
export class NodeJsImporter implements Importer {
31+
export class NodeJsImporter extends BaseImporter {
3132
aliases: Alias[];
3233
constructor(aliases: Alias[] | ObjectDictionary<string> = []) {
34+
super();
35+
3336
// Normalize aliases input.
3437
this.aliases = Array.isArray(aliases)
3538
? aliases.slice()

0 commit comments

Comments
 (0)