@utoo/pack 0.0.1-alpha.2 → 0.0.1-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/binding.d.ts +11 -9
- package/cjs/build.d.ts +4 -2
- package/cjs/build.js +18 -11
- package/cjs/dev.d.ts +44 -0
- package/cjs/dev.js +362 -0
- package/cjs/{hotReloader.d.ts → hmr.d.ts} +4 -11
- package/cjs/{hotReloader.js → hmr.js} +14 -61
- package/cjs/index.d.ts +13 -14
- package/cjs/index.js +6 -4
- package/cjs/mkcert.d.ts +7 -0
- package/cjs/mkcert.js +183 -0
- package/cjs/project.d.ts +4 -4
- package/cjs/project.js +0 -2
- package/cjs/types.d.ts +22 -9
- package/cjs/util.d.ts +1 -1
- package/cjs/util.js +1 -1
- package/cjs/webpackCompat.d.ts +6 -0
- package/cjs/webpackCompat.js +341 -0
- package/config_schema.json +30 -2
- package/esm/binding.d.ts +11 -9
- package/esm/build.d.ts +4 -2
- package/esm/build.js +18 -11
- package/esm/dev.d.ts +44 -0
- package/esm/dev.js +346 -0
- package/esm/{hotReloader.d.ts → hmr.d.ts} +4 -11
- package/esm/{hotReloader.js → hmr.js} +14 -60
- package/esm/index.d.ts +13 -14
- package/esm/index.js +6 -2
- package/esm/mkcert.d.ts +7 -0
- package/esm/mkcert.js +176 -0
- package/esm/project.d.ts +4 -4
- package/esm/project.js +0 -2
- package/esm/types.d.ts +22 -9
- package/esm/util.d.ts +1 -1
- package/esm/util.js +1 -1
- package/esm/webpackCompat.d.ts +6 -0
- package/esm/webpackCompat.js +338 -0
- package/package.json +17 -11
- package/cjs/sourceMap.d.ts +0 -21
- package/cjs/sourceMap.js +0 -2
- package/cjs/watch.d.ts +0 -2
- package/cjs/watch.js +0 -14
- package/esm/sourceMap.d.ts +0 -21
- package/esm/sourceMap.js +0 -1
- package/esm/watch.d.ts +0 -2
- package/esm/watch.js +0 -11
package/esm/mkcert.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
import { createPrivateKey, X509Certificate } from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import os from "os";
|
|
6
|
+
const { WritableStream } = require("node:stream/web");
|
|
7
|
+
const MKCERT_VERSION = "v1.4.4";
|
|
8
|
+
function getBinaryName() {
|
|
9
|
+
const platform = process.platform;
|
|
10
|
+
const arch = process.arch === "x64" ? "amd64" : process.arch;
|
|
11
|
+
if (platform === "win32") {
|
|
12
|
+
return `mkcert-${MKCERT_VERSION}-windows-${arch}.exe`;
|
|
13
|
+
}
|
|
14
|
+
if (platform === "darwin") {
|
|
15
|
+
return `mkcert-${MKCERT_VERSION}-darwin-${arch}`;
|
|
16
|
+
}
|
|
17
|
+
if (platform === "linux") {
|
|
18
|
+
return `mkcert-${MKCERT_VERSION}-linux-${arch}`;
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`Unsupported platform: ${platform}`);
|
|
21
|
+
}
|
|
22
|
+
async function downloadBinary() {
|
|
23
|
+
try {
|
|
24
|
+
const binaryName = getBinaryName();
|
|
25
|
+
const cacheDirectory = getCacheDirectory("mkcert");
|
|
26
|
+
const binaryPath = path.join(cacheDirectory, binaryName);
|
|
27
|
+
if (fs.existsSync(binaryPath)) {
|
|
28
|
+
return binaryPath;
|
|
29
|
+
}
|
|
30
|
+
const downloadUrl = `https://github.com/FiloSottile/mkcert/releases/download/${MKCERT_VERSION}/${binaryName}`;
|
|
31
|
+
await fs.promises.mkdir(cacheDirectory, { recursive: true });
|
|
32
|
+
console.log(`Downloading mkcert package...`);
|
|
33
|
+
const response = await fetch(downloadUrl);
|
|
34
|
+
if (!response.ok || !response.body) {
|
|
35
|
+
throw new Error(`request failed with status ${response.status}`);
|
|
36
|
+
}
|
|
37
|
+
console.log(`Download response was successful, writing to disk`);
|
|
38
|
+
const binaryWriteStream = fs.createWriteStream(binaryPath);
|
|
39
|
+
await response.body.pipeTo(new WritableStream({
|
|
40
|
+
write(chunk) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
binaryWriteStream.write(chunk, (error) => {
|
|
43
|
+
if (error) {
|
|
44
|
+
reject(error);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
resolve();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
close() {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
binaryWriteStream.close((error) => {
|
|
54
|
+
if (error) {
|
|
55
|
+
reject(error);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
resolve();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
}));
|
|
63
|
+
await fs.promises.chmod(binaryPath, 0o755);
|
|
64
|
+
return binaryPath;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
console.error("Error downloading mkcert:", err);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export async function createSelfSignedCertificate(host, certDir = "certificates") {
|
|
71
|
+
try {
|
|
72
|
+
const binaryPath = await downloadBinary();
|
|
73
|
+
if (!binaryPath)
|
|
74
|
+
throw new Error("missing mkcert binary");
|
|
75
|
+
const resolvedCertDir = path.resolve(process.cwd(), `./${certDir}`);
|
|
76
|
+
await fs.promises.mkdir(resolvedCertDir, {
|
|
77
|
+
recursive: true,
|
|
78
|
+
});
|
|
79
|
+
const keyPath = path.resolve(resolvedCertDir, "localhost-key.pem");
|
|
80
|
+
const certPath = path.resolve(resolvedCertDir, "localhost.pem");
|
|
81
|
+
if (fs.existsSync(keyPath) && fs.existsSync(certPath)) {
|
|
82
|
+
const cert = new X509Certificate(fs.readFileSync(certPath));
|
|
83
|
+
const key = fs.readFileSync(keyPath);
|
|
84
|
+
if (cert.checkHost(host !== null && host !== void 0 ? host : "localhost") &&
|
|
85
|
+
cert.checkPrivateKey(createPrivateKey(key))) {
|
|
86
|
+
console.log("Using already generated self signed certificate");
|
|
87
|
+
const caLocation = execSync(`"${binaryPath}" -CAROOT`)
|
|
88
|
+
.toString()
|
|
89
|
+
.trim();
|
|
90
|
+
return {
|
|
91
|
+
key: keyPath,
|
|
92
|
+
cert: certPath,
|
|
93
|
+
rootCA: `${caLocation}/rootCA.pem`,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
console.log("Attempting to generate self signed certificate. This may prompt for your password");
|
|
98
|
+
const defaultHosts = ["localhost", "127.0.0.1", "::1"];
|
|
99
|
+
const hosts = host && !defaultHosts.includes(host)
|
|
100
|
+
? [...defaultHosts, host]
|
|
101
|
+
: defaultHosts;
|
|
102
|
+
execSync(`"${binaryPath}" -install -key-file "${keyPath}" -cert-file "${certPath}" ${hosts.join(" ")}`, { stdio: "ignore" });
|
|
103
|
+
const caLocation = execSync(`"${binaryPath}" -CAROOT`).toString().trim();
|
|
104
|
+
if (!fs.existsSync(keyPath) || !fs.existsSync(certPath)) {
|
|
105
|
+
throw new Error("Certificate files not found");
|
|
106
|
+
}
|
|
107
|
+
console.log(`CA Root certificate created in ${caLocation}`);
|
|
108
|
+
console.log(`Certificates created in ${resolvedCertDir}`);
|
|
109
|
+
const gitignorePath = path.resolve(process.cwd(), "./.gitignore");
|
|
110
|
+
if (fs.existsSync(gitignorePath)) {
|
|
111
|
+
const gitignore = await fs.promises.readFile(gitignorePath, "utf8");
|
|
112
|
+
if (!gitignore.includes(certDir)) {
|
|
113
|
+
console.log("Adding certificates to .gitignore");
|
|
114
|
+
await fs.promises.appendFile(gitignorePath, `\n${certDir}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
key: keyPath,
|
|
119
|
+
cert: certPath,
|
|
120
|
+
rootCA: `${caLocation}/rootCA.pem`,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
console.error("Failed to generate self-signed certificate. Falling back to http.", err);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// get platform specific cache directory adapted from playwright's handling
|
|
128
|
+
// https://github.com/microsoft/playwright/blob/7d924470d397975a74a19184c136b3573a974e13/packages/playwright-core/src/utils/registry.ts#L141
|
|
129
|
+
export function getCacheDirectory(fileDirectory, envPath) {
|
|
130
|
+
let result;
|
|
131
|
+
if (envPath) {
|
|
132
|
+
result = envPath;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
let systemCacheDirectory;
|
|
136
|
+
if (process.platform === "linux") {
|
|
137
|
+
systemCacheDirectory =
|
|
138
|
+
process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
|
|
139
|
+
}
|
|
140
|
+
else if (process.platform === "darwin") {
|
|
141
|
+
systemCacheDirectory = path.join(os.homedir(), "Library", "Caches");
|
|
142
|
+
}
|
|
143
|
+
else if (process.platform === "win32") {
|
|
144
|
+
systemCacheDirectory =
|
|
145
|
+
process.env.LOCALAPPDATA || path.join(os.homedir(), "AppData", "Local");
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
/// Attempt to use generic tmp location for un-handled platform
|
|
149
|
+
if (!systemCacheDirectory) {
|
|
150
|
+
for (const dir of [
|
|
151
|
+
path.join(os.homedir(), ".cache"),
|
|
152
|
+
path.join(os.tmpdir()),
|
|
153
|
+
]) {
|
|
154
|
+
if (fs.existsSync(dir)) {
|
|
155
|
+
systemCacheDirectory = dir;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (!systemCacheDirectory) {
|
|
161
|
+
console.error(new Error("Unsupported platform: " + process.platform));
|
|
162
|
+
process.exit(0);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
result = path.join(systemCacheDirectory, fileDirectory);
|
|
166
|
+
}
|
|
167
|
+
if (!path.isAbsolute(result)) {
|
|
168
|
+
// It is important to resolve to the absolute path:
|
|
169
|
+
// - for unzipping to work correctly;
|
|
170
|
+
// - so that registry directory matches between installation and execution.
|
|
171
|
+
// INIT_CWD points to the root of `npm/yarn install` and is probably what
|
|
172
|
+
// the user meant when typing the relative path.
|
|
173
|
+
result = path.resolve(process.env["INIT_CWD"] || process.cwd(), result);
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
package/esm/project.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import type { HmrIdentifiers, NapiUpdateMessage, NapiWrittenEndpoint, StackFrame } from "./binding";
|
|
1
2
|
import * as binding from "./binding";
|
|
2
|
-
import type { NapiWrittenEndpoint, StackFrame } from "./binding";
|
|
3
3
|
import { ProjectOptions, RawEntrypoints, Update } from "./types";
|
|
4
4
|
export declare class TurbopackInternalError extends Error {
|
|
5
5
|
name: string;
|
|
6
6
|
constructor(cause: Error);
|
|
7
7
|
}
|
|
8
|
-
export declare function projectFactory(): (options: ProjectOptions
|
|
8
|
+
export declare function projectFactory(): (options: Required<ProjectOptions>, turboEngineOptions: binding.NapiTurboEngineOptions) => Promise<{
|
|
9
9
|
readonly _nativeProject: {
|
|
10
10
|
__napiType: "Project";
|
|
11
11
|
};
|
|
@@ -32,12 +32,12 @@ export declare function projectFactory(): (options: ProjectOptions, turboEngineO
|
|
|
32
32
|
diagnostics: binding.NapiDiagnostic[];
|
|
33
33
|
}, void, unknown>;
|
|
34
34
|
hmrEvents(identifier: string): AsyncIterableIterator<TurbopackResult<Update>>;
|
|
35
|
-
hmrIdentifiersSubscribe(): AsyncIterableIterator<TurbopackResult<
|
|
35
|
+
hmrIdentifiersSubscribe(): AsyncIterableIterator<TurbopackResult<HmrIdentifiers>>;
|
|
36
36
|
traceSource(stackFrame: StackFrame, currentDirectoryFileUrl: string): Promise<StackFrame | null>;
|
|
37
37
|
getSourceForAsset(filePath: string): Promise<string | null>;
|
|
38
38
|
getSourceMap(filePath: string): Promise<string | null>;
|
|
39
39
|
getSourceMapSync(filePath: string): string | null;
|
|
40
|
-
updateInfoSubscribe(aggregationMs: number): AsyncIterableIterator<TurbopackResult<
|
|
40
|
+
updateInfoSubscribe(aggregationMs: number): AsyncIterableIterator<TurbopackResult<NapiUpdateMessage>>;
|
|
41
41
|
shutdown(): Promise<void>;
|
|
42
42
|
onExit(): Promise<void>;
|
|
43
43
|
}>;
|
package/esm/project.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from "util";
|
|
2
2
|
import * as binding from "./binding";
|
|
3
|
-
import { nanoid } from "nanoid";
|
|
4
3
|
import { rustifyEnv } from "./util";
|
|
5
4
|
export class TurbopackInternalError extends Error {
|
|
6
5
|
constructor(cause) {
|
|
@@ -53,7 +52,6 @@ function ensureLoadersHaveSerializableOptions(turbopackRules) {
|
|
|
53
52
|
async function serializeConfig(config) {
|
|
54
53
|
var _a, _b;
|
|
55
54
|
let configSerializable = { ...config };
|
|
56
|
-
configSerializable.generateBuildId = () => nanoid();
|
|
57
55
|
if ((_a = configSerializable.module) === null || _a === void 0 ? void 0 : _a.rules) {
|
|
58
56
|
ensureLoadersHaveSerializableOptions(configSerializable.module.rules);
|
|
59
57
|
}
|
package/esm/types.d.ts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import { HmrIdentifiers, NapiIssue, NapiUpdateMessage, NapiWrittenEndpoint, StackFrame } from "./binding";
|
|
1
|
+
import { HmrIdentifiers, NapiDiagnostic, NapiIssue, NapiUpdateMessage, NapiWrittenEndpoint, StackFrame } from "./binding";
|
|
2
|
+
declare global {
|
|
3
|
+
export type TurbopackResult<T = {}> = T & {
|
|
4
|
+
issues: NapiIssue[];
|
|
5
|
+
diagnostics: NapiDiagnostic[];
|
|
6
|
+
};
|
|
7
|
+
export type RefCell = {
|
|
8
|
+
readonly __tag: unique symbol;
|
|
9
|
+
};
|
|
10
|
+
export type ExternalEndpoint = {
|
|
11
|
+
readonly __tag: unique symbol;
|
|
12
|
+
};
|
|
13
|
+
export type RcStr = string;
|
|
14
|
+
}
|
|
2
15
|
export interface BaseUpdate {
|
|
3
16
|
resource: {
|
|
4
17
|
headers: unknown;
|
|
@@ -53,7 +66,6 @@ export interface DefineEnv {
|
|
|
53
66
|
}
|
|
54
67
|
export interface ExperimentalConfig {
|
|
55
68
|
}
|
|
56
|
-
export type TurbopackRuleConfigItemOrShortcut = TurbopackRuleConfigItem;
|
|
57
69
|
export type TurbopackRuleConfigItem = TurbopackRuleConfigItemOptions | {
|
|
58
70
|
[condition: string]: TurbopackRuleConfigItem;
|
|
59
71
|
} | false;
|
|
@@ -69,7 +81,7 @@ export type TurbopackRuleConfigItemOptions = {
|
|
|
69
81
|
as?: string;
|
|
70
82
|
};
|
|
71
83
|
export interface ModuleOptions {
|
|
72
|
-
rules?: Record<string,
|
|
84
|
+
rules?: Record<string, TurbopackRuleConfigItem>;
|
|
73
85
|
}
|
|
74
86
|
export interface ResolveOptions {
|
|
75
87
|
alias?: Record<string, string | string[] | Record<string, string | string[]>>;
|
|
@@ -140,6 +152,7 @@ export interface ConfigComplete {
|
|
|
140
152
|
images?: {
|
|
141
153
|
inlineLimit?: number;
|
|
142
154
|
};
|
|
155
|
+
stats?: boolean;
|
|
143
156
|
experimental?: ExperimentalConfig;
|
|
144
157
|
persistentCaching?: boolean;
|
|
145
158
|
cacheHandler?: string;
|
|
@@ -193,25 +206,25 @@ export interface ProjectOptions {
|
|
|
193
206
|
/**
|
|
194
207
|
* A map of environment variables to use when compiling code.
|
|
195
208
|
*/
|
|
196
|
-
processEnv
|
|
197
|
-
|
|
209
|
+
processEnv?: Record<string, string>;
|
|
210
|
+
defineEnv?: DefineEnv;
|
|
198
211
|
/**
|
|
199
212
|
* Whether to watch the filesystem for file changes.
|
|
200
213
|
*/
|
|
201
|
-
watch
|
|
214
|
+
watch?: {
|
|
202
215
|
enable: boolean;
|
|
203
216
|
pollIntervalMs?: number;
|
|
204
217
|
};
|
|
205
218
|
/**
|
|
206
219
|
* The mode of utoo-pack.
|
|
207
220
|
*/
|
|
208
|
-
dev
|
|
221
|
+
dev?: boolean;
|
|
209
222
|
/**
|
|
210
223
|
* The build id.
|
|
211
224
|
*/
|
|
212
|
-
buildId
|
|
225
|
+
buildId?: string;
|
|
213
226
|
}
|
|
214
|
-
export type
|
|
227
|
+
export type BundleOptions = Omit<ProjectOptions, "rootPath" | "projectPath">;
|
|
215
228
|
export interface Project {
|
|
216
229
|
update(options: Partial<ProjectOptions>): Promise<void>;
|
|
217
230
|
entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<RawEntrypoints>>;
|
package/esm/util.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NapiIssue } from "./binding";
|
|
2
|
-
import {
|
|
2
|
+
import { ConfigComplete, DefineEnv, RustifiedEnv, StyledString } from "./types";
|
|
3
3
|
export declare class ModuleBuildError extends Error {
|
|
4
4
|
name: string;
|
|
5
5
|
}
|
package/esm/util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { bold, green, magenta, red } from "picocolors";
|
|
2
1
|
import { codeFrameColumns } from "@babel/code-frame";
|
|
2
|
+
import { bold, green, magenta, red } from "picocolors";
|
|
3
3
|
import { decodeMagicIdentifier, MAGIC_IDENTIFIER_REGEX, } from "./magicIdentifier";
|
|
4
4
|
export class ModuleBuildError extends Error {
|
|
5
5
|
constructor() {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type webpack from "webpack";
|
|
2
|
+
import { BundleOptions } from "./types";
|
|
3
|
+
export type WebpackConfig = Pick<webpack.Configuration, "name" | "entry" | "mode" | "module" | "resolve" | "externals" | "output" | "target" | "devtool" | "optimization" | "plugins" | "stats"> & {
|
|
4
|
+
compatMode: true;
|
|
5
|
+
};
|
|
6
|
+
export declare function compatOptionsFromWebpack(webpackConfig: WebpackConfig): BundleOptions;
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
export function compatOptionsFromWebpack(webpackConfig) {
|
|
2
|
+
const { entry, mode, module, resolve, externals, output, target, devtool, optimization, plugins, stats, } = webpackConfig;
|
|
3
|
+
return {
|
|
4
|
+
config: {
|
|
5
|
+
entry: compatEntry(entry),
|
|
6
|
+
mode: compatMode(mode),
|
|
7
|
+
module: compatModule(module),
|
|
8
|
+
resolve: compatResolve(resolve),
|
|
9
|
+
externals: compatExternals(externals),
|
|
10
|
+
output: compatOutput(output),
|
|
11
|
+
target: compatTarget(target),
|
|
12
|
+
sourceMaps: compatSourceMaps(devtool),
|
|
13
|
+
optimization: compatOptimization(optimization),
|
|
14
|
+
define: compatFromWebpackPlugin(plugins, compatDefine),
|
|
15
|
+
stats: compatStats(stats),
|
|
16
|
+
},
|
|
17
|
+
buildId: webpackConfig.name,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function compatMode(webpackMode) {
|
|
21
|
+
return webpackMode
|
|
22
|
+
? webpackMode === "none"
|
|
23
|
+
? "production"
|
|
24
|
+
: webpackMode
|
|
25
|
+
: "production";
|
|
26
|
+
}
|
|
27
|
+
function compatEntry(webpackEntry) {
|
|
28
|
+
const entry = [];
|
|
29
|
+
switch (typeof webpackEntry) {
|
|
30
|
+
case "string":
|
|
31
|
+
entry.push({ import: webpackEntry });
|
|
32
|
+
break;
|
|
33
|
+
case "object":
|
|
34
|
+
if (Array.isArray(webpackEntry)) {
|
|
35
|
+
webpackEntry.forEach((e) => entry.push({
|
|
36
|
+
import: e,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
Object.entries(webpackEntry).forEach(([k, v]) => {
|
|
41
|
+
var _a;
|
|
42
|
+
switch (typeof v) {
|
|
43
|
+
case "string":
|
|
44
|
+
entry.push({ name: k, import: v });
|
|
45
|
+
break;
|
|
46
|
+
case "object":
|
|
47
|
+
if (!Array.isArray(v)) {
|
|
48
|
+
switch (typeof v.import) {
|
|
49
|
+
case "string":
|
|
50
|
+
entry.push({
|
|
51
|
+
name: k,
|
|
52
|
+
import: v.import,
|
|
53
|
+
library: ((_a = v.library) === null || _a === void 0 ? void 0 : _a.type) === "umd"
|
|
54
|
+
? {
|
|
55
|
+
name: typeof v.library.name === "string"
|
|
56
|
+
? v.library.name
|
|
57
|
+
: undefined,
|
|
58
|
+
export: typeof v.library.export === "string"
|
|
59
|
+
? [v.library.export]
|
|
60
|
+
: v.library.export,
|
|
61
|
+
}
|
|
62
|
+
: undefined,
|
|
63
|
+
});
|
|
64
|
+
break;
|
|
65
|
+
default:
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw "multi entry items for one entry not supported yet";
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
throw "non string and non object entry path not supported yet";
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
case "function":
|
|
80
|
+
throw "functional entry not supported yet";
|
|
81
|
+
default:
|
|
82
|
+
throw "entry config not compatible now";
|
|
83
|
+
}
|
|
84
|
+
return entry;
|
|
85
|
+
}
|
|
86
|
+
function compatFromWebpackPlugin(webpackPlugins, picker) {
|
|
87
|
+
const plugin = webpackPlugins === null || webpackPlugins === void 0 ? void 0 : webpackPlugins.find((p) => p && typeof p === "object" && p.constructor.name === picker.pluginName);
|
|
88
|
+
return picker(plugin);
|
|
89
|
+
}
|
|
90
|
+
compatDefine.pluginName = "DefinePlugin";
|
|
91
|
+
function compatDefine(maybeWebpackPluginInstance) {
|
|
92
|
+
return maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
|
|
93
|
+
}
|
|
94
|
+
function compatExternals(webpackExternals) {
|
|
95
|
+
if (!webpackExternals) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
let externals = {};
|
|
99
|
+
switch (typeof webpackExternals) {
|
|
100
|
+
case "string": {
|
|
101
|
+
// Single string external: "lodash" -> { "lodash": "lodash" }
|
|
102
|
+
externals[webpackExternals] = webpackExternals;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case "object": {
|
|
106
|
+
if (Array.isArray(webpackExternals)) {
|
|
107
|
+
// Array of externals: ["lodash", "react"] -> { "lodash": "lodash", "react": "react" }
|
|
108
|
+
externals = webpackExternals.reduce((acc, external) => {
|
|
109
|
+
if (typeof external === "string") {
|
|
110
|
+
acc[external] = external;
|
|
111
|
+
}
|
|
112
|
+
else if (typeof external === "object" && external !== null) {
|
|
113
|
+
Object.assign(acc, compatExternals(external));
|
|
114
|
+
}
|
|
115
|
+
return acc;
|
|
116
|
+
}, {});
|
|
117
|
+
}
|
|
118
|
+
else if (webpackExternals instanceof RegExp) {
|
|
119
|
+
throw "regex external not supported yet";
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
if ("byLayer" in webpackExternals) {
|
|
123
|
+
throw "by layer external item not supported yet";
|
|
124
|
+
}
|
|
125
|
+
Object.entries(webpackExternals).forEach(([key, value]) => {
|
|
126
|
+
if (typeof value === "string") {
|
|
127
|
+
// Check if it's a script type with shorthand syntax: "global@https://example.com/script.js"
|
|
128
|
+
if (value.includes("@") &&
|
|
129
|
+
(value.startsWith("script ") || value.includes("://"))) {
|
|
130
|
+
const match = value.match(/^(?:script\s+)?(.+?)@(.+)$/);
|
|
131
|
+
if (match) {
|
|
132
|
+
const [, globalName, scriptUrl] = match;
|
|
133
|
+
// Use utoo-pack string format: "script globalName@url"
|
|
134
|
+
externals[key] = `script ${globalName}@${scriptUrl}`;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
externals[key] = value;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
// Simple string mapping: { "react": "React" }
|
|
142
|
+
externals[key] = value;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else if (Array.isArray(value)) {
|
|
146
|
+
// Array format handling
|
|
147
|
+
if (value.length >= 2) {
|
|
148
|
+
const [first, second] = value;
|
|
149
|
+
// Check if it's a script type array: ["https://example.com/script.js", "GlobalName"]
|
|
150
|
+
if (typeof first === "string" &&
|
|
151
|
+
first.includes("://") &&
|
|
152
|
+
typeof second === "string") {
|
|
153
|
+
// Use utoo-pack object format for script
|
|
154
|
+
externals[key] = {
|
|
155
|
+
root: second,
|
|
156
|
+
type: "script",
|
|
157
|
+
script: first,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
else if (typeof first === "string" &&
|
|
161
|
+
typeof second === "string") {
|
|
162
|
+
// Handle type prefix formats
|
|
163
|
+
if (first.startsWith("commonjs")) {
|
|
164
|
+
externals[key] = `commonjs ${second}`;
|
|
165
|
+
}
|
|
166
|
+
else if (first === "module") {
|
|
167
|
+
externals[key] = `esm ${second}`;
|
|
168
|
+
}
|
|
169
|
+
else if (first === "var" ||
|
|
170
|
+
first === "global" ||
|
|
171
|
+
first === "window") {
|
|
172
|
+
externals[key] = second;
|
|
173
|
+
}
|
|
174
|
+
else if (first === "script") {
|
|
175
|
+
// Script type without URL in array format - treat as regular script prefix
|
|
176
|
+
externals[key] = `script ${second}`;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
externals[key] = `${first} ${second}`;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
externals[key] = value[0] || key;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
externals[key] = value[0] || key;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else if (typeof value === "object" && value !== null) {
|
|
191
|
+
// Object format: handle complex configurations
|
|
192
|
+
if ("root" in value || "commonjs" in value || "amd" in value) {
|
|
193
|
+
// Standard webpack externals object format
|
|
194
|
+
if (value.commonjs) {
|
|
195
|
+
externals[key] = `commonjs ${value.commonjs}`;
|
|
196
|
+
}
|
|
197
|
+
else if (value.root) {
|
|
198
|
+
externals[key] = value.root;
|
|
199
|
+
}
|
|
200
|
+
else if (value.amd) {
|
|
201
|
+
externals[key] = value.amd;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
externals[key] = key;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
// Treat as utoo-pack specific configuration (might already be in correct format)
|
|
209
|
+
externals[key] = value;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
// Fallback to key name
|
|
214
|
+
externals[key] = key;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case "function": {
|
|
221
|
+
throw "functional external not supported yet";
|
|
222
|
+
}
|
|
223
|
+
default:
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
return externals;
|
|
227
|
+
}
|
|
228
|
+
function compatModule(webpackModule) {
|
|
229
|
+
if (!Array.isArray(webpackModule === null || webpackModule === void 0 ? void 0 : webpackModule.rules)) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const moduleRules = {
|
|
233
|
+
rules: webpackModule.rules.reduce((acc, cur) => {
|
|
234
|
+
var _a, _b;
|
|
235
|
+
switch (typeof cur) {
|
|
236
|
+
case "object":
|
|
237
|
+
if (cur) {
|
|
238
|
+
let condition = (_b = (_a = cur.test) === null || _a === void 0 ? void 0 : _a.toString().match(/(\.\w+)/)) === null || _b === void 0 ? void 0 : _b[1];
|
|
239
|
+
if (condition) {
|
|
240
|
+
Object.assign(acc, {
|
|
241
|
+
["*" + condition]: {
|
|
242
|
+
loaders: typeof cur.use === "string"
|
|
243
|
+
? [cur.use]
|
|
244
|
+
: typeof (cur === null || cur === void 0 ? void 0 : cur.use) === "object"
|
|
245
|
+
? Array.isArray(cur.use)
|
|
246
|
+
? cur.use.map((use) => typeof use === "string"
|
|
247
|
+
? { loader: use, options: {} }
|
|
248
|
+
: {
|
|
249
|
+
loader: use.loader,
|
|
250
|
+
options: use.options || {},
|
|
251
|
+
})
|
|
252
|
+
: [
|
|
253
|
+
{
|
|
254
|
+
loader: cur.loader,
|
|
255
|
+
options: cur.options || {},
|
|
256
|
+
},
|
|
257
|
+
]
|
|
258
|
+
: [],
|
|
259
|
+
as: "*.js",
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
break;
|
|
265
|
+
default:
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
return acc;
|
|
269
|
+
}, {}),
|
|
270
|
+
};
|
|
271
|
+
return moduleRules;
|
|
272
|
+
}
|
|
273
|
+
function compatResolve(webpackResolve) {
|
|
274
|
+
if (!webpackResolve) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const { alias, extensions } = webpackResolve;
|
|
278
|
+
return {
|
|
279
|
+
alias: alias
|
|
280
|
+
? Array.isArray(alias)
|
|
281
|
+
? alias.reduce((acc, cur) => Object.assign(acc, { [cur.name]: cur.alias }), {})
|
|
282
|
+
: Object.entries(alias).reduce((acc, [k, v]) => {
|
|
283
|
+
if (typeof v === "string") {
|
|
284
|
+
Object.assign(acc, { [k]: v });
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
throw "non string alias value not supported yet";
|
|
288
|
+
}
|
|
289
|
+
return acc;
|
|
290
|
+
}, {})
|
|
291
|
+
: undefined,
|
|
292
|
+
extensions,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function compatOutput(webpackOutput) {
|
|
296
|
+
if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename) && typeof webpackOutput.filename !== "string") {
|
|
297
|
+
throw "non string output filename not supported yet";
|
|
298
|
+
}
|
|
299
|
+
if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename) &&
|
|
300
|
+
typeof webpackOutput.chunkFilename !== "string") {
|
|
301
|
+
throw "non string output chunkFilename not supported yet";
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
path: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.path,
|
|
305
|
+
filename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename,
|
|
306
|
+
chunkFilename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename,
|
|
307
|
+
clean: !!(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.clean),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function compatTarget(webpackTarget) {
|
|
311
|
+
return webpackTarget
|
|
312
|
+
? Array.isArray(webpackTarget)
|
|
313
|
+
? webpackTarget.join(" ")
|
|
314
|
+
: webpackTarget
|
|
315
|
+
: undefined;
|
|
316
|
+
}
|
|
317
|
+
function compatSourceMaps(webpackSourceMaps) {
|
|
318
|
+
return !!webpackSourceMaps;
|
|
319
|
+
}
|
|
320
|
+
function compatOptimization(webpackOptimization) {
|
|
321
|
+
if (!webpackOptimization) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const { moduleIds, minimize,
|
|
325
|
+
// TODO: concatenateModules to be supported, need to upgrade to next.js@15.4
|
|
326
|
+
} = webpackOptimization;
|
|
327
|
+
return {
|
|
328
|
+
moduleIds: moduleIds === "named"
|
|
329
|
+
? "named"
|
|
330
|
+
: moduleIds === "deterministic"
|
|
331
|
+
? "deterministic"
|
|
332
|
+
: undefined,
|
|
333
|
+
minify: minimize,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
function compatStats(webpackStats) {
|
|
337
|
+
return !!webpackStats;
|
|
338
|
+
}
|