Skip to content

Commit 94b27fd

Browse files
authored
Add instructions to use with forked processes
- awesome-typescript-loader - ts-loader with HappyPack or thread-loader
1 parent 964408e commit 94b27fd

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ In the `webpack.config.js` file in the section where **awesome-typescript-loader
3131

3232
```js
3333
// 1. import default from the plugin module
34-
var createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
34+
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
3535

3636
// 2. create a transformer;
3737
// the factory additionally accepts an options object which described below
38-
var styledComponentsTransformer = createStyledComponentsTransformer();
38+
const styledComponentsTransformer = createStyledComponentsTransformer();
3939

4040
// 3. add getCustomTransformer method to the loader config
4141
var config = {
@@ -56,17 +56,20 @@ var config = {
5656
};
5757
```
5858

59+
Please note, that in the development mode, `awesome-typescript-loader` uses multiple separate processes to speed up compilation. In that mode the above configuration cannot work because functions, which `getCustomTransformers` is, are not transferrable between processes.
60+
To solve this please refer to [Forked process configuration](#forked-process-configuration) section.
61+
5962
## `ts-loader`
6063

6164
In the `webpack.config.js` file in the section where **ts-loader** is configured as a loader:
6265

6366
```js
6467
// 1. import default from the plugin module
65-
var createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
68+
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
6669

6770
// 2. create a transformer;
6871
// the factory additionally accepts an options object which described below
69-
var styledComponentsTransformer = createStyledComponentsTransformer();
72+
const styledComponentsTransformer = createStyledComponentsTransformer();
7073

7174
// 3. add getCustomTransformer method to the loader config
7275
var config = {
@@ -87,6 +90,44 @@ var config = {
8790
};
8891
```
8992

93+
Please note, when `awesome-typescript-loader` is used with `HappyPack` or `thread-loader`, they use multiple separate processes to speed up compilation. In that mode the above configuration cannot work because functions, which `getCustomTransformers` is, are not transferrable between processes.
94+
To solve this please refer to [Forked process configuration](#forked-process-configuration) section.
95+
96+
## Forked process configuration
97+
98+
To configure the transformer for development mode in `awesome-typescript-loader` or `ts-loader` with `HappyPack` or `thread-loader`, you need to make `getCustomTransformers` a resolvoble module name instead of the function. Since the configuration is very similar, here's an example:
99+
100+
### 1. Move `styledComponentsTransformer` into a separate file
101+
102+
Let's assume it is in the same folder as your `webpack.config` and has name `webpack.ts-transformers.js`:
103+
```js
104+
// 1. import default from the plugin module
105+
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
106+
107+
// 2. create a transformer;
108+
// the factory additionally accepts an options object which described below
109+
const styledComponentsTransformer = createStyledComponentsTransformer();
110+
111+
// 3. create getCustomTransformer function
112+
const getCustomTransformers: () => ({ before: [styledComponentsTransformer] });
113+
114+
// 4. export getCustomTransformers
115+
module.exports = getCustomTransformers;
116+
```
117+
118+
### 2. Change the loader's options to point to that file instead of explicit function
119+
120+
```diff
121+
-const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
122+
-const styledComponentsTransformer = createStyledComponentsTransformer();
123+
124+
options: {
125+
... // other loader's options
126+
- getCustomTransformers: () => ({ before: [styledComponentsTransformer] })
127+
+ getCustomTransformers: path.join(__dirname, './webpack.ts-transformers.js')
128+
}
129+
```
130+
90131
# API
91132

92133
## `createTransformer`

0 commit comments

Comments
 (0)