1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loadPrivatePartialConfig;
|
7 | exports.loadPartialConfig = loadPartialConfig;
|
8 | function _path() {
|
9 | const data = require("path");
|
10 | _path = function () {
|
11 | return data;
|
12 | };
|
13 | return data;
|
14 | }
|
15 | var _plugin = require("./plugin.js");
|
16 | var _util = require("./util.js");
|
17 | var _item = require("./item.js");
|
18 | var _configChain = require("./config-chain.js");
|
19 | var _environment = require("./helpers/environment.js");
|
20 | var _options = require("./validation/options.js");
|
21 | var _index = require("./files/index.js");
|
22 | var _resolveTargets = require("./resolve-targets.js");
|
23 | const _excluded = ["showIgnoredFiles"];
|
24 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
25 | function resolveRootMode(rootDir, rootMode) {
|
26 | switch (rootMode) {
|
27 | case "root":
|
28 | return rootDir;
|
29 | case "upward-optional":
|
30 | {
|
31 | const upwardRootDir = (0, _index.findConfigUpwards)(rootDir);
|
32 | return upwardRootDir === null ? rootDir : upwardRootDir;
|
33 | }
|
34 | case "upward":
|
35 | {
|
36 | const upwardRootDir = (0, _index.findConfigUpwards)(rootDir);
|
37 | if (upwardRootDir !== null) return upwardRootDir;
|
38 | throw Object.assign(new Error(`Babel was run with rootMode:"upward" but a root could not ` + `be found when searching upward from "${rootDir}".\n` + `One of the following config files must be in the directory tree: ` + `"${_index.ROOT_CONFIG_FILENAMES.join(", ")}".`), {
|
39 | code: "BABEL_ROOT_NOT_FOUND",
|
40 | dirname: rootDir
|
41 | });
|
42 | }
|
43 | default:
|
44 | throw new Error(`Assertion failure - unknown rootMode value.`);
|
45 | }
|
46 | }
|
47 | function* loadPrivatePartialConfig(inputOpts) {
|
48 | if (inputOpts != null && (typeof inputOpts !== "object" || Array.isArray(inputOpts))) {
|
49 | throw new Error("Babel options must be an object, null, or undefined");
|
50 | }
|
51 | const args = inputOpts ? (0, _options.validate)("arguments", inputOpts) : {};
|
52 | const {
|
53 | envName = (0, _environment.getEnv)(),
|
54 | cwd = ".",
|
55 | root: rootDir = ".",
|
56 | rootMode = "root",
|
57 | caller,
|
58 | cloneInputAst = true
|
59 | } = args;
|
60 | const absoluteCwd = _path().resolve(cwd);
|
61 | const absoluteRootDir = resolveRootMode(_path().resolve(absoluteCwd, rootDir), rootMode);
|
62 | const filename = typeof args.filename === "string" ? _path().resolve(cwd, args.filename) : undefined;
|
63 | const showConfigPath = yield* (0, _index.resolveShowConfigPath)(absoluteCwd);
|
64 | const context = {
|
65 | filename,
|
66 | cwd: absoluteCwd,
|
67 | root: absoluteRootDir,
|
68 | envName,
|
69 | caller,
|
70 | showConfig: showConfigPath === filename
|
71 | };
|
72 | const configChain = yield* (0, _configChain.buildRootChain)(args, context);
|
73 | if (!configChain) return null;
|
74 | const merged = {
|
75 | assumptions: {}
|
76 | };
|
77 | configChain.options.forEach(opts => {
|
78 | (0, _util.mergeOptions)(merged, opts);
|
79 | });
|
80 | const options = Object.assign({}, merged, {
|
81 | targets: (0, _resolveTargets.resolveTargets)(merged, absoluteRootDir),
|
82 | cloneInputAst,
|
83 | babelrc: false,
|
84 | configFile: false,
|
85 | browserslistConfigFile: false,
|
86 | passPerPreset: false,
|
87 | envName: context.envName,
|
88 | cwd: context.cwd,
|
89 | root: context.root,
|
90 | rootMode: "root",
|
91 | filename: typeof context.filename === "string" ? context.filename : undefined,
|
92 | plugins: configChain.plugins.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor)),
|
93 | presets: configChain.presets.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor))
|
94 | });
|
95 | return {
|
96 | options,
|
97 | context,
|
98 | fileHandling: configChain.fileHandling,
|
99 | ignore: configChain.ignore,
|
100 | babelrc: configChain.babelrc,
|
101 | config: configChain.config,
|
102 | files: configChain.files
|
103 | };
|
104 | }
|
105 | function* loadPartialConfig(opts) {
|
106 | let showIgnoredFiles = false;
|
107 | if (typeof opts === "object" && opts !== null && !Array.isArray(opts)) {
|
108 | var _opts = opts;
|
109 | ({
|
110 | showIgnoredFiles
|
111 | } = _opts);
|
112 | opts = _objectWithoutPropertiesLoose(_opts, _excluded);
|
113 | _opts;
|
114 | }
|
115 | const result = yield* loadPrivatePartialConfig(opts);
|
116 | if (!result) return null;
|
117 | const {
|
118 | options,
|
119 | babelrc,
|
120 | ignore,
|
121 | config,
|
122 | fileHandling,
|
123 | files
|
124 | } = result;
|
125 | if (fileHandling === "ignored" && !showIgnoredFiles) {
|
126 | return null;
|
127 | }
|
128 | (options.plugins || []).forEach(item => {
|
129 | if (item.value instanceof _plugin.default) {
|
130 | throw new Error("Passing cached plugin instances is not supported in " + "babel.loadPartialConfig()");
|
131 | }
|
132 | });
|
133 | return new PartialConfig(options, babelrc ? babelrc.filepath : undefined, ignore ? ignore.filepath : undefined, config ? config.filepath : undefined, fileHandling, files);
|
134 | }
|
135 | class PartialConfig {
|
136 | constructor(options, babelrc, ignore, config, fileHandling, files) {
|
137 | this.options = void 0;
|
138 | this.babelrc = void 0;
|
139 | this.babelignore = void 0;
|
140 | this.config = void 0;
|
141 | this.fileHandling = void 0;
|
142 | this.files = void 0;
|
143 | this.options = options;
|
144 | this.babelignore = ignore;
|
145 | this.babelrc = babelrc;
|
146 | this.config = config;
|
147 | this.fileHandling = fileHandling;
|
148 | this.files = files;
|
149 | Object.freeze(this);
|
150 | }
|
151 | hasFilesystemConfig() {
|
152 | return this.babelrc !== undefined || this.config !== undefined;
|
153 | }
|
154 | }
|
155 | Object.freeze(PartialConfig.prototype);
|
156 | 0 && 0;
|
157 |
|
158 |
|