|
1 | 1 | # typescript-transform-unspec
|
2 |
| -Typescript transform plugin |
| 2 | +Typescript transform plugin removes spec definition from source file. |
| 3 | +Inspired by [unassert](https://github.com/unassert-js/unassert). |
| 4 | + |
| 5 | +## Motivation |
| 6 | +Imagine we have `function.ts` with single function. Usually we create `function.spec.ts` with tests. |
| 7 | +But what if we can keep tests in same file, and remove spec defenition for production. |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +### Before |
| 12 | +```ts |
| 13 | +export function hello(greet = 'world') { |
| 14 | + return `hello ${greet}`; |
| 15 | +} |
| 16 | + |
| 17 | +it('hello world test', () => { |
| 18 | + expect(hello()).toBe('hello world'); |
| 19 | +}); |
| 20 | +``` |
| 21 | + |
| 22 | +### After |
| 23 | +```js |
| 24 | +function hello(greet) { |
| 25 | + if (greet === void 0) { greet = 'world'; } |
| 26 | + return "hello " + greet; |
| 27 | +} |
| 28 | +```` |
| 29 | + |
| 30 | +## Installation |
| 31 | +```sh |
| 32 | +npm install --save-dev typescript-transform-unspec |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +#### webpack (with ts-loader or awesome-typescript-loader) |
| 38 | +```js |
| 39 | +// webpack.config.js |
| 40 | +const unspecTransformer = require('typescript-transform-unspec'); |
| 41 | +
|
| 42 | +rules: [ |
| 43 | + { |
| 44 | + test: /\.tsx?$/, |
| 45 | + loader: 'ts-loader', // or 'awesome-typescript-loader' |
| 46 | + options: { |
| 47 | + getCustomTransformers: program => ({ |
| 48 | + before: [ |
| 49 | + unspecTransformer(program), |
| 50 | + ] |
| 51 | + }) |
| 52 | + } |
| 53 | + }, |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | +#### TTypescript |
| 58 | +```json |
| 59 | +// tsconfig.json |
| 60 | +{ |
| 61 | + "compilerOptions": { |
| 62 | + "plugins": [ |
| 63 | + { "transform": "typescript-transform-unspec" }, |
| 64 | + ] |
| 65 | + }, |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +#### Rollup (with rollup-plugin-typescript2) |
| 70 | +```js |
| 71 | +// rollup.config.js |
| 72 | +import typescript from 'rollup-plugin-typescript2'; |
| 73 | +import unspecTransformer from 'typescript-transform-unspec'; |
| 74 | +
|
| 75 | +plugins: [ |
| 76 | + typescript({ |
| 77 | + transformers: [ |
| 78 | + service => ({ |
| 79 | + before: [unspecTransformer(service.getProgram())], |
| 80 | + after: [], |
| 81 | + }), |
| 82 | + ], |
| 83 | + }), |
| 84 | +] |
| 85 | +``` |
3 | 86 |
|
4 | 87 | ## Resources
|
5 | 88 | - https://dev.doctorevidence.com/how-to-write-a-typescript-transform-plugin-fc5308fdd943
|
|
0 commit comments