Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): added origin filepath to processNamespace #2313

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions packages/cli/test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -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, {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/resolve-namespace-factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ 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, originStylesheetPath?: string) => {
const configPath = findConfig('package.json', {
cwd: dirname(originStylesheetPath || stylesheetPath),
});
if (!configPath) {
throw new Error(`Could not find package.json for ${stylesheetPath}`);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/stylable-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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);
}

Expand Down