@kitsy/cnos-webpack 1.5.0
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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/index.cjs +147 -0
- package/dist/index.d.cts +242 -0
- package/dist/index.d.ts +242 -0
- package/dist/index.js +118 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kitsy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CnosWebpackPlugin: () => CnosWebpackPlugin,
|
|
24
|
+
loadCnosWebpackEnv: () => loadCnosWebpackEnv,
|
|
25
|
+
loadCnosWebpackRuntime: () => loadCnosWebpackRuntime,
|
|
26
|
+
resolveCnosWebpackBindings: () => resolveCnosWebpackBindings,
|
|
27
|
+
resolveCnosWebpackBuildConfig: () => resolveCnosWebpackBuildConfig
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
var import_build = require("@kitsy/cnos/build");
|
|
31
|
+
var import_configure = require("@kitsy/cnos/configure");
|
|
32
|
+
var PLUGIN_NAME = "CnosWebpackPlugin";
|
|
33
|
+
async function loadCnosWebpackEnv(options = {}) {
|
|
34
|
+
return (0, import_build.resolveFrameworkEnv)(options, "webpack", {
|
|
35
|
+
...options.prefix ? { prefix: options.prefix } : {}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async function resolveCnosWebpackBindings(options = {}) {
|
|
39
|
+
const [browserData, publicEnv] = await Promise.all([
|
|
40
|
+
(0, import_build.resolveBrowserData)(options),
|
|
41
|
+
loadCnosWebpackEnv(options)
|
|
42
|
+
]);
|
|
43
|
+
const definitions = {
|
|
44
|
+
"globalThis.__CNOS_BROWSER_DATA__": JSON.stringify(JSON.stringify(browserData))
|
|
45
|
+
};
|
|
46
|
+
if (options.includeProcessEnvShim ?? true) {
|
|
47
|
+
definitions["process.env.__CNOS_BROWSER_DATA__"] = JSON.stringify(JSON.stringify(browserData));
|
|
48
|
+
}
|
|
49
|
+
for (const [key, value] of Object.entries(publicEnv)) {
|
|
50
|
+
definitions[`process.env.${key}`] = JSON.stringify(value);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
browserData,
|
|
54
|
+
publicEnv,
|
|
55
|
+
definitions
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function createDynamicDefinitions(DefinePlugin, state, keys, options) {
|
|
59
|
+
const definitions = {
|
|
60
|
+
"globalThis.__CNOS_BROWSER_DATA__": DefinePlugin.runtimeValue(
|
|
61
|
+
() => JSON.stringify(JSON.stringify(state.browserData)),
|
|
62
|
+
true
|
|
63
|
+
)
|
|
64
|
+
};
|
|
65
|
+
if (options.includeProcessEnvShim ?? true) {
|
|
66
|
+
definitions["process.env.__CNOS_BROWSER_DATA__"] = DefinePlugin.runtimeValue(
|
|
67
|
+
() => JSON.stringify(JSON.stringify(state.browserData)),
|
|
68
|
+
true
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
for (const key of keys) {
|
|
72
|
+
definitions[`process.env.${key}`] = DefinePlugin.runtimeValue(
|
|
73
|
+
() => JSON.stringify(state.publicEnv[key] ?? ""),
|
|
74
|
+
true
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return definitions;
|
|
78
|
+
}
|
|
79
|
+
var CnosWebpackPlugin = class {
|
|
80
|
+
options;
|
|
81
|
+
constructor(options = {}) {
|
|
82
|
+
this.options = options;
|
|
83
|
+
}
|
|
84
|
+
apply(compiler) {
|
|
85
|
+
const state = {
|
|
86
|
+
browserData: {},
|
|
87
|
+
publicEnv: {}
|
|
88
|
+
};
|
|
89
|
+
let initialized = false;
|
|
90
|
+
let knownKeys = /* @__PURE__ */ new Set();
|
|
91
|
+
const updateState = async () => {
|
|
92
|
+
const bindings = await resolveCnosWebpackBindings(this.options);
|
|
93
|
+
state.browserData = bindings.browserData;
|
|
94
|
+
state.publicEnv = bindings.publicEnv;
|
|
95
|
+
if (initialized) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
knownKeys = new Set(Object.keys(bindings.publicEnv));
|
|
99
|
+
const definitions = createDynamicDefinitions(
|
|
100
|
+
compiler.webpack.DefinePlugin,
|
|
101
|
+
state,
|
|
102
|
+
[...knownKeys],
|
|
103
|
+
this.options
|
|
104
|
+
);
|
|
105
|
+
new compiler.webpack.DefinePlugin(definitions).apply(compiler);
|
|
106
|
+
initialized = true;
|
|
107
|
+
};
|
|
108
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, async () => {
|
|
109
|
+
await updateState();
|
|
110
|
+
});
|
|
111
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
112
|
+
const wasInitialized = initialized;
|
|
113
|
+
const previousKeys = new Set(knownKeys);
|
|
114
|
+
await updateState();
|
|
115
|
+
if (wasInitialized && Object.keys(state.publicEnv).some((key) => !previousKeys.has(key))) {
|
|
116
|
+
process.stderr.write(
|
|
117
|
+
"[cnos-webpack] New public env keys were detected during watch mode. Restart webpack to apply new define entries.\n"
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
async function loadCnosWebpackRuntime(options = {}) {
|
|
124
|
+
return (0, import_configure.createCnos)(options);
|
|
125
|
+
}
|
|
126
|
+
async function resolveCnosWebpackBuildConfig(options = {}) {
|
|
127
|
+
const [runtime, browserData, publicEnv] = await Promise.all([
|
|
128
|
+
(0, import_configure.createCnos)(options),
|
|
129
|
+
(0, import_build.resolveBrowserData)(options),
|
|
130
|
+
(0, import_build.resolveFrameworkEnv)(options, "webpack", {
|
|
131
|
+
...options.prefix ? { prefix: options.prefix } : {}
|
|
132
|
+
})
|
|
133
|
+
]);
|
|
134
|
+
return {
|
|
135
|
+
runtime,
|
|
136
|
+
browserData,
|
|
137
|
+
publicEnv
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
141
|
+
0 && (module.exports = {
|
|
142
|
+
CnosWebpackPlugin,
|
|
143
|
+
loadCnosWebpackEnv,
|
|
144
|
+
loadCnosWebpackRuntime,
|
|
145
|
+
resolveCnosWebpackBindings,
|
|
146
|
+
resolveCnosWebpackBuildConfig
|
|
147
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
export { FrameworkEnvTarget } from '@kitsy/cnos/build';
|
|
2
|
+
import { CnosCreateOptions, createCnos } from '@kitsy/cnos/configure';
|
|
3
|
+
|
|
4
|
+
type ProfileResolveFrom = 'cli.profile' | 'env.CNOS_PROFILE' | 'default';
|
|
5
|
+
type ProfileSource = 'cli' | 'workspace-file' | 'env' | 'manifest-default';
|
|
6
|
+
|
|
7
|
+
interface SchemaRule {
|
|
8
|
+
type?: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
9
|
+
required?: boolean;
|
|
10
|
+
enum?: unknown[];
|
|
11
|
+
pattern?: string;
|
|
12
|
+
default?: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type WorkspaceSource = 'cli' | 'workspace-file' | 'manifest-default' | 'implicit';
|
|
16
|
+
type GlobalRootSource = 'cli' | 'workspace-file' | 'manifest' | 'CNOS_HOME';
|
|
17
|
+
interface NormalizedWorkspaceItem {
|
|
18
|
+
extends: string[];
|
|
19
|
+
globalId?: string;
|
|
20
|
+
}
|
|
21
|
+
interface WorkspaceRoot {
|
|
22
|
+
scope: 'global' | 'local';
|
|
23
|
+
workspaceId: string;
|
|
24
|
+
path: string;
|
|
25
|
+
}
|
|
26
|
+
interface WorkspaceContext {
|
|
27
|
+
workspaceId: string;
|
|
28
|
+
workspaceSource: WorkspaceSource;
|
|
29
|
+
globalRoot?: string;
|
|
30
|
+
globalRootSource?: GlobalRootSource;
|
|
31
|
+
workspaceChain: string[];
|
|
32
|
+
workspaceRoots: WorkspaceRoot[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type ResolutionArrayPolicy = 'replace' | 'append' | 'unique-append';
|
|
36
|
+
type NamespaceKind = 'data' | 'projection' | 'system';
|
|
37
|
+
type NamespaceProjectionSource = 'promote' | 'envMapping';
|
|
38
|
+
type VaultProviderName = 'local' | 'github-secrets' | (string & {});
|
|
39
|
+
type VaultAuthMethod = 'passphrase' | 'environment' | 'token' | 'iam' | 'keychain';
|
|
40
|
+
interface VaultAuthSourceConfig {
|
|
41
|
+
from?: string[];
|
|
42
|
+
}
|
|
43
|
+
interface VaultAuthDefinition {
|
|
44
|
+
method?: VaultAuthMethod;
|
|
45
|
+
passphrase?: VaultAuthSourceConfig;
|
|
46
|
+
token?: VaultAuthSourceConfig;
|
|
47
|
+
config?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
interface NamespaceDefinition {
|
|
50
|
+
kind: NamespaceKind;
|
|
51
|
+
shareable: boolean;
|
|
52
|
+
sensitive?: boolean;
|
|
53
|
+
readonly?: boolean;
|
|
54
|
+
source?: NamespaceProjectionSource;
|
|
55
|
+
}
|
|
56
|
+
interface VaultDefinition {
|
|
57
|
+
provider: VaultProviderName;
|
|
58
|
+
auth?: VaultAuthDefinition;
|
|
59
|
+
mapping?: Record<string, string>;
|
|
60
|
+
}
|
|
61
|
+
interface NormalizedManifest {
|
|
62
|
+
version: 1;
|
|
63
|
+
project: {
|
|
64
|
+
name: string;
|
|
65
|
+
};
|
|
66
|
+
workspaces: {
|
|
67
|
+
default?: string;
|
|
68
|
+
global: {
|
|
69
|
+
enabled: boolean;
|
|
70
|
+
root?: string;
|
|
71
|
+
allowWrite: boolean;
|
|
72
|
+
};
|
|
73
|
+
items: Record<string, NormalizedWorkspaceItem>;
|
|
74
|
+
};
|
|
75
|
+
profiles: {
|
|
76
|
+
default: string;
|
|
77
|
+
resolveFrom: ProfileResolveFrom[];
|
|
78
|
+
};
|
|
79
|
+
plugins: {
|
|
80
|
+
loaders: string[];
|
|
81
|
+
resolver: string;
|
|
82
|
+
validators: string[];
|
|
83
|
+
exporters: string[];
|
|
84
|
+
inspectors: string[];
|
|
85
|
+
};
|
|
86
|
+
sources: Record<string, Record<string, unknown>>;
|
|
87
|
+
resolution: {
|
|
88
|
+
precedence: string[];
|
|
89
|
+
arrayPolicy: ResolutionArrayPolicy;
|
|
90
|
+
};
|
|
91
|
+
envMapping: {
|
|
92
|
+
convention?: 'SCREAMING_SNAKE';
|
|
93
|
+
explicit: Record<string, LogicalKey>;
|
|
94
|
+
};
|
|
95
|
+
public: {
|
|
96
|
+
promote: LogicalKey[];
|
|
97
|
+
frameworks: Record<string, string>;
|
|
98
|
+
};
|
|
99
|
+
namespaces: Record<string, NamespaceDefinition>;
|
|
100
|
+
vaults: Record<string, VaultDefinition>;
|
|
101
|
+
writePolicy: {
|
|
102
|
+
define: {
|
|
103
|
+
defaultProfile: string;
|
|
104
|
+
targets: Record<'value' | 'secret', string>;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
schema: Record<LogicalKey, SchemaRule>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type LogicalKey = string;
|
|
111
|
+
type NamespaceName = string;
|
|
112
|
+
interface ConfigOrigin {
|
|
113
|
+
file?: string;
|
|
114
|
+
line?: number;
|
|
115
|
+
envVar?: string;
|
|
116
|
+
cliArg?: string;
|
|
117
|
+
}
|
|
118
|
+
interface ConfigEntry {
|
|
119
|
+
key: LogicalKey;
|
|
120
|
+
value: unknown;
|
|
121
|
+
namespace: NamespaceName;
|
|
122
|
+
sourceId: string;
|
|
123
|
+
pluginId: string;
|
|
124
|
+
workspaceId: string;
|
|
125
|
+
profile?: string;
|
|
126
|
+
origin?: ConfigOrigin;
|
|
127
|
+
metadata?: Record<string, unknown>;
|
|
128
|
+
}
|
|
129
|
+
interface ResolvedEntry {
|
|
130
|
+
key: LogicalKey;
|
|
131
|
+
value: unknown;
|
|
132
|
+
namespace: NamespaceName;
|
|
133
|
+
winner: ConfigEntry;
|
|
134
|
+
overridden: ConfigEntry[];
|
|
135
|
+
}
|
|
136
|
+
interface ResolvedGraph {
|
|
137
|
+
entries: Map<LogicalKey, ResolvedEntry>;
|
|
138
|
+
profile: string;
|
|
139
|
+
resolvedAt: string;
|
|
140
|
+
profileSource: ProfileSource;
|
|
141
|
+
workspace: WorkspaceContext;
|
|
142
|
+
}
|
|
143
|
+
interface InspectResult {
|
|
144
|
+
key: LogicalKey;
|
|
145
|
+
value: unknown;
|
|
146
|
+
namespace: NamespaceName;
|
|
147
|
+
profile: string;
|
|
148
|
+
profileSource: ProfileSource;
|
|
149
|
+
workspace: {
|
|
150
|
+
id: string;
|
|
151
|
+
source: WorkspaceContext['workspaceSource'];
|
|
152
|
+
chain: string[];
|
|
153
|
+
};
|
|
154
|
+
winner: {
|
|
155
|
+
sourceId: string;
|
|
156
|
+
pluginId: string;
|
|
157
|
+
workspaceId: string;
|
|
158
|
+
origin?: ConfigOrigin;
|
|
159
|
+
};
|
|
160
|
+
overridden: Array<{
|
|
161
|
+
sourceId: string;
|
|
162
|
+
pluginId: string;
|
|
163
|
+
workspaceId: string;
|
|
164
|
+
value: unknown;
|
|
165
|
+
origin?: ConfigOrigin;
|
|
166
|
+
}>;
|
|
167
|
+
}
|
|
168
|
+
interface ToEnvOptions {
|
|
169
|
+
includeSecrets?: boolean;
|
|
170
|
+
}
|
|
171
|
+
interface ToPublicEnvOptions {
|
|
172
|
+
framework?: string;
|
|
173
|
+
prefix?: string;
|
|
174
|
+
}
|
|
175
|
+
interface CnosRuntime {
|
|
176
|
+
manifest: NormalizedManifest;
|
|
177
|
+
plugins: CnosPlugin[];
|
|
178
|
+
readonly graph: ResolvedGraph;
|
|
179
|
+
read<T = unknown>(key: LogicalKey): T | undefined;
|
|
180
|
+
require<T = unknown>(key: LogicalKey): T;
|
|
181
|
+
readOr<T>(key: LogicalKey, fallback: T): T;
|
|
182
|
+
value<T = unknown>(path: string): T | undefined;
|
|
183
|
+
secret<T = unknown>(path: string): T | undefined;
|
|
184
|
+
meta<T = unknown>(path: string): T | undefined;
|
|
185
|
+
inspect(key: LogicalKey): InspectResult;
|
|
186
|
+
toObject(): Record<string, unknown>;
|
|
187
|
+
toNamespace(namespace: NamespaceName): Record<string, unknown>;
|
|
188
|
+
toEnv(options?: ToEnvOptions): Record<string, string>;
|
|
189
|
+
toPublicEnv(options?: ToPublicEnvOptions): Record<string, string>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
type CnosPluginKind = 'loader' | 'resolver' | 'validator' | 'exporter' | 'inspector';
|
|
193
|
+
interface CnosPlugin {
|
|
194
|
+
id: string;
|
|
195
|
+
kind: CnosPluginKind;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface CnosWebpackPluginOptions extends CnosCreateOptions {
|
|
199
|
+
prefix?: string;
|
|
200
|
+
includeProcessEnvShim?: boolean;
|
|
201
|
+
}
|
|
202
|
+
interface CnosWebpackBindings {
|
|
203
|
+
browserData: Record<string, unknown>;
|
|
204
|
+
publicEnv: Record<string, string>;
|
|
205
|
+
definitions: Record<string, unknown>;
|
|
206
|
+
}
|
|
207
|
+
interface WebpackLikeDefinePluginInstance {
|
|
208
|
+
apply(compiler: WebpackCompilerLike): void;
|
|
209
|
+
}
|
|
210
|
+
interface WebpackLikeDefinePluginConstructor {
|
|
211
|
+
new (definitions: Record<string, unknown>): WebpackLikeDefinePluginInstance;
|
|
212
|
+
runtimeValue(getter: () => string, watched: true | string[] | {
|
|
213
|
+
fileDependencies?: string[];
|
|
214
|
+
}): unknown;
|
|
215
|
+
}
|
|
216
|
+
interface TapPromiseHookLike<TArgs extends unknown[] = []> {
|
|
217
|
+
tapPromise(name: string, handler: (...args: TArgs) => Promise<void>): void;
|
|
218
|
+
}
|
|
219
|
+
interface WebpackCompilerLike {
|
|
220
|
+
webpack: {
|
|
221
|
+
DefinePlugin: WebpackLikeDefinePluginConstructor;
|
|
222
|
+
};
|
|
223
|
+
hooks: {
|
|
224
|
+
beforeRun: TapPromiseHookLike<[WebpackCompilerLike]>;
|
|
225
|
+
watchRun: TapPromiseHookLike<[WebpackCompilerLike]>;
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
declare function loadCnosWebpackEnv(options?: CnosWebpackPluginOptions): Promise<Record<string, string>>;
|
|
229
|
+
declare function resolveCnosWebpackBindings(options?: CnosWebpackPluginOptions): Promise<CnosWebpackBindings>;
|
|
230
|
+
declare class CnosWebpackPlugin {
|
|
231
|
+
readonly options: CnosWebpackPluginOptions;
|
|
232
|
+
constructor(options?: CnosWebpackPluginOptions);
|
|
233
|
+
apply(compiler: WebpackCompilerLike): void;
|
|
234
|
+
}
|
|
235
|
+
declare function loadCnosWebpackRuntime(options?: CnosCreateOptions): Promise<CnosRuntime>;
|
|
236
|
+
declare function resolveCnosWebpackBuildConfig(options?: CnosWebpackPluginOptions): Promise<{
|
|
237
|
+
runtime: Awaited<ReturnType<typeof createCnos>>;
|
|
238
|
+
browserData: Record<string, unknown>;
|
|
239
|
+
publicEnv: Record<string, string>;
|
|
240
|
+
}>;
|
|
241
|
+
|
|
242
|
+
export { type CnosWebpackBindings, CnosWebpackPlugin, type CnosWebpackPluginOptions, type TapPromiseHookLike, type WebpackCompilerLike, type WebpackLikeDefinePluginConstructor, type WebpackLikeDefinePluginInstance, loadCnosWebpackEnv, loadCnosWebpackRuntime, resolveCnosWebpackBindings, resolveCnosWebpackBuildConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
export { FrameworkEnvTarget } from '@kitsy/cnos/build';
|
|
2
|
+
import { CnosCreateOptions, createCnos } from '@kitsy/cnos/configure';
|
|
3
|
+
|
|
4
|
+
type ProfileResolveFrom = 'cli.profile' | 'env.CNOS_PROFILE' | 'default';
|
|
5
|
+
type ProfileSource = 'cli' | 'workspace-file' | 'env' | 'manifest-default';
|
|
6
|
+
|
|
7
|
+
interface SchemaRule {
|
|
8
|
+
type?: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
9
|
+
required?: boolean;
|
|
10
|
+
enum?: unknown[];
|
|
11
|
+
pattern?: string;
|
|
12
|
+
default?: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type WorkspaceSource = 'cli' | 'workspace-file' | 'manifest-default' | 'implicit';
|
|
16
|
+
type GlobalRootSource = 'cli' | 'workspace-file' | 'manifest' | 'CNOS_HOME';
|
|
17
|
+
interface NormalizedWorkspaceItem {
|
|
18
|
+
extends: string[];
|
|
19
|
+
globalId?: string;
|
|
20
|
+
}
|
|
21
|
+
interface WorkspaceRoot {
|
|
22
|
+
scope: 'global' | 'local';
|
|
23
|
+
workspaceId: string;
|
|
24
|
+
path: string;
|
|
25
|
+
}
|
|
26
|
+
interface WorkspaceContext {
|
|
27
|
+
workspaceId: string;
|
|
28
|
+
workspaceSource: WorkspaceSource;
|
|
29
|
+
globalRoot?: string;
|
|
30
|
+
globalRootSource?: GlobalRootSource;
|
|
31
|
+
workspaceChain: string[];
|
|
32
|
+
workspaceRoots: WorkspaceRoot[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type ResolutionArrayPolicy = 'replace' | 'append' | 'unique-append';
|
|
36
|
+
type NamespaceKind = 'data' | 'projection' | 'system';
|
|
37
|
+
type NamespaceProjectionSource = 'promote' | 'envMapping';
|
|
38
|
+
type VaultProviderName = 'local' | 'github-secrets' | (string & {});
|
|
39
|
+
type VaultAuthMethod = 'passphrase' | 'environment' | 'token' | 'iam' | 'keychain';
|
|
40
|
+
interface VaultAuthSourceConfig {
|
|
41
|
+
from?: string[];
|
|
42
|
+
}
|
|
43
|
+
interface VaultAuthDefinition {
|
|
44
|
+
method?: VaultAuthMethod;
|
|
45
|
+
passphrase?: VaultAuthSourceConfig;
|
|
46
|
+
token?: VaultAuthSourceConfig;
|
|
47
|
+
config?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
interface NamespaceDefinition {
|
|
50
|
+
kind: NamespaceKind;
|
|
51
|
+
shareable: boolean;
|
|
52
|
+
sensitive?: boolean;
|
|
53
|
+
readonly?: boolean;
|
|
54
|
+
source?: NamespaceProjectionSource;
|
|
55
|
+
}
|
|
56
|
+
interface VaultDefinition {
|
|
57
|
+
provider: VaultProviderName;
|
|
58
|
+
auth?: VaultAuthDefinition;
|
|
59
|
+
mapping?: Record<string, string>;
|
|
60
|
+
}
|
|
61
|
+
interface NormalizedManifest {
|
|
62
|
+
version: 1;
|
|
63
|
+
project: {
|
|
64
|
+
name: string;
|
|
65
|
+
};
|
|
66
|
+
workspaces: {
|
|
67
|
+
default?: string;
|
|
68
|
+
global: {
|
|
69
|
+
enabled: boolean;
|
|
70
|
+
root?: string;
|
|
71
|
+
allowWrite: boolean;
|
|
72
|
+
};
|
|
73
|
+
items: Record<string, NormalizedWorkspaceItem>;
|
|
74
|
+
};
|
|
75
|
+
profiles: {
|
|
76
|
+
default: string;
|
|
77
|
+
resolveFrom: ProfileResolveFrom[];
|
|
78
|
+
};
|
|
79
|
+
plugins: {
|
|
80
|
+
loaders: string[];
|
|
81
|
+
resolver: string;
|
|
82
|
+
validators: string[];
|
|
83
|
+
exporters: string[];
|
|
84
|
+
inspectors: string[];
|
|
85
|
+
};
|
|
86
|
+
sources: Record<string, Record<string, unknown>>;
|
|
87
|
+
resolution: {
|
|
88
|
+
precedence: string[];
|
|
89
|
+
arrayPolicy: ResolutionArrayPolicy;
|
|
90
|
+
};
|
|
91
|
+
envMapping: {
|
|
92
|
+
convention?: 'SCREAMING_SNAKE';
|
|
93
|
+
explicit: Record<string, LogicalKey>;
|
|
94
|
+
};
|
|
95
|
+
public: {
|
|
96
|
+
promote: LogicalKey[];
|
|
97
|
+
frameworks: Record<string, string>;
|
|
98
|
+
};
|
|
99
|
+
namespaces: Record<string, NamespaceDefinition>;
|
|
100
|
+
vaults: Record<string, VaultDefinition>;
|
|
101
|
+
writePolicy: {
|
|
102
|
+
define: {
|
|
103
|
+
defaultProfile: string;
|
|
104
|
+
targets: Record<'value' | 'secret', string>;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
schema: Record<LogicalKey, SchemaRule>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type LogicalKey = string;
|
|
111
|
+
type NamespaceName = string;
|
|
112
|
+
interface ConfigOrigin {
|
|
113
|
+
file?: string;
|
|
114
|
+
line?: number;
|
|
115
|
+
envVar?: string;
|
|
116
|
+
cliArg?: string;
|
|
117
|
+
}
|
|
118
|
+
interface ConfigEntry {
|
|
119
|
+
key: LogicalKey;
|
|
120
|
+
value: unknown;
|
|
121
|
+
namespace: NamespaceName;
|
|
122
|
+
sourceId: string;
|
|
123
|
+
pluginId: string;
|
|
124
|
+
workspaceId: string;
|
|
125
|
+
profile?: string;
|
|
126
|
+
origin?: ConfigOrigin;
|
|
127
|
+
metadata?: Record<string, unknown>;
|
|
128
|
+
}
|
|
129
|
+
interface ResolvedEntry {
|
|
130
|
+
key: LogicalKey;
|
|
131
|
+
value: unknown;
|
|
132
|
+
namespace: NamespaceName;
|
|
133
|
+
winner: ConfigEntry;
|
|
134
|
+
overridden: ConfigEntry[];
|
|
135
|
+
}
|
|
136
|
+
interface ResolvedGraph {
|
|
137
|
+
entries: Map<LogicalKey, ResolvedEntry>;
|
|
138
|
+
profile: string;
|
|
139
|
+
resolvedAt: string;
|
|
140
|
+
profileSource: ProfileSource;
|
|
141
|
+
workspace: WorkspaceContext;
|
|
142
|
+
}
|
|
143
|
+
interface InspectResult {
|
|
144
|
+
key: LogicalKey;
|
|
145
|
+
value: unknown;
|
|
146
|
+
namespace: NamespaceName;
|
|
147
|
+
profile: string;
|
|
148
|
+
profileSource: ProfileSource;
|
|
149
|
+
workspace: {
|
|
150
|
+
id: string;
|
|
151
|
+
source: WorkspaceContext['workspaceSource'];
|
|
152
|
+
chain: string[];
|
|
153
|
+
};
|
|
154
|
+
winner: {
|
|
155
|
+
sourceId: string;
|
|
156
|
+
pluginId: string;
|
|
157
|
+
workspaceId: string;
|
|
158
|
+
origin?: ConfigOrigin;
|
|
159
|
+
};
|
|
160
|
+
overridden: Array<{
|
|
161
|
+
sourceId: string;
|
|
162
|
+
pluginId: string;
|
|
163
|
+
workspaceId: string;
|
|
164
|
+
value: unknown;
|
|
165
|
+
origin?: ConfigOrigin;
|
|
166
|
+
}>;
|
|
167
|
+
}
|
|
168
|
+
interface ToEnvOptions {
|
|
169
|
+
includeSecrets?: boolean;
|
|
170
|
+
}
|
|
171
|
+
interface ToPublicEnvOptions {
|
|
172
|
+
framework?: string;
|
|
173
|
+
prefix?: string;
|
|
174
|
+
}
|
|
175
|
+
interface CnosRuntime {
|
|
176
|
+
manifest: NormalizedManifest;
|
|
177
|
+
plugins: CnosPlugin[];
|
|
178
|
+
readonly graph: ResolvedGraph;
|
|
179
|
+
read<T = unknown>(key: LogicalKey): T | undefined;
|
|
180
|
+
require<T = unknown>(key: LogicalKey): T;
|
|
181
|
+
readOr<T>(key: LogicalKey, fallback: T): T;
|
|
182
|
+
value<T = unknown>(path: string): T | undefined;
|
|
183
|
+
secret<T = unknown>(path: string): T | undefined;
|
|
184
|
+
meta<T = unknown>(path: string): T | undefined;
|
|
185
|
+
inspect(key: LogicalKey): InspectResult;
|
|
186
|
+
toObject(): Record<string, unknown>;
|
|
187
|
+
toNamespace(namespace: NamespaceName): Record<string, unknown>;
|
|
188
|
+
toEnv(options?: ToEnvOptions): Record<string, string>;
|
|
189
|
+
toPublicEnv(options?: ToPublicEnvOptions): Record<string, string>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
type CnosPluginKind = 'loader' | 'resolver' | 'validator' | 'exporter' | 'inspector';
|
|
193
|
+
interface CnosPlugin {
|
|
194
|
+
id: string;
|
|
195
|
+
kind: CnosPluginKind;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface CnosWebpackPluginOptions extends CnosCreateOptions {
|
|
199
|
+
prefix?: string;
|
|
200
|
+
includeProcessEnvShim?: boolean;
|
|
201
|
+
}
|
|
202
|
+
interface CnosWebpackBindings {
|
|
203
|
+
browserData: Record<string, unknown>;
|
|
204
|
+
publicEnv: Record<string, string>;
|
|
205
|
+
definitions: Record<string, unknown>;
|
|
206
|
+
}
|
|
207
|
+
interface WebpackLikeDefinePluginInstance {
|
|
208
|
+
apply(compiler: WebpackCompilerLike): void;
|
|
209
|
+
}
|
|
210
|
+
interface WebpackLikeDefinePluginConstructor {
|
|
211
|
+
new (definitions: Record<string, unknown>): WebpackLikeDefinePluginInstance;
|
|
212
|
+
runtimeValue(getter: () => string, watched: true | string[] | {
|
|
213
|
+
fileDependencies?: string[];
|
|
214
|
+
}): unknown;
|
|
215
|
+
}
|
|
216
|
+
interface TapPromiseHookLike<TArgs extends unknown[] = []> {
|
|
217
|
+
tapPromise(name: string, handler: (...args: TArgs) => Promise<void>): void;
|
|
218
|
+
}
|
|
219
|
+
interface WebpackCompilerLike {
|
|
220
|
+
webpack: {
|
|
221
|
+
DefinePlugin: WebpackLikeDefinePluginConstructor;
|
|
222
|
+
};
|
|
223
|
+
hooks: {
|
|
224
|
+
beforeRun: TapPromiseHookLike<[WebpackCompilerLike]>;
|
|
225
|
+
watchRun: TapPromiseHookLike<[WebpackCompilerLike]>;
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
declare function loadCnosWebpackEnv(options?: CnosWebpackPluginOptions): Promise<Record<string, string>>;
|
|
229
|
+
declare function resolveCnosWebpackBindings(options?: CnosWebpackPluginOptions): Promise<CnosWebpackBindings>;
|
|
230
|
+
declare class CnosWebpackPlugin {
|
|
231
|
+
readonly options: CnosWebpackPluginOptions;
|
|
232
|
+
constructor(options?: CnosWebpackPluginOptions);
|
|
233
|
+
apply(compiler: WebpackCompilerLike): void;
|
|
234
|
+
}
|
|
235
|
+
declare function loadCnosWebpackRuntime(options?: CnosCreateOptions): Promise<CnosRuntime>;
|
|
236
|
+
declare function resolveCnosWebpackBuildConfig(options?: CnosWebpackPluginOptions): Promise<{
|
|
237
|
+
runtime: Awaited<ReturnType<typeof createCnos>>;
|
|
238
|
+
browserData: Record<string, unknown>;
|
|
239
|
+
publicEnv: Record<string, string>;
|
|
240
|
+
}>;
|
|
241
|
+
|
|
242
|
+
export { type CnosWebpackBindings, CnosWebpackPlugin, type CnosWebpackPluginOptions, type TapPromiseHookLike, type WebpackCompilerLike, type WebpackLikeDefinePluginConstructor, type WebpackLikeDefinePluginInstance, loadCnosWebpackEnv, loadCnosWebpackRuntime, resolveCnosWebpackBindings, resolveCnosWebpackBuildConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { resolveBrowserData, resolveFrameworkEnv } from "@kitsy/cnos/build";
|
|
3
|
+
import { createCnos } from "@kitsy/cnos/configure";
|
|
4
|
+
var PLUGIN_NAME = "CnosWebpackPlugin";
|
|
5
|
+
async function loadCnosWebpackEnv(options = {}) {
|
|
6
|
+
return resolveFrameworkEnv(options, "webpack", {
|
|
7
|
+
...options.prefix ? { prefix: options.prefix } : {}
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
async function resolveCnosWebpackBindings(options = {}) {
|
|
11
|
+
const [browserData, publicEnv] = await Promise.all([
|
|
12
|
+
resolveBrowserData(options),
|
|
13
|
+
loadCnosWebpackEnv(options)
|
|
14
|
+
]);
|
|
15
|
+
const definitions = {
|
|
16
|
+
"globalThis.__CNOS_BROWSER_DATA__": JSON.stringify(JSON.stringify(browserData))
|
|
17
|
+
};
|
|
18
|
+
if (options.includeProcessEnvShim ?? true) {
|
|
19
|
+
definitions["process.env.__CNOS_BROWSER_DATA__"] = JSON.stringify(JSON.stringify(browserData));
|
|
20
|
+
}
|
|
21
|
+
for (const [key, value] of Object.entries(publicEnv)) {
|
|
22
|
+
definitions[`process.env.${key}`] = JSON.stringify(value);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
browserData,
|
|
26
|
+
publicEnv,
|
|
27
|
+
definitions
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function createDynamicDefinitions(DefinePlugin, state, keys, options) {
|
|
31
|
+
const definitions = {
|
|
32
|
+
"globalThis.__CNOS_BROWSER_DATA__": DefinePlugin.runtimeValue(
|
|
33
|
+
() => JSON.stringify(JSON.stringify(state.browserData)),
|
|
34
|
+
true
|
|
35
|
+
)
|
|
36
|
+
};
|
|
37
|
+
if (options.includeProcessEnvShim ?? true) {
|
|
38
|
+
definitions["process.env.__CNOS_BROWSER_DATA__"] = DefinePlugin.runtimeValue(
|
|
39
|
+
() => JSON.stringify(JSON.stringify(state.browserData)),
|
|
40
|
+
true
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
for (const key of keys) {
|
|
44
|
+
definitions[`process.env.${key}`] = DefinePlugin.runtimeValue(
|
|
45
|
+
() => JSON.stringify(state.publicEnv[key] ?? ""),
|
|
46
|
+
true
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return definitions;
|
|
50
|
+
}
|
|
51
|
+
var CnosWebpackPlugin = class {
|
|
52
|
+
options;
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
this.options = options;
|
|
55
|
+
}
|
|
56
|
+
apply(compiler) {
|
|
57
|
+
const state = {
|
|
58
|
+
browserData: {},
|
|
59
|
+
publicEnv: {}
|
|
60
|
+
};
|
|
61
|
+
let initialized = false;
|
|
62
|
+
let knownKeys = /* @__PURE__ */ new Set();
|
|
63
|
+
const updateState = async () => {
|
|
64
|
+
const bindings = await resolveCnosWebpackBindings(this.options);
|
|
65
|
+
state.browserData = bindings.browserData;
|
|
66
|
+
state.publicEnv = bindings.publicEnv;
|
|
67
|
+
if (initialized) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
knownKeys = new Set(Object.keys(bindings.publicEnv));
|
|
71
|
+
const definitions = createDynamicDefinitions(
|
|
72
|
+
compiler.webpack.DefinePlugin,
|
|
73
|
+
state,
|
|
74
|
+
[...knownKeys],
|
|
75
|
+
this.options
|
|
76
|
+
);
|
|
77
|
+
new compiler.webpack.DefinePlugin(definitions).apply(compiler);
|
|
78
|
+
initialized = true;
|
|
79
|
+
};
|
|
80
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, async () => {
|
|
81
|
+
await updateState();
|
|
82
|
+
});
|
|
83
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
84
|
+
const wasInitialized = initialized;
|
|
85
|
+
const previousKeys = new Set(knownKeys);
|
|
86
|
+
await updateState();
|
|
87
|
+
if (wasInitialized && Object.keys(state.publicEnv).some((key) => !previousKeys.has(key))) {
|
|
88
|
+
process.stderr.write(
|
|
89
|
+
"[cnos-webpack] New public env keys were detected during watch mode. Restart webpack to apply new define entries.\n"
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
async function loadCnosWebpackRuntime(options = {}) {
|
|
96
|
+
return createCnos(options);
|
|
97
|
+
}
|
|
98
|
+
async function resolveCnosWebpackBuildConfig(options = {}) {
|
|
99
|
+
const [runtime, browserData, publicEnv] = await Promise.all([
|
|
100
|
+
createCnos(options),
|
|
101
|
+
resolveBrowserData(options),
|
|
102
|
+
resolveFrameworkEnv(options, "webpack", {
|
|
103
|
+
...options.prefix ? { prefix: options.prefix } : {}
|
|
104
|
+
})
|
|
105
|
+
]);
|
|
106
|
+
return {
|
|
107
|
+
runtime,
|
|
108
|
+
browserData,
|
|
109
|
+
publicEnv
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
CnosWebpackPlugin,
|
|
114
|
+
loadCnosWebpackEnv,
|
|
115
|
+
loadCnosWebpackRuntime,
|
|
116
|
+
resolveCnosWebpackBindings,
|
|
117
|
+
resolveCnosWebpackBuildConfig
|
|
118
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kitsy/cnos-webpack",
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"description": "Webpack integration for CNOS public config injection.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/kitsyai/cnos.git",
|
|
23
|
+
"directory": "packages/webpack"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/kitsyai/cnos/tree/main/packages/webpack",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/kitsyai/cnos/issues"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"cnos",
|
|
31
|
+
"webpack",
|
|
32
|
+
"config"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@kitsy/cnos": "1.5.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"webpack": ">=5"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
45
|
+
"clean": "rimraf dist",
|
|
46
|
+
"dev": "tsup src/index.ts --watch --format esm,cjs --dts",
|
|
47
|
+
"lint": "eslint src test",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
50
|
+
}
|
|
51
|
+
}
|