1 | import builtinModules from "builtin-modules";
|
2 | import readPkg from "read-pkg";
|
3 | import resolvePath from "./resolve";
|
4 |
|
5 | import { log } from "./console";
|
6 | import { error, invalidOption, invalidPkgField } from "./errors";
|
7 | import {
|
8 | AnalizedPkg,
|
9 | BrowserOptions,
|
10 | BundlibDependencies,
|
11 | BundlibOutputFiles,
|
12 | BundlibPkgJson,
|
13 | BundlibPkgOptions,
|
14 | MinifyOutOptions,
|
15 | PkgJsonModuleOutputFields,
|
16 | } from "./pkg";
|
17 | import { isArray, isNull, isObject, isString } from "./type-check";
|
18 | import { BrowserBuildFormat } from "./types";
|
19 | import { isBrowserFormat } from "./validate-fmt";
|
20 | import { isValidMinOption } from "./validate-min";
|
21 |
|
22 | const analizePkg = async (cwd: string, pkg?: BundlibPkgJson): Promise<AnalizedPkg> => {
|
23 |
|
24 | const resolvedPkg: BundlibPkgJson = pkg || await readPkg({ cwd });
|
25 |
|
26 | const {
|
27 | name: pkgName,
|
28 | main: cjsModuleFile,
|
29 | module: esModuleFile,
|
30 | browser: browserFile,
|
31 | types: pkgTypes,
|
32 | typings,
|
33 | dependencies: runtimeDependencies,
|
34 | peerDependencies,
|
35 | bundledDependencies,
|
36 | bundleDependencies,
|
37 | bundlib: bundlibOptions,
|
38 | } = resolvedPkg;
|
39 |
|
40 | if (browserFile && !isString(browserFile)) {
|
41 | throw invalidPkgField("browser");
|
42 | }
|
43 |
|
44 | if (!isNull(bundlibOptions) && (!isObject(bundlibOptions) || isArray(bundlibOptions))) {
|
45 | throw invalidPkgField("bundlib");
|
46 | }
|
47 |
|
48 | const {
|
49 | input: pkgInput,
|
50 | sourcemap: sourcemapFlag,
|
51 | esModule: esModuleFlag,
|
52 | interop: interopFlag,
|
53 | extend: extendFlag,
|
54 | equals: equalsFlag,
|
55 | browser: pkgBrowserFormat,
|
56 | name: browserName,
|
57 | id: amdId,
|
58 | globals: browserGlobals,
|
59 | iife,
|
60 | amd,
|
61 | umd,
|
62 | min,
|
63 | } = (bundlibOptions || {}) as BundlibPkgOptions;
|
64 |
|
65 | if (!isNull(pkgInput) && !isString(pkgInput)) {
|
66 | throw invalidOption("input");
|
67 | }
|
68 |
|
69 | if (!isNull(pkgBrowserFormat) && !isBrowserFormat(pkgBrowserFormat)) {
|
70 | throw invalidOption("browser");
|
71 | }
|
72 |
|
73 | if (!isNull(browserName) && !isString(browserName)) {
|
74 | throw invalidOption("name");
|
75 | }
|
76 |
|
77 | if (!isNull(amdId) && !isString(amdId)) {
|
78 | throw invalidOption("id");
|
79 | }
|
80 |
|
81 | if (!isNull(browserGlobals) && !isObject(browserGlobals)) {
|
82 | throw invalidOption("globals");
|
83 | }
|
84 |
|
85 | if (!isNull(min) && !isValidMinOption(min)) {
|
86 | throw invalidOption("min");
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 | if ((iife && amd) || (iife && umd) || (amd && umd)) {
|
92 | throw error("multiple browser builds are no longer supported in bundlib >= 0.3.");
|
93 | }
|
94 |
|
95 | if (iife || amd || umd) {
|
96 |
|
97 | log("options iife, amd & umd are deprecated in version >= 0.3");
|
98 | }
|
99 |
|
100 | const deprecatedBrowserFormat: BrowserBuildFormat | null = iife ? "iife" : amd ? "amd" : null;
|
101 |
|
102 |
|
103 |
|
104 | const browserFormat: BrowserBuildFormat = pkgBrowserFormat || deprecatedBrowserFormat || "umd";
|
105 |
|
106 |
|
107 |
|
108 | const input = resolvePath(
|
109 | pkgInput || "src/index.ts",
|
110 | cwd,
|
111 | );
|
112 |
|
113 | const typesPath = pkgTypes || typings;
|
114 |
|
115 | const output: BundlibOutputFiles = {
|
116 | main: cjsModuleFile ? resolvePath(cjsModuleFile, cwd) : null,
|
117 | module: esModuleFile ? resolvePath(esModuleFile, cwd) : null,
|
118 | browser: browserFile ? resolvePath(browserFile, cwd) : null,
|
119 | types: typesPath ? resolvePath(typesPath, cwd) : null,
|
120 | };
|
121 |
|
122 | const dependencies: BundlibDependencies = {
|
123 | builtin: builtinModules,
|
124 | runtime: runtimeDependencies ? Object.keys(runtimeDependencies) : [],
|
125 | peer: peerDependencies ? Object.keys(peerDependencies) : [],
|
126 | bundled: bundledDependencies || bundleDependencies || [],
|
127 | };
|
128 |
|
129 | const buildName = browserName || pkgName || null;
|
130 |
|
131 | const minify: MinifyOutOptions = !min
|
132 | ? {}
|
133 | : min as unknown as boolean === true
|
134 | ? { main: true, module: true, browser: true }
|
135 | : isArray(min)
|
136 | ? min.reduce((result, value) => {
|
137 | result[value] = true;
|
138 | return result;
|
139 | }, {} as MinifyOutOptions)
|
140 | : {
|
141 | [min as PkgJsonModuleOutputFields]: true,
|
142 | };
|
143 |
|
144 | const globals = !browserGlobals
|
145 | ? null
|
146 | : isArray(browserGlobals)
|
147 | ? browserGlobals.reduce<Record<string, string>>((result, value) => {
|
148 | if (isString(value)) {
|
149 | result[value] = value;
|
150 | }
|
151 | return result;
|
152 | }, {})
|
153 | : browserGlobals as Record<string, string>;
|
154 |
|
155 | const browser: BrowserOptions = {
|
156 | format: browserFormat,
|
157 | name: buildName,
|
158 | id: amdId || null,
|
159 | globals,
|
160 | };
|
161 |
|
162 | const options = {
|
163 | sourcemap: sourcemapFlag !== false,
|
164 | esModule: !!esModuleFlag,
|
165 | interop: !!interopFlag,
|
166 | extend: !!extendFlag,
|
167 | equals: !!equalsFlag,
|
168 | };
|
169 |
|
170 | return { cwd, pkg: resolvedPkg, dependencies, input, output, minify, browser, options };
|
171 |
|
172 | };
|
173 |
|
174 | export default analizePkg;
|
175 |
|
\ | No newline at end of file |