diff --git a/src/compiler/build/build-finish.ts b/src/compiler/build/build-finish.ts index ad55b86d..0273e1d7 100644 --- a/src/compiler/build/build-finish.ts +++ b/src/compiler/build/build-finish.ts @@ -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'; diff --git a/src/compiler/output-targets/dist-collection/index.ts b/src/compiler/output-targets/dist-collection/index.ts index b301006c..9dd23130 100644 --- a/src/compiler/output-targets/dist-collection/index.ts +++ b/src/compiler/output-targets/dist-collection/index.ts @@ -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'; @@ -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); diff --git a/src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts b/src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts index 07609e5c..b26da8b7 100644 --- a/src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts +++ b/src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts @@ -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'; diff --git a/src/compiler/output-targets/output-lazy-loader.ts b/src/compiler/output-targets/output-lazy-loader.ts index dc066335..40929309 100644 --- a/src/compiler/output-targets/output-lazy-loader.ts +++ b/src/compiler/output-targets/output-lazy-loader.ts @@ -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'; @@ -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() }; `; diff --git a/src/compiler/output-targets/output-www.ts b/src/compiler/output-targets/output-www.ts index 136038a8..0a08523e 100644 --- a/src/compiler/output-targets/output-www.ts +++ b/src/compiler/output-targets/output-www.ts @@ -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'; diff --git a/src/compiler/prerender/prerender-queue.ts b/src/compiler/prerender/prerender-queue.ts index 3d6bb053..91bc0fc0 100644 --- a/src/compiler/prerender/prerender-queue.ts +++ b/src/compiler/prerender/prerender-queue.ts @@ -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'; diff --git a/src/compiler/service-worker/service-worker-util.ts b/src/compiler/service-worker/service-worker-util.ts index 75c96463..c68788b7 100644 --- a/src/compiler/service-worker/service-worker-util.ts +++ b/src/compiler/service-worker/service-worker-util.ts @@ -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; diff --git a/src/compiler/transformers/collections/parse-collection-module.ts b/src/compiler/transformers/collections/parse-collection-module.ts index 040db22e..6196396a 100644 --- a/src/compiler/transformers/collections/parse-collection-module.ts +++ b/src/compiler/transformers/collections/parse-collection-module.ts @@ -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'; diff --git a/src/compiler/transformers/map-imports-to-path-aliases.ts b/src/compiler/transformers/map-imports-to-path-aliases.ts index b97cfc03..3a07c7b8 100644 --- a/src/compiler/transformers/map-imports-to-path-aliases.ts +++ b/src/compiler/transformers/map-imports-to-path-aliases.ts @@ -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'; diff --git a/src/compiler/transformers/rewrite-aliased-paths.ts b/src/compiler/transformers/rewrite-aliased-paths.ts index 36b406d9..42c1e6e7 100644 --- a/src/compiler/transformers/rewrite-aliased-paths.ts +++ b/src/compiler/transformers/rewrite-aliased-paths.ts @@ -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'; diff --git a/src/compiler/transformers/rindo-import-path.ts b/src/compiler/transformers/rindo-import-path.ts index 9323999c..7430e972 100644 --- a/src/compiler/transformers/rindo-import-path.ts +++ b/src/compiler/transformers/rindo-import-path.ts @@ -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'; @@ -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; } diff --git a/src/compiler/transformers/static-to-meta/component.ts b/src/compiler/transformers/static-to-meta/component.ts index 7267a819..3392b80d 100644 --- a/src/compiler/transformers/static-to-meta/component.ts +++ b/src/compiler/transformers/static-to-meta/component.ts @@ -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'; diff --git a/src/compiler/transformers/type-library.ts b/src/compiler/transformers/type-library.ts index 553d63b2..c21ce940 100644 --- a/src/compiler/transformers/type-library.ts +++ b/src/compiler/transformers/type-library.ts @@ -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'; @@ -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')) { diff --git a/src/compiler/transpile/run-program.ts b/src/compiler/transpile/run-program.ts index 4044d037..7324b203 100644 --- a/src/compiler/transpile/run-program.ts +++ b/src/compiler/transpile/run-program.ts @@ -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'; diff --git a/src/compiler/transpile/validate-components.ts b/src/compiler/transpile/validate-components.ts index 2dbf3045..f52dc5f0 100644 --- a/src/compiler/transpile/validate-components.ts +++ b/src/compiler/transpile/validate-components.ts @@ -1,5 +1,4 @@ -import { buildError } from '@utils'; -import { relative } from 'path'; +import { buildError, relative } from '@utils'; import type * as d from '../../declarations'; diff --git a/src/utils/index.ts b/src/utils/index.ts index b345a5cb..e7c2ace7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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'; diff --git a/src/utils/logger/logger-typescript.ts b/src/utils/logger/logger-typescript.ts index 0093148c..7ca829ea 100644 --- a/src/utils/logger/logger-typescript.ts +++ b/src/utils/logger/logger-typescript.ts @@ -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'; /** diff --git a/src/utils/normalize-path.ts b/src/utils/path.ts similarity index 86% rename from src/utils/normalize-path.ts rename to src/utils/path.ts index 40cdbbf9..4e8da8e2 100644 --- a/src/utils/normalize-path.ts +++ b/src/utils/path.ts @@ -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 @@ -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); +}