depflow 3.0.0-dev.1 → 3.0.0-dev.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -43
- package/dist/builder/Builder.d.ts +279 -0
- package/dist/builder/Builder.js +391 -0
- package/dist/builder/Builder.js.map +1 -0
- package/dist/cli/CLI.d.ts +157 -0
- package/dist/cli/CLI.js +214 -0
- package/dist/cli/CLI.js.map +1 -0
- package/dist/cli/DepFLowCLI.d.ts +93 -0
- package/dist/cli/DepFLowCLI.js +413 -0
- package/dist/cli/DepFLowCLI.js.map +1 -0
- package/dist/cli/Flags.d.ts +106 -0
- package/dist/cli/Flags.js +125 -0
- package/dist/cli/Flags.js.map +1 -0
- package/dist/cli/bin.js +68 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/config/Config.d.ts +690 -0
- package/dist/config/Config.js +45 -0
- package/dist/config/Config.js.map +1 -0
- package/dist/config/ImportMap.d.ts +60 -0
- package/dist/config/ImportMap.js +66 -0
- package/dist/config/ImportMap.js.map +1 -0
- package/dist/config/JsonSchema.d.ts +12 -0
- package/dist/config/JsonSchema.js +26 -0
- package/dist/config/JsonSchema.js.map +1 -0
- package/dist/config/Settings.d.ts +201 -0
- package/dist/config/Settings.js +191 -0
- package/dist/config/Settings.js.map +1 -0
- package/dist/config/TSConfig.d.ts +73 -0
- package/dist/config/TSConfig.js +73 -0
- package/dist/config/TSConfig.js.map +1 -0
- package/dist/config/codecs/JsonCodec.d.ts +30 -0
- package/dist/config/codecs/JsonCodec.js +31 -0
- package/dist/config/codecs/JsonCodec.js.map +1 -0
- package/dist/config/codecs/YamlCodec.d.ts +62 -0
- package/dist/config/codecs/YamlCodec.js +89 -0
- package/dist/config/codecs/YamlCodec.js.map +1 -0
- package/dist/dependency/DepUtils.d.ts +50 -0
- package/dist/dependency/DepUtils.js +71 -0
- package/dist/dependency/DepUtils.js.map +1 -0
- package/dist/dependency/Dependency.d.ts +81 -0
- package/dist/dependency/Dependency.js +71 -0
- package/dist/dependency/Dependency.js.map +1 -0
- package/dist/dependency/Git.d.ts +75 -0
- package/dist/dependency/Git.js +97 -0
- package/dist/dependency/Git.js.map +1 -0
- package/dist/dependency/GitDependency.d.ts +30 -0
- package/dist/dependency/GitDependency.js +79 -0
- package/dist/dependency/GitDependency.js.map +1 -0
- package/dist/dependency/NpmDependency.d.ts +27 -0
- package/dist/dependency/NpmDependency.js +91 -0
- package/dist/dependency/NpmDependency.js.map +1 -0
- package/dist/resolve/AliasCompiler.d.ts +45 -0
- package/dist/resolve/AliasCompiler.js +84 -0
- package/dist/resolve/AliasCompiler.js.map +1 -0
- package/{build/support/PathResolver → dist/resolve}/PathResolver.d.ts +26 -9
- package/dist/resolve/PathResolver.js +133 -0
- package/dist/resolve/PathResolver.js.map +1 -0
- package/dist/resolve/PathRewriter.d.ts +24 -0
- package/{build/support/PathResolver → dist/resolve}/PathRewriter.js +21 -11
- package/dist/resolve/PathRewriter.js.map +1 -0
- package/dist/schema/Config/Builder.d.ts +496 -0
- package/dist/schema/Config/Builder.js +78 -0
- package/dist/schema/Config/Builder.js.map +1 -0
- package/dist/schema/Config/Config.d.ts +673 -0
- package/dist/schema/Config/Config.js +62 -0
- package/dist/schema/Config/Config.js.map +1 -0
- package/dist/schema/Config/Dependency.d.ts +878 -0
- package/dist/schema/Config/Dependency.js +80 -0
- package/dist/schema/Config/Dependency.js.map +1 -0
- package/dist/schema/Config/Extractor.d.ts +157 -0
- package/dist/schema/Config/Extractor.js +43 -0
- package/dist/schema/Config/Extractor.js.map +1 -0
- package/dist/schema/Config/Resolver.d.ts +87 -0
- package/dist/schema/Config/Resolver.js +43 -0
- package/dist/schema/Config/Resolver.js.map +1 -0
- package/dist/schema/Config/Transform.d.ts +90 -0
- package/dist/schema/Config/Transform.js +51 -0
- package/dist/schema/Config/Transform.js.map +1 -0
- package/dist/schema/Defaults.d.ts +20 -0
- package/dist/schema/Defaults.js +24 -0
- package/dist/schema/Defaults.js.map +1 -0
- package/dist/schema/ImportMap.d.ts +30 -0
- package/dist/schema/ImportMap.js +28 -0
- package/dist/schema/ImportMap.js.map +1 -0
- package/dist/schema/TSConfig.d.ts +70 -0
- package/dist/schema/TSConfig.js +39 -0
- package/dist/schema/TSConfig.js.map +1 -0
- package/dist/schema/schema.d.ts +38 -0
- package/dist/schema/schema.js +40 -0
- package/dist/schema/schema.js.map +1 -0
- package/dist/task/Command.d.ts +287 -0
- package/dist/task/Command.js +404 -0
- package/dist/task/Command.js.map +1 -0
- package/dist/task/Group.d.ts +15 -0
- package/dist/task/Group.js +29 -0
- package/dist/task/Group.js.map +1 -0
- package/dist/task/OutputPolicy.d.ts +262 -0
- package/dist/task/OutputPolicy.js +364 -0
- package/dist/task/OutputPolicy.js.map +1 -0
- package/dist/task/Task.d.ts +210 -0
- package/dist/task/Task.js +312 -0
- package/dist/task/Task.js.map +1 -0
- package/package.json +16 -14
- package/.gitignore +0 -7
- package/build/cli/DepFLowCLI.d.ts +0 -15
- package/build/cli/DepFLowCLI.js +0 -223
- package/build/cli/DepFLowCLI.js.map +0 -1
- package/build/cli/bin.js +0 -41
- package/build/cli/bin.js.map +0 -1
- package/build/config/Config.d.ts +0 -28
- package/build/config/Config.js +0 -46
- package/build/config/Config.js.map +0 -1
- package/build/config/ImportMap.d.ts +0 -37
- package/build/config/ImportMap.js +0 -82
- package/build/config/ImportMap.js.map +0 -1
- package/build/config/Tsconfig.d.ts +0 -57
- package/build/config/Tsconfig.js +0 -100
- package/build/config/Tsconfig.js.map +0 -1
- package/build/config/schemas.d.ts +0 -1194
- package/build/config/schemas.js +0 -66
- package/build/config/schemas.js.map +0 -1
- package/build/support/Async.d.ts +0 -22
- package/build/support/Async.js +0 -62
- package/build/support/Async.js.map +0 -1
- package/build/support/Builder/Builder.d.ts +0 -36
- package/build/support/Builder/Builder.js +0 -89
- package/build/support/Builder/Builder.js.map +0 -1
- package/build/support/Dependency/Dependency.d.ts +0 -38
- package/build/support/Dependency/Dependency.js +0 -14
- package/build/support/Dependency/Dependency.js.map +0 -1
- package/build/support/Dependency/GitDependency.d.ts +0 -20
- package/build/support/Dependency/GitDependency.js +0 -67
- package/build/support/Dependency/GitDependency.js.map +0 -1
- package/build/support/Dependency/NpmDependency.d.ts +0 -18
- package/build/support/Dependency/NpmDependency.js +0 -65
- package/build/support/Dependency/NpmDependency.js.map +0 -1
- package/build/support/Dependency/Resolver.d.ts +0 -7
- package/build/support/Dependency/Resolver.js +0 -4
- package/build/support/Dependency/Resolver.js.map +0 -1
- package/build/support/Git.d.ts +0 -53
- package/build/support/Git.js +0 -86
- package/build/support/Git.js.map +0 -1
- package/build/support/PathResolver/AliasCompiler.d.ts +0 -25
- package/build/support/PathResolver/AliasCompiler.js +0 -39
- package/build/support/PathResolver/AliasCompiler.js.map +0 -1
- package/build/support/PathResolver/PathResolver.js +0 -104
- package/build/support/PathResolver/PathResolver.js.map +0 -1
- package/build/support/PathResolver/PathRewriter.d.ts +0 -18
- package/build/support/PathResolver/PathRewriter.js.map +0 -1
- package/build/support/PathResolver/Utils.d.ts +0 -36
- package/build/support/PathResolver/Utils.js +0 -45
- package/build/support/PathResolver/Utils.js.map +0 -1
- package/build/support/Task/Command.d.ts +0 -49
- package/build/support/Task/Command.js +0 -100
- package/build/support/Task/Command.js.map +0 -1
- package/build/support/Task/Task.d.ts +0 -84
- package/build/support/Task/Task.js +0 -145
- package/build/support/Task/Task.js.map +0 -1
- package/build/support/Utils.d.ts +0 -40
- package/build/support/Utils.js +0 -138
- package/build/support/Utils.js.map +0 -1
- package/build/support/Validator.d.ts +0 -22
- package/build/support/Validator.js +0 -28
- package/build/support/Validator.js.map +0 -1
- /package/{build → dist}/cli/bin.d.ts +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <netfeez.dev@gmail.com>.
|
|
3
|
+
* @description Path resolution with Local/CDN support and dual path fixing.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import FS, { promises as FSP } from 'node:fs';
|
|
7
|
+
import { Async, File, Path } from '@netfeez/common-node';
|
|
8
|
+
import { Logger } from "@netfeez/vterm";
|
|
9
|
+
import PathRewriter from './PathRewriter.js';
|
|
10
|
+
import AliasCompiler from './AliasCompiler.js';
|
|
11
|
+
export class PathResolver {
|
|
12
|
+
options;
|
|
13
|
+
/** The extensions of the built files the resolver owns, in any mode. */
|
|
14
|
+
static EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx', '.cjs', '.mjs'];
|
|
15
|
+
logger;
|
|
16
|
+
rewriter;
|
|
17
|
+
outDir;
|
|
18
|
+
aliases;
|
|
19
|
+
constructor(data, projectRoot, options = {}) {
|
|
20
|
+
this.options = options;
|
|
21
|
+
this.logger = options.logger || new Logger({ name: 'PATH-RW' });
|
|
22
|
+
const outDir = data.outDir;
|
|
23
|
+
this.outDir = !Path.isAbsolute(outDir)
|
|
24
|
+
? Path.resolve(projectRoot, outDir)
|
|
25
|
+
: outDir;
|
|
26
|
+
this.aliases = new AliasCompiler(projectRoot).compile(data);
|
|
27
|
+
this.rewriter = new PathRewriter(this.aliases);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The pattern that matches the built files the resolver owns, derived from the extensions it does.
|
|
31
|
+
* @returns The glob pattern of the files to rewrite.
|
|
32
|
+
**/
|
|
33
|
+
static get filter() {
|
|
34
|
+
return `**/*{${PathResolver.EXTENSIONS.map(extension => extension.slice(1)).join(',')}}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolves and rewrites paths in built files based on the provided mode (local or CDN).
|
|
38
|
+
* It processes all files in the output directory, rewriting import paths according to the configured aliases.
|
|
39
|
+
* @param mode - The resolution mode ('local' or 'cdn') to determine which target paths to use.
|
|
40
|
+
* @throws { Error } When any of the files could not be rewritten, reporting all of them.
|
|
41
|
+
*
|
|
42
|
+
* @remarks A file that cannot be rewritten is not a file that was left alone: a build that reports
|
|
43
|
+
* success while some of its files still point at the source tree is a build that cannot be
|
|
44
|
+
* deployed, so the failure travels up to whoever asked for the resolution.
|
|
45
|
+
*/
|
|
46
|
+
async rewritePaths(mode) {
|
|
47
|
+
this.logger.log(`&C2Starting path resolver in &C3${mode} &C2mode for ${this.outDir}...`);
|
|
48
|
+
if (!await File.exists(this.outDir))
|
|
49
|
+
return void this.logger.warn(`Directory &C4${this.outDir}&R not found.`);
|
|
50
|
+
const files = await File.glob(PathResolver.filter, { cwd: this.outDir, absolute: false });
|
|
51
|
+
let rewrittenCount = 0;
|
|
52
|
+
const failed = [];
|
|
53
|
+
const limiter = Async.currencyLimiter(16);
|
|
54
|
+
await Promise.all(files.map((file) => limiter(async () => {
|
|
55
|
+
try {
|
|
56
|
+
if (await this.processFile(Path.join(this.outDir, file), mode))
|
|
57
|
+
rewrittenCount += 1;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
failed.push(`${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
61
|
+
}
|
|
62
|
+
})));
|
|
63
|
+
if (failed.length > 0)
|
|
64
|
+
throw new Error(`Path resolver failed to rewrite ${failed.length} of ${files.length} file(s):\n${failed.join('\n')}`);
|
|
65
|
+
this.logger.log(`&C2Path aliases resolved in &C3${rewrittenCount} &C2files.`);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Processes a single file, rewriting import paths based on the configured aliases and the specified mode (local or CDN).
|
|
69
|
+
* It reads the file content, applies the path rewriting logic, and writes the updated content back to the file system if any changes were made.
|
|
70
|
+
* @param file - The path of the file to process.
|
|
71
|
+
* @param mode - The resolution mode ('local' or 'cdn') to determine which target paths to use for rewriting.
|
|
72
|
+
* @returns A boolean indicating whether the file was modified (true if rewritten, false if no changes were made).
|
|
73
|
+
* @throws { Error } When the file cannot be read, rewritten or written.
|
|
74
|
+
*/
|
|
75
|
+
async processFile(file, mode) {
|
|
76
|
+
const content = await FSP.readFile(file, 'utf8');
|
|
77
|
+
const newContent = this.rewriter.rewrite(content, file, mode);
|
|
78
|
+
if (content === newContent)
|
|
79
|
+
return false;
|
|
80
|
+
await FSP.writeFile(file, newContent, 'utf8');
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Watches the output directory for changes and automatically rewrites paths in modified files based on the configured aliases and specified mode (local or CDN).
|
|
85
|
+
* It sets up a file system watcher that listens for changes in the output directory, and when a relevant file is modified, it triggers the path rewriting process for that file.
|
|
86
|
+
* The method includes debouncing to prevent excessive processing during rapid file changes, ensuring efficient handling of updates while maintaining responsiveness.
|
|
87
|
+
* @param mode - The resolution mode ('local' or 'cdn') to determine which target paths to use for rewriting when changes are detected.
|
|
88
|
+
* @returns A promise that resolves when the watcher is set up and running, allowing the application to continue monitoring for changes indefinitely until manually stopped.
|
|
89
|
+
* @throws Will throw an error if there is an issue setting up the file system watcher or processing files, which can be caught by the caller to handle it appropriately.
|
|
90
|
+
*/
|
|
91
|
+
async watch(mode) {
|
|
92
|
+
await this.rewritePaths(mode);
|
|
93
|
+
this.logger.log(`&C2Watching for changes in &C4${this.outDir}...`);
|
|
94
|
+
const debouncedProcessor = PathResolver.debounce(async (fullPath, filename) => {
|
|
95
|
+
try {
|
|
96
|
+
if (await this.processFile(fullPath, mode)) {
|
|
97
|
+
this.logger.log(`&C2File &C4${filename}&C2 updated.`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
this.logger.error(`Failed to process file &C4${filename}&C7: &C1${error instanceof Error ? error.message : String(error)}`);
|
|
102
|
+
}
|
|
103
|
+
}, 300);
|
|
104
|
+
FS.watch(this.outDir, { recursive: true }, (eventType, filename) => {
|
|
105
|
+
if (!filename)
|
|
106
|
+
return;
|
|
107
|
+
const isTargetExtension = PathResolver.EXTENSIONS.some(ext => filename.endsWith(ext));
|
|
108
|
+
if (!isTargetExtension)
|
|
109
|
+
return;
|
|
110
|
+
const fullPath = Path.join(this.outDir, filename);
|
|
111
|
+
debouncedProcessor(fullPath, filename);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Debounces a function so it only executes after the specified delay has passed since the last invocation.
|
|
116
|
+
* @param fn - The function to debounce.
|
|
117
|
+
* @param delay - The number of milliseconds to wait before executing the function.
|
|
118
|
+
* @returns A debounced version of the function that delays its execution.
|
|
119
|
+
*/
|
|
120
|
+
static debounce(fn, delay) {
|
|
121
|
+
let timeoutId = null;
|
|
122
|
+
return function (...args) {
|
|
123
|
+
if (timeoutId)
|
|
124
|
+
clearTimeout(timeoutId);
|
|
125
|
+
timeoutId = setTimeout(() => {
|
|
126
|
+
fn.apply(this, args);
|
|
127
|
+
timeoutId = null;
|
|
128
|
+
}, delay);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export default PathResolver;
|
|
133
|
+
//# sourceMappingURL=PathResolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PathResolver.js","sourceRoot":"","sources":["../../src/resolve/PathResolver.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,EAAE,EAAE,QAAQ,IAAI,GAAG,EAAE,MAAM,SAAS,CAAC;AAE9C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,OAAO,YAAY,MAAM,mBAAmB,CAAC;AAC7C,OAAO,aAAa,MAAM,oBAAoB,CAAC;AAG/C,MAAM,OAAO,YAAY;IAYD,OAAO;IAX3B,wEAAwE;IACjE,MAAM,CAAU,UAAU,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhE,MAAM,CAAS;IACf,QAAQ,CAAe;IACvB,MAAM,CAAS;IAClB,OAAO,CAAgC;IAEvD,YACI,IAAmB,EACnB,WAAmB,EACH,OAAO,GAAy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AC,iBAAiB;gBAAE,OAAO;YAE/B,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YAClD,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACP,CAAC;IACD;;;;;OAKG;IACK,MAAM,CAAC,QAAQ,CACnB,EAA2B,EAC3B,KAAa;QAEb,IAAI,SAAS,GAA0B,IAAI,CAAC;QAC5C,OAAO,UAAoB,GAAG,IAAU;YACpC,IAAI,SAAS;gBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;YACvC,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;gBACxB,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBACrB,SAAS,GAAG,IAAI,CAAC;YACrB,CAAC,EAAE,KAAK,CAAC,CAAC;QACd,CAAC,CAAC;IACN,CAAC;CACJ;AAOD,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import AliasCompiler from "./AliasCompiler.js";
|
|
2
|
+
export declare class PathRewriter {
|
|
3
|
+
private readonly aliases;
|
|
4
|
+
static readonly IMPORT_REGEX: RegExp;
|
|
5
|
+
constructor(aliases: AliasCompiler.CompiledAlias[]);
|
|
6
|
+
/**
|
|
7
|
+
* Rewrites import paths in the given content based on the configured aliases.
|
|
8
|
+
* It detects import statements and replaces alias paths with their resolved targets.
|
|
9
|
+
* @param content - The file content to rewrite.
|
|
10
|
+
* @param filePath - The path of the file being processed (used for relative path calculations).
|
|
11
|
+
* @param mode - The resolution mode ('local' or 'cdn') to determine which target to use.
|
|
12
|
+
* @returns The rewritten content with resolved import paths.
|
|
13
|
+
*/
|
|
14
|
+
rewrite(content: string, filePath: string, mode: 'local' | 'cdn'): string;
|
|
15
|
+
/**
|
|
16
|
+
* The specifier of an import, which is never a file system path: the separator it is written with
|
|
17
|
+
* is the one of the module system, not the one of the machine that wrote it.
|
|
18
|
+
* @param target - The target of the import, as the file system spells it.
|
|
19
|
+
* @returns The target written with the separator an import is written with.
|
|
20
|
+
**/
|
|
21
|
+
protected static posix(target: string): string;
|
|
22
|
+
}
|
|
23
|
+
export declare namespace PathRewriter { }
|
|
24
|
+
export default PathRewriter;
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <netfeez.dev@gmail.com>.
|
|
3
|
+
* @description Rewrites import statements in file content using compiled aliases.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
1
6
|
import PATH from "node:path";
|
|
2
|
-
import { Path } from "@netfeez/common-node";
|
|
3
7
|
export class PathRewriter {
|
|
4
8
|
aliases;
|
|
5
|
-
projectRoot;
|
|
6
9
|
static IMPORT_REGEX = /(from\s+['"])([^'"]+)(['"])/g;
|
|
7
|
-
constructor(aliases
|
|
10
|
+
constructor(aliases) {
|
|
8
11
|
this.aliases = aliases;
|
|
9
|
-
this.projectRoot = projectRoot;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* Rewrites import paths in the given content based on the configured aliases.
|
|
13
15
|
* It detects import statements and replaces alias paths with their resolved targets.
|
|
14
|
-
* @param content The file content to rewrite.
|
|
15
|
-
* @param filePath The path of the file being processed (used for relative path calculations).
|
|
16
|
-
* @param mode The resolution mode ('local' or 'cdn') to determine which target to use.
|
|
16
|
+
* @param content - The file content to rewrite.
|
|
17
|
+
* @param filePath - The path of the file being processed (used for relative path calculations).
|
|
18
|
+
* @param mode - The resolution mode ('local' or 'cdn') to determine which target to use.
|
|
17
19
|
* @returns The rewritten content with resolved import paths.
|
|
18
20
|
*/
|
|
19
21
|
rewrite(content, filePath, mode) {
|
|
@@ -36,12 +38,20 @@ export class PathRewriter {
|
|
|
36
38
|
if (isCDN)
|
|
37
39
|
return `${prefix}${resolvedPath}${suffix}`;
|
|
38
40
|
const currentFileDir = PATH.dirname(filePath);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return `${prefix}${Path.normalize(relativeTarget)}${suffix}`;
|
|
41
|
+
const relative = PATH.relative(currentFileDir, resolvedPath);
|
|
42
|
+
const relativeTarget = relative.startsWith('.') ? relative : `./${relative}`;
|
|
43
|
+
return `${prefix}${PathRewriter.posix(relativeTarget)}${suffix}`;
|
|
43
44
|
});
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The specifier of an import, which is never a file system path: the separator it is written with
|
|
48
|
+
* is the one of the module system, not the one of the machine that wrote it.
|
|
49
|
+
* @param target - The target of the import, as the file system spells it.
|
|
50
|
+
* @returns The target written with the separator an import is written with.
|
|
51
|
+
**/
|
|
52
|
+
static posix(target) {
|
|
53
|
+
return target.split(PATH.sep).join('/');
|
|
54
|
+
}
|
|
45
55
|
}
|
|
46
56
|
export default PathRewriter;
|
|
47
57
|
//# sourceMappingURL=PathRewriter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PathRewriter.js","sourceRoot":"","sources":["../../src/resolve/PathRewriter.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,IAAI,MAAM,WAAW,CAAC;AAI7B,MAAM,OAAO,YAAY;IAGA,OAAO;IAFrB,MAAM,CAAU,YAAY,GAAG,8BAA8B,CAAC;IACrE,YACqB,OAAsC;uBAAtC,OAAO;IACzB,CAAC;IACJ;;;;;;;OAOG;IACI,OAAO,CAAC,OAAe,EAAE,QAAgB,EAAE,IAAqB;QACnE,OAAO,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE;YACpF,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC,C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