Skip to content

Commit 11969fa

Browse files
committed
fix issue with retaining first '.' in property match and test with real files
1 parent 75747d8 commit 11969fa

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

__tests__/index.spec.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ describe('js-to-styles-vars-loader', () => {
5353
});
5454
});
5555

56-
describe('guardExportType', () => {
56+
describe('validateExportType', () => {
5757
it ("throws on anything except an object, does not throw otherwise", () => {
5858
const areOk = [{}, {a: "foo"}];
5959
const areNotOk = [[], ["a"], "", "123", 123, false, true, null, undefined, NaN];
6060
expect(() => {
6161
for (const okThing of areOk) {
62-
operator.guardExportType(okThing, "");
62+
operator.validateExportType(okThing, "");
6363
}
6464
}).not.toThrow();
6565
for (const notOkThing of areNotOk) {
66-
6766
expect(() => {
68-
operator.guardExportType(notOkThing, "");
67+
operator.validateExportType(notOkThing, "");
68+
console.error(`Should have thrown on ${typeof notOkThing} '${notOkThing}'`);
6969
},).toThrow();
7070

7171
}
@@ -180,7 +180,7 @@ describe('js-to-styles-vars-loader', () => {
180180
expect(context.addDependency).toHaveBeenCalledWith(path.resolve(dependencyPath));
181181
});
182182

183-
it('gives back content as is if there is no requre', () => {
183+
it('gives back content as-is if there is no require', () => {
184184
const content = ".someClass { color: #fff;}";
185185
expect(operator.mergeVarsToContent(content, context)).toEqual(content);
186186
});
@@ -191,6 +191,15 @@ describe('js-to-styles-vars-loader', () => {
191191
const merged = operator.mergeVarsToContent(content, {...context, context: path.resolve('./mocks/')}, 'less');
192192
expect(merged.trim()).toEqual(expectedContent.trim());
193193
})
194+
195+
it("imports nested props", () => {
196+
const content = fs.readFileSync(path.resolve('./mocks/case2.less'), 'utf8');
197+
const expectedContent = fs.readFileSync(path.resolve('./mocks/case2_expected.less'), 'utf8');
198+
const merged = operator.mergeVarsToContent(content, {...context, context: path.resolve('./mocks/')}, 'less');
199+
expect(merged.trim()).toEqual(expectedContent.trim());
200+
})
201+
202+
194203
});
195204

196205
describe('getResource', () => {

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const requireReg = /require\s*\((["'])([\w.\/]+)(?:\1)\)((?:\.[\w_-]+)*);?/igm;
66

77
const operator = {
88

9-
guardExportType (data, relativePath) {
10-
if (typeof data !== "object" || Array.isArray(data)) {
11-
throw new Error(`Value must be an object '${relativePath}'`)
9+
validateExportType (data, relativePath) {
10+
if (data === null || typeof data !== "object" || Array.isArray(data)) {
11+
throw new Error(`Value must be a flat object '${relativePath}'`)
1212
}
1313
},
1414

@@ -18,10 +18,10 @@ const operator = {
1818
if (!data) {
1919
throw new Error(`No data in '${relativePath}'`)
2020
}
21-
this.guardExportType(data, relativePath);
21+
this.validateExportType(data, relativePath);
2222
if (property) {
2323
const propVal = squba(data, property);
24-
this.guardExportType(propVal, relativePath);
24+
this.validateExportType(propVal, relativePath);
2525
return propVal;
2626
}
2727
return data;
@@ -55,10 +55,17 @@ const operator = {
5555
}
5656
},
5757

58+
propDeDot (strPropMatch) {
59+
if (!strPropMatch || strPropMatch[0] !== ".")
60+
return strPropMatch;
61+
else
62+
return strPropMatch.substr(1);
63+
},
64+
5865
mergeVarsToContent (content, webpackContext, preprocessorType) {
5966
const replacer = function (m,q, relativePath, property) {
6067
const modulePath = path.join(webpackContext.context, relativePath)
61-
const varData = this.getVarData(modulePath, property);
68+
const varData = this.getVarData(modulePath, this.propDeDot(property));
6269
webpackContext.addDependency(modulePath);
6370
return this.transformToStyleVars({
6471
type: preprocessorType,

mocks/case2.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
a: {
3+
foo: "a"
4+
},
5+
b: {
6+
c: {
7+
foo: "c"
8+
}
9+
}
10+
};

mocks/case2.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.a {
2+
require("./case2.js").a;
3+
}
4+
.c {
5+
require("./case2.js").b.c;
6+
}

mocks/case2_expected.less

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.a {
2+
@foo: a;
3+
4+
}
5+
.c {
6+
@foo: c;
7+
8+
}

0 commit comments

Comments
 (0)