From 74b4002baf10db28aa859823f1f4ec6b0f313f09 Mon Sep 17 00:00:00 2001 From: Barak Igal Date: Sun, 6 Feb 2022 18:25:53 +0200 Subject: [PATCH 1/3] feat(core): added origin filepath to processNamespace --- packages/core/src/resolve-namespace-factories.ts | 4 ++-- packages/core/src/stylable-processor.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core/src/resolve-namespace-factories.ts b/packages/core/src/resolve-namespace-factories.ts index fd343b4b8..7ad1b217b 100644 --- a/packages/core/src/resolve-namespace-factories.ts +++ b/packages/core/src/resolve-namespace-factories.ts @@ -12,8 +12,8 @@ export function packageNamespaceFactory( prefix = '', normalizeVersion = (semver: string) => semver ): typeof processNamespace { - return (namespace: string, stylesheetPath: string) => { - const configPath = findConfig('package.json', { cwd: dirname(stylesheetPath) }); + return (namespace: string, stylesheetPath: string, origin?: string) => { + const configPath = findConfig('package.json', { cwd: dirname(origin || stylesheetPath) }); if (!configPath) { throw new Error(`Could not find package.json for ${stylesheetPath}`); } diff --git a/packages/core/src/stylable-processor.ts b/packages/core/src/stylable-processor.ts index a1cff6ed8..0c04bafb6 100644 --- a/packages/core/src/stylable-processor.ts +++ b/packages/core/src/stylable-processor.ts @@ -271,7 +271,8 @@ export class StylableProcessor implements FeatureContext { namespace, pathToSource ? path.resolve(path.dirname(this.meta.source), pathToSource) - : this.meta.source + : this.meta.source, + this.meta.source ); } @@ -632,7 +633,7 @@ export function createEmptyMeta(root: postcss.Root, diagnostics: Diagnostics): S return new StylableMeta(root, diagnostics); } -export function processNamespace(namespace: string, source: string) { +export function processNamespace(namespace: string, source: string, _origin?: string) { return namespace + murmurhash3_32_gc(source); // .toString(36); } From 4d796b697673908a30d158cd989b7de3b893b9c1 Mon Sep 17 00:00:00 2001 From: Barak Igal Date: Mon, 7 Feb 2022 15:22:31 +0200 Subject: [PATCH 2/3] added test --- packages/cli/test/cli.spec.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/cli/test/cli.spec.ts b/packages/cli/test/cli.spec.ts index faff2783b..9e417459d 100644 --- a/packages/cli/test/cli.spec.ts +++ b/packages/cli/test/cli.spec.ts @@ -1,10 +1,10 @@ -import { join } from 'path'; +import { join, dirname, relative } from 'path'; import { expect } from 'chai'; import { createTempDirectory, ITempDirectory } from 'create-temp-directory'; import { evalStylableModule } from '@stylable/module-utils/dist/test/test-kit'; import { resolveNamespace } from '@stylable/node'; import { loadDirSync, populateDirectorySync, runCliSync } from '@stylable/e2e-test-kit'; -import { processorWarnings } from '@stylable/core'; +import { processorWarnings, packageNamespaceFactory } from '@stylable/core'; import { STImport } from '@stylable/core/dist/features'; describe('Stylable Cli', function () { @@ -255,6 +255,36 @@ describe('Stylable Cli', function () { expect(stdout).to.contain('I HAVE BEEN REQUIRED'); }); + it('single file with namespace reference and use the origin to find the package.json', () => { + populateDirectorySync(tempDir.path, { + 'package.json': `{"name": "test", "version": "0.0.0"}`, + 'style.st.css': `/* st-namespace-reference="../../invalid-path.st.css" */.root{color:red}`, + }); + + runCliSync(['--rootDir', tempDir.path]); + + const resolveNamespace = packageNamespaceFactory( + () => join(tempDir.path, 'package.json'), + () => { + return { name: 'test', version: '0.0.0' }; + }, + { dirname, relative } + ); + const dirContent = loadDirSync(tempDir.path); + expect( + evalStylableModule<{ namespace: string }>( + dirContent['style.st.css.js'], + 'style.st.css.js' + ).namespace + ).equal( + resolveNamespace( + 'style', + join(tempDir.path, '../../invalid-path.st.css'), + join(tempDir.path, 'style.st.css') + ) + ); + }); + describe('CLI diagnostics', () => { it('should report diagnostics by default and exit the process with error exit code 1', () => { populateDirectorySync(tempDir.path, { From e6fbb53d0c311b580f51ca82dd22be998ddbbb40 Mon Sep 17 00:00:00 2001 From: Barak Igal Date: Mon, 7 Feb 2022 15:24:02 +0200 Subject: [PATCH 3/3] rename param --- packages/core/src/resolve-namespace-factories.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/src/resolve-namespace-factories.ts b/packages/core/src/resolve-namespace-factories.ts index 7ad1b217b..cbefcfda4 100644 --- a/packages/core/src/resolve-namespace-factories.ts +++ b/packages/core/src/resolve-namespace-factories.ts @@ -12,8 +12,10 @@ export function packageNamespaceFactory( prefix = '', normalizeVersion = (semver: string) => semver ): typeof processNamespace { - return (namespace: string, stylesheetPath: string, origin?: string) => { - const configPath = findConfig('package.json', { cwd: dirname(origin || stylesheetPath) }); + return (namespace: string, stylesheetPath: string, originStylesheetPath?: string) => { + const configPath = findConfig('package.json', { + cwd: dirname(originStylesheetPath || stylesheetPath), + }); if (!configPath) { throw new Error(`Could not find package.json for ${stylesheetPath}`); }