Skip to content

Commit 4c61f0b

Browse files
authored
Merge pull request #8 from anc95/pass-config
pass config
2 parents 6efba9e + a0802a1 commit 4c61f0b

File tree

1 file changed

+87
-34
lines changed

1 file changed

+87
-34
lines changed

src/transformer.ts

+87-34
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ interface Config {
1717
* custom cache key
1818
* if not set, I will combine all transform's cahceKey.
1919
*/
20-
getCacheKey?: Transformer<Config>['getCacheKey'] | Transformer<Config>['getCacheKeyAsync'];
20+
getCacheKey?:
21+
| Transformer<Config>['getCacheKey']
22+
| Transformer<Config>['getCacheKeyAsync'];
2123
}
2224

23-
const requireTransformer = (path: string) => {
24-
const transformer = (require(path).default || require(path));
25+
const requireTransformer = (path: string, config: Record<string, any>) => {
26+
const transformer = require(path).default || require(path);
2527

2628
if (!transformer.process && !transformer.processAsync) {
2729
if (transformer.createTransformer) {
28-
return transformer.createTransformer();
30+
return transformer.createTransformer(config || {});
2931
}
3032

3133
return null;
3234
}
3335

3436
return transformer;
35-
}
37+
};
3638

3739
const flatTransformers = (transformers: Config['transformers']) => {
3840
const containers: any[] = [];
@@ -41,25 +43,24 @@ const flatTransformers = (transformers: Config['transformers']) => {
4143
let transformerModule;
4244

4345
if (typeof transformer === 'string') {
44-
transformerModule = requireTransformer(transformer);
46+
transformerModule = requireTransformer(transformer, {});
4547

4648
if (!transformerModule) {
4749
console.error(`cant load ${transformer} as a transformer, so skip it`);
4850
break;
4951
}
50-
}
51-
else if (Array.isArray(transformer)) {
52+
} else if (Array.isArray(transformer)) {
5253
transformerModule = {
53-
...requireTransformer(transformer[0]),
54-
transformerConfig: transformer[1]
54+
...requireTransformer(transformer[0], transformer[1]),
55+
transformerConfig: transformer[1],
5556
};
5657
}
5758

5859
containers.push(transformerModule);
5960
}
6061

6162
return containers;
62-
}
63+
};
6364

6465
const createTransformer = (): Transformer<Config> => {
6566
let flattenTransformers: any = null;
@@ -69,49 +70,101 @@ const createTransformer = (): Transformer<Config> => {
6970
return flattenTransformers;
7071
}
7172

72-
flattenTransformers = flatTransformers(options.transformerConfig.transformers);
73+
flattenTransformers = flatTransformers(
74+
options.transformerConfig.transformers
75+
);
7376

7477
return flattenTransformers;
75-
}
78+
};
7679

77-
const constructOptions = <T>(options: TransformOptions<Config>, config: T): TransformOptions<T> => {
80+
const constructOptions = <T>(
81+
options: TransformOptions<Config>,
82+
config: T
83+
): TransformOptions<T> => {
7884
return {
7985
...options,
80-
transformerConfig: config
86+
transformerConfig: config,
8187
};
82-
}
88+
};
8389

8490
return {
8591
canInstrument: true,
86-
getCacheKey: (sourceText: string, sourcePath: string, options: TransformOptions<Config>): string => {
92+
getCacheKey: (
93+
sourceText: string,
94+
sourcePath: string,
95+
options: TransformOptions<Config>
96+
): string => {
8797
const transformers = getFlattenTransformers(options);
8898

8999
return transformers.reduce((res: string, transformer: any) => {
90-
return res + transformer.getCacheKey?.(sourceText, sourcePath, constructOptions(options, transformer.transformerConfig)) || ''
100+
return (
101+
res +
102+
transformer.getCacheKey?.(
103+
sourceText,
104+
sourcePath,
105+
constructOptions(options, transformer.transformerConfig)
106+
) || ''
107+
);
91108
}, '');
92109
},
93-
process: (sourceText: string, sourcePath: string, options: TransformOptions<Config>): string => {
110+
process: (
111+
sourceText: string,
112+
sourcePath: string,
113+
options: TransformOptions<Config>
114+
): string => {
94115
const transformers = getFlattenTransformers(options);
95116

96-
return transformers.reduce((res: { code: string }, transformer: any) => {
97-
return transformer.process?.(res.code ? res.code : res, sourcePath, constructOptions(options, transformer.transformerConfig))
98-
}, { code: sourceText });
117+
return transformers.reduce(
118+
(res: { code: string }, transformer: any) => {
119+
return transformer.process?.(
120+
res.code ? res.code : res,
121+
sourcePath,
122+
constructOptions(options, transformer.transformerConfig)
123+
);
124+
},
125+
{ code: sourceText }
126+
);
99127
},
100-
getCacheKeyAsync: async (sourceText: string, sourcePath: string, options: TransformOptions<Config>) => {
128+
getCacheKeyAsync: async (
129+
sourceText: string,
130+
sourcePath: string,
131+
options: TransformOptions<Config>
132+
) => {
101133
const transformers = getFlattenTransformers(options);
102134

103-
return await transformers.reduce(async (res: string, transformer: any) => {
104-
return res + await transformer.getCacheKeyAsync?.(sourceText, sourcePath, constructOptions(options, transformer.transformerConfig)) || ''
105-
}, '');
135+
return await transformers.reduce(
136+
async (res: string, transformer: any) => {
137+
return (
138+
res +
139+
(await transformer.getCacheKeyAsync?.(
140+
sourceText,
141+
sourcePath,
142+
constructOptions(options, transformer.transformerConfig)
143+
)) || ''
144+
);
145+
},
146+
''
147+
);
106148
},
107-
processAsync: async (sourceText: string, sourcePath: string, options: TransformOptions<Config>) => {
149+
processAsync: async (
150+
sourceText: string,
151+
sourcePath: string,
152+
options: TransformOptions<Config>
153+
) => {
108154
const transformers = getFlattenTransformers(options);
109155

110-
return await transformers.reduce(async (res: { code: string }, transformer: any) => {
111-
return await transformer.process?.(res.code ? res.code : res, sourcePath, constructOptions(options, transformer.transformerConfig));
112-
}, { code: sourceText });
113-
}
114-
}
115-
}
156+
return await transformers.reduce(
157+
async (res: { code: string }, transformer: any) => {
158+
return await transformer.process?.(
159+
res.code ? res.code : res,
160+
sourcePath,
161+
constructOptions(options, transformer.transformerConfig)
162+
);
163+
},
164+
{ code: sourceText }
165+
);
166+
},
167+
};
168+
};
116169

117-
export default createTransformer();
170+
export default createTransformer();

0 commit comments

Comments
 (0)