-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptions.ts
76 lines (62 loc) · 2.35 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { TransformOptions } from "./transforms/transforms"
import { Transformer } from "./utils/visitor-wrapper"
import { allExtractors } from "./extractor/extractor"
import { ModuleId } from "./extractor/module"
import { Options as generatorOptions } from "./generator/options"
export interface Options {
/**
* bundle type
* @default "webpack"
*/
type?: keyof typeof allExtractors;
/** output files, otherwise only return "retidied" codes */
writeFiles?: boolean;
/** @default "retidy-out" */
outDir?: string;
/**
* for the compatibility of module solving
* @default "javascript"
* @see https://www.typescriptlang.org/docs/handbook/modules.html
*/
outputFileType?: "javascript" | "typescript";
transformerOptions?: TransformOptions;
/**
* extra transformers that run before any other transformers
* some extractors may add them by editing the options
*/
extraTransformers?: Transformer[];
generatorOptions?: generatorOptions;
/**
* webpack only
* a reference to the bundle ast (to the webpackBootstrap function call ast)
* @default ["body", 0, "expression", "argument"]
*/
bundleAstReferenceKeys?: (string | number)[];
// override
entryPoint?: ModuleId;
/**
* webpack only
* Transform variables defined in ModuleFunction params to their real values
* @see https://github.com/Xmader/retidy/blob/master/src/extractor/webpack/transformer.ts#L6
*/
replaceModuleFunctionParams?: boolean;
}
export const defaultOptions: Options = {
type: "webpack",
writeFiles: true,
outDir: "./retidy-out/",
outputFileType: "javascript",
bundleAstReferenceKeys: [],
replaceModuleFunctionParams: true,
}
export const defaultAstRefs = {
"webpack": ["body", 0, "expression", "argument"] as const, // AST: `function(modules){…`
"webpack-jsonp": ["body", 0, "expression"] as const, // AST: `(window.webpackJsonp = window.webpackJsonp || []).push(…` or simply `webpackJsonp.push(…`
}
export const normalizeOptions = (options?: Options): Options => {
options = Object.assign({}, defaultOptions, options)
if (!options.bundleAstReferenceKeys || !options.bundleAstReferenceKeys.length) {
options.bundleAstReferenceKeys = defaultAstRefs[options.type] || []
}
return options
}