Skip to content

Commit

Permalink
fix(output-targets): fix path normalization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduy1407 committed Dec 1, 2023
1 parent 90984b3 commit ff46ece
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 41 deletions.
3 changes: 1 addition & 2 deletions src/compiler/build/build-finish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isFunction, isRemoteUrl } from '@utils';
import { relative } from 'path';
import { isFunction, isRemoteUrl, relative } from '@utils';

import type * as d from '../../declarations';
import { generateBuildResults } from './build-results';
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/output-targets/dist-collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
flatOne,
generatePreamble,
isOutputTargetDistCollection,
join,
normalizePath,
relative,
sortBy,
} from '@utils';
import { join, relative } from 'path';
import ts from 'typescript';

import type * as d from '../../../declarations';
Expand Down Expand Up @@ -108,10 +109,10 @@ const writeCollectionManifest = async (
outputTarget: d.OutputTargetDistCollection
) => {
// get the absolute path to the directory where the collection will be saved
const collectionDir = normalizePath(outputTarget.collectionDir);
const { collectionDir } = outputTarget;

// create an absolute file path to the actual collection json file
const collectionFilePath = normalizePath(join(collectionDir, COLLECTION_MANIFEST_FILE_NAME));
const collectionFilePath = join(collectionDir, COLLECTION_MANIFEST_FILE_NAME);

// don't bother serializing/writing the collection if we're not creating a distribution
await compilerCtx.fs.writeFile(collectionFilePath, collectionData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dashToPascalCase, isOutputTargetDistCustomElements, normalizePath } from '@utils';
import { dirname, join, relative } from 'path';
import { dashToPascalCase, isOutputTargetDistCustomElements, join, normalizePath, relative } from '@utils';
import { dirname } from 'path';

import type * as d from '../../../declarations';

Expand Down
11 changes: 5 additions & 6 deletions src/compiler/output-targets/output-lazy-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { generatePreamble, isOutputTargetDistLazyLoader, normalizePath, relativeImport } from '@utils';
import { join, relative } from 'path';
import { generatePreamble, isOutputTargetDistLazyLoader, join, relative, relativeImport } from '@utils';

import type * as d from '../../declarations';
import { getClientPolyfill } from '../app-core/app-polyfills';
Expand Down Expand Up @@ -49,18 +48,18 @@ const generateLoader = async (
const es2017EntryPoint = join(es2017Dir, 'loader.js');
const polyfillsEntryPoint = join(es2017Dir, 'polyfills/index.js');
const cjsEntryPoint = join(cjsDir, 'loader.cjs.js');
const polyfillsExport = `export * from '${normalizePath(relative(loaderPath, polyfillsEntryPoint))}';`;
const polyfillsExport = `export * from '${relative(loaderPath, polyfillsEntryPoint)}';`;
const indexContent = `${generatePreamble(config)}
${es5HtmlElement}
${polyfillsExport}
export * from '${normalizePath(relative(loaderPath, es5EntryPoint))}';
export * from '${relative(loaderPath, es5EntryPoint)}';
`;
const indexES2017Content = `${generatePreamble(config)}
${polyfillsExport}
export * from '${normalizePath(relative(loaderPath, es2017EntryPoint))}';
export * from '${relative(loaderPath, es2017EntryPoint)}';
`;
const indexCjsContent = `${generatePreamble(config)}
module.exports = require('${normalizePath(relative(loaderPath, cjsEntryPoint))}');
module.exports = require('${relative(loaderPath, cjsEntryPoint)}');
module.exports.applyPolyfills = function() { return Promise.resolve() };
`;

Expand Down
3 changes: 1 addition & 2 deletions src/compiler/output-targets/output-www.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cloneDocument, serializeNodeToHtml } from '@rindo/core/mock-doc';
import { catchError, flatOne, isOutputTargetWww, unique } from '@utils';
import { join, relative } from 'path';
import { catchError, flatOne, isOutputTargetWww, join, relative, unique } from '@utils';

import type * as d from '../../declarations';
import { generateEs5DisabledMessage } from '../app-core/app-es5-disabled';
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/prerender/prerender-queue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { buildError, catchError, isFunction, isString } from '@utils';
import { relative } from 'path';
import { buildError, catchError, isFunction, isString, relative } from '@utils';

import type * as d from '../../declarations';
import { crawlAnchorsForNextUrls } from './crawl-urls';
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/service-worker/service-worker-util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { normalizePath } from '@utils';
import { relative } from 'path';
import { relative } from '@utils';

import type * as d from '../../declarations';

export const generateServiceWorkerUrl = (outputTarget: d.OutputTargetWww, serviceWorker: d.ServiceWorkerConfig) => {
let swUrl = normalizePath(relative(outputTarget.appDir, serviceWorker.swDest));
let swUrl = relative(outputTarget.appDir, serviceWorker.swDest);

if (swUrl.charAt(0) !== '/') {
swUrl = '/' + swUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { normalizePath } from '@utils';
import { dirname, join, relative } from 'path';
import { join, normalizePath, relative } from '@utils';
import { dirname } from 'path';

import type * as d from '../../../declarations';
import { parseCollectionManifest } from './parse-collection-manifest';
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/map-imports-to-path-aliases.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { normalizePath } from '@utils';
import { dirname, relative } from 'path';
import { normalizePath, relative } from '@utils';
import { dirname } from 'path';
import ts from 'typescript';

import type * as d from '../../declarations';
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/rewrite-aliased-paths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { normalizePath } from '@utils';
import { dirname, relative } from 'path';
import { normalizePath, relative } from '@utils';
import { dirname } from 'path';
import ts from 'typescript';

import { retrieveTsModifiers } from './transform-utils';
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/transformers/rindo-import-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_STYLE_MODE, isString, normalizePath } from '@utils';
import { basename, dirname, isAbsolute, relative } from 'path';
import { DEFAULT_STYLE_MODE, isString, relative } from '@utils';
import { basename, dirname, isAbsolute } from 'path';

import type { ImportData, ParsedImport, SerializeImportData } from '../../declarations';

Expand All @@ -24,7 +24,6 @@ export const serializeImportPath = (data: SerializeImportData, styleImportData:
if (isString(data.importerPath) && isAbsolute(data.importeePath)) {
p = relative(dirname(data.importerPath), data.importeePath);
}
p = normalizePath(p);
if (!p.startsWith('.')) {
p = './' + p;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/static-to-meta/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { normalizePath, unique } from '@utils';
import { dirname, isAbsolute, join, relative } from 'path';
import { join, normalizePath, relative, unique } from '@utils';
import { dirname, isAbsolute } from 'path';
import ts from 'typescript';

import type * as d from '../../../declarations';
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/transformers/type-library.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { normalizePath } from '@utils';
import path from 'path';
import { normalizePath, relative } from '@utils';
import ts from 'typescript';

import type * as d from '../../declarations';
Expand Down Expand Up @@ -30,8 +29,7 @@ export function addToLibrary(
checker: ts.TypeChecker,
pathToTypeModule: string
): string {
pathToTypeModule = path.relative(process.cwd(), pathToTypeModule);
pathToTypeModule = normalizePath(pathToTypeModule, false);
pathToTypeModule = relative(process.cwd(), pathToTypeModule);

// for now we don't make any attempt to include types in node_modules
if (pathToTypeModule.startsWith('node_modules')) {
Expand Down
11 changes: 9 additions & 2 deletions src/compiler/transpile/run-program.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { getComponentsFromModules, isOutputTargetDistTypes, loadTypeScriptDiagnostics, normalizePath } from '@utils';
import { basename, join, relative } from 'path';
import {
getComponentsFromModules,
isOutputTargetDistTypes,
join,
loadTypeScriptDiagnostics,
normalizePath,
relative,
} from '@utils';
import { basename } from 'path';
import ts from 'typescript';

import type * as d from '../../declarations';
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/transpile/validate-components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { buildError } from '@utils';
import { relative } from 'path';
import { buildError, relative } from '@utils';

import type * as d from '../../declarations';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export * from './logger/logger-rollup';
export * from './logger/logger-typescript';
export * from './logger/logger-utils';
export * from './message-utils';
export * from './normalize-path';
export * from './output-target';
export * from './path';
export * from './query-nonce-meta-tag-content';
export * from './sourcemaps';
export * from './url-paths';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger/logger-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Diagnostic, DiagnosticMessageChain, Node } from 'typescript';

import type * as d from '../../declarations';
import { isIterable } from '../helpers';
import { normalizePath } from '../normalize-path';
import { normalizePath } from '../path';
import { splitLineBreaks } from './logger-utils';

/**
Expand Down
30 changes: 30 additions & 0 deletions src/utils/normalize-path.ts → src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

/**
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
* Forward-slash paths can be used in Windows as long as they're not
Expand Down Expand Up @@ -187,3 +189,31 @@ const enum CharacterCodes {
percent = 0x25, // %
slash = 0x2f, // /
}

/**
* A wrapped version of node.js' {@link path.relative} which adds our custom
* normalization logic. This solves the relative path between `from` and `to`!
*
* @throws the underlying node.js function can throw if either path is not a
* string
* @param from the path where relative resolution starts
* @param to the destination path
* @returns the resolved relative path
*/
export function relative(from: string, to: string): string {
return normalizePath(path.relative(from, to), false);
}

/**
* A wrapped version of node.js' {@link path.join} which adds our custom
* normalization logic. This joins all the arguments (path fragments) into a
* single path.
*
* @throws the underlying node function will throw if any argument is not a
* string
* @param paths the paths to join together
* @returns a joined path!
*/
export function join(...paths: string[]): string {
return normalizePath(path.join(...paths), false);
}

0 comments on commit ff46ece

Please sign in to comment.