Skip to content

Commit

Permalink
fix(#1): antd.css less transform throw
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Apr 28, 2020
1 parent 9631a0c commit 21e631c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Update your jest configure, and add it into transform.

```diff
+ "transform": {
+ ".+\\.less$": "jest-less-loader"
+ "\\.(less|css)$": "jest-less-loader"
+ },
```

Expand Down
9 changes: 9 additions & 0 deletions __tests__/css.css

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions __tests__/electron-css.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-ignore
import transformer from '../src';
import './css.css';

describe('jest-less-loader', () => {
test('import css', () => {
expect(document.querySelectorAll('style')[0].innerHTML).toContain('antd');
});
});
2 changes: 1 addition & 1 deletion __tests__/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ describe('jest-less-loader', () => {
}
`;

expect(() => transformer.process(less)).toThrow();
expect(() => transformer.process(less)).not.toThrow();
});
});
5 changes: 3 additions & 2 deletions __tests__/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('jest-less-loader', () => {
}
`;

expect(transformer.process(less).code).toEqual('');
expect(transformer.process(less).code).toBe('');
});

test('transformer less syntax', () => {
Expand All @@ -24,6 +24,7 @@ describe('jest-less-loader', () => {
}
`;

expect(() => transformer.process(less)).toThrow();
// only warn, not throw
expect(() => transformer.process(less)).not.toThrow();
});
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-less-loader",
"version": "0.1.0",
"version": "0.1.1",
"description": "Jest transformer for .less file.",
"license": "MIT",
"main": "lib/index.js",
Expand All @@ -11,7 +11,7 @@
"clean": "rimraf -rf lib",
"lint": "eslint src/**/*.ts __tests__/**/*.ts",
"lint-staged": "lint-staged",
"test": "jest",
"test": "jest --no-cache",
"ci": "npm run lint && npm run test && lint-md .",
"start": "tsc -w",
"build": "rimraf ./lib && tsc --module commonjs --outDir lib",
Expand Down Expand Up @@ -55,7 +55,7 @@
"src/**/*.ts"
],
"transform": {
".+\\.less$": "./lib/index.js"
"\\.(less|css)$": "./lib/index.js"
}
},
"lint-staged": {
Expand Down
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ function injectStyle(css: string): void {
* @type {{process(*=): *}}
*/
module.exports = {
process(fileContent): object {
const css = transformLess(fileContent);

// when running in nodejs env, will throw error.
process(fileContent: string, filePath: string): object {
let css = fileContent;
try {
injectStyle(css);
// if .less, transform, if .css, keep it
css = filePath.endsWith('.less')
? transformLess(fileContent)
: filePath.endsWith('.css')
? fileContent
: fileContent;
} catch (e) {
// do nothing
// if throw, use file content
css = fileContent;
console.warn('jest-less-loader: process the file error.', { fileContent, filePath });
} finally {
// when running in nodejs env, will throw error.
try {
injectStyle(css);
} catch (e) {
// do nothing
}
}

// no code for js module
Expand Down

0 comments on commit 21e631c

Please sign in to comment.