@open-xchange/vite-plugin-replace-pkg 1.1.1 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## `1.2.0` – 2026-Mar-13
4
+
5
+ - added: support for Vite 8
6
+
3
7
  ## `1.1.1` – 2025-Sep-09
4
8
 
5
9
  - chore: bump dependencies
@@ -37,7 +41,7 @@
37
41
 
38
42
  - added: options `prepend`, `append`, `exportGlobal`
39
43
  - changed: made option `replace.token` optional (defaults to `GITLAB_TOKEN` env var)
40
- - fixed: inactive log level "info"
44
+ - fixed: inactive log level 'info'
41
45
 
42
46
  ## `0.0.2` – 2024-Aug-07
43
47
 
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Open-Xchange GmbH, Germany <info@open-xchange.com>
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 CHANGED
@@ -11,14 +11,14 @@ A common use case is replacing a public package with an internally hosted commer
11
11
  ```ts
12
12
  // vite.config.ts
13
13
 
14
- import { defineConfig } from "vite" // or "vitest/config"
15
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
14
+ import { defineConfig } from 'vite' // or 'vitest/config'
15
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
16
16
 
17
17
  export default defineConfig(() => {
18
18
  plugins: [
19
19
  replacePlugin({
20
- package: "external-package",
21
- replace: "https://example.org/path/to/index.js",
20
+ package: 'external-package',
21
+ replace: 'https://example.org/path/to/index.js',
22
22
  }),
23
23
  ],
24
24
  })
@@ -27,7 +27,7 @@ export default defineConfig(() => {
27
27
  ```ts
28
28
  // src/index.ts
29
29
 
30
- import * as ext from "external-package" // imports replacement if available
30
+ import * as ext from 'external-package' // imports replacement if available
31
31
  ```
32
32
 
33
33
  It is possible to replace different packages by instantiating this plugin multiple times:
@@ -35,17 +35,17 @@ It is possible to replace different packages by instantiating this plugin multip
35
35
  ```ts
36
36
  // vite.config.ts
37
37
 
38
- import { defineConfig } from "vite" // or "vitest/config"
39
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
38
+ import { defineConfig } from 'vite' // or 'vitest/config'
39
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
40
40
 
41
41
  export default defineConfig(() => {
42
42
  plugins: [
43
43
  replacePlugin({
44
- package: "external-package",
45
- replace: "https://example.org/path/to/index.js",
44
+ package: 'external-package',
45
+ replace: 'https://example.org/path/to/index.js',
46
46
  }),
47
47
  replacePlugin({
48
- package: "@namespace/another-package",
48
+ package: '@namespace/another-package',
49
49
  replace: { /* GitLab project file */ },
50
50
  }),
51
51
  ],
@@ -82,17 +82,17 @@ The location of the source file to resolve the module imports with. There are di
82
82
  ```ts
83
83
  // vite.config.ts
84
84
 
85
- import { defineConfig } from "vite" // or "vitest/config"
86
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
85
+ import { defineConfig } from 'vite' // or 'vitest/config'
86
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
87
87
 
88
88
  export default defineConfig(() => {
89
89
  plugins: [
90
90
  replacePlugin({
91
- package: "external-package",
91
+ package: 'external-package',
92
92
  replace: {
93
- method: "POST",
94
- headers: { "X-Some-Header", "abc" },
95
- url: "https://example.org/api/to/endpoint",
93
+ method: 'POST',
94
+ headers: { 'X-Some-Header', 'abc' },
95
+ url: 'https://example.org/api/to/endpoint',
96
96
  query: { ts: Date.now() },
97
97
  },
98
98
  }),
@@ -105,7 +105,7 @@ The location of the source file to resolve the module imports with. There are di
105
105
  | Name | Type | Default | Description |
106
106
  | - | - | - | - |
107
107
  | `server` | `string` | _required_ | The URL of the GitLab server to be used for downloading the source file. |
108
- | `project` | `string\|number` | _required_ | Name or numeric identifier of the GitLab project. The project name may contain slashes (e.g. `"my-group/my-project"`) which will be encoded internally. |
108
+ | `project` | `string\|number` | _required_ | Name or numeric identifier of the GitLab project. The project name may contain slashes (e.g. `'my-group/my-project'`) which will be encoded internally. |
109
109
  | `path` | `string` | _required_ | Path to the source file to be downloaded. Slashes in the path will be encoded internally. |
110
110
  | `ref` | `string` | `HEAD` | The name of a branch or tag, or a commit ID. Slashes in branch names will be encoded internally. |
111
111
  | `token` | `string` | environment variable `GITLAB_TOKEN` | A valid GitLab API access token. If omitted, uses the contents of the environment variable `GITLAB_TOKEN`. If the resulting token is empty, downloading the source file will be skipped silently. |
@@ -115,18 +115,18 @@ The location of the source file to resolve the module imports with. There are di
115
115
  ```ts
116
116
  // vite.config.ts
117
117
 
118
- import { defineConfig } from "vite" // or "vitest/config"
119
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
118
+ import { defineConfig } from 'vite' // or 'vitest/config'
119
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
120
120
 
121
121
  export default defineConfig(() => {
122
122
  plugins: [
123
123
  replacePlugin({
124
- package: "external-package",
124
+ package: 'external-package',
125
125
  replace: {
126
- server: "https://gitlab.example.org",
127
- project: "internals/commercial-package",
128
- path: "path/to/index.js",
129
- ref: "feature/branch-2",
126
+ server: 'https://gitlab.example.org',
127
+ project: 'internals/commercial-package',
128
+ path: 'path/to/index.js',
129
+ ref: 'feature/branch-2',
130
130
  },
131
131
  }),
132
132
  ],
@@ -140,13 +140,13 @@ The location of the source file to resolve the module imports with. There are di
140
140
  ```ts
141
141
  // vite.config.ts
142
142
 
143
- import { defineConfig } from "vite" // or "vitest/config"
144
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
143
+ import { defineConfig } from 'vite' // or 'vitest/config'
144
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
145
145
 
146
146
  export default defineConfig(() => {
147
147
  plugins: [
148
148
  replacePlugin({
149
- package: "external-package",
149
+ package: 'external-package',
150
150
  async replace() { /* ... */ },
151
151
  }),
152
152
  ],
@@ -165,14 +165,14 @@ Example:
165
165
  ```ts
166
166
  // vite.config.ts
167
167
 
168
- import { defineConfig } from "vite" // or "vitest/config"
169
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
168
+ import { defineConfig } from 'vite' // or 'vitest/config'
169
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
170
170
 
171
171
  export default defineConfig(() => {
172
172
  plugins: [
173
173
  replacePlugin({
174
- package: "external-package"
175
- replace: "https://example.org/path/to/index.js",
174
+ package: 'external-package'
175
+ replace: 'https://example.org/path/to/index.js',
176
176
  transform: source => source.replaceAll(/* ... */)
177
177
  }),
178
178
  ],
@@ -193,14 +193,14 @@ Example:
193
193
  ```ts
194
194
  // vite.config.ts
195
195
 
196
- import { defineConfig } from "vite" // or "vitest/config"
197
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
196
+ import { defineConfig } from 'vite' // or 'vitest/config'
197
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
198
198
 
199
199
  export default defineConfig(() => {
200
200
  plugins: [
201
201
  replacePlugin({
202
- package: "external-package"
203
- replace: "https://example.org/path/to/index.js",
202
+ package: 'external-package'
203
+ replace: 'https://example.org/path/to/index.js',
204
204
  prepend: [/* ... */],
205
205
  }),
206
206
  ],
@@ -221,14 +221,14 @@ Example:
221
221
  ```ts
222
222
  // vite.config.ts
223
223
 
224
- import { defineConfig } from "vite" // or "vitest/config"
225
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
224
+ import { defineConfig } from 'vite' // or 'vitest/config'
225
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
226
226
 
227
227
  export default defineConfig(() => {
228
228
  plugins: [
229
229
  replacePlugin({
230
- package: "external-package"
231
- replace: "https://example.org/path/to/index.js",
230
+ package: 'external-package'
231
+ replace: 'https://example.org/path/to/index.js',
232
232
  append: [/* ... */],
233
233
  }),
234
234
  ],
@@ -249,15 +249,15 @@ Example:
249
249
  ```ts
250
250
  // vite.config.ts
251
251
 
252
- import { defineConfig } from "vite" // or "vitest/config"
253
- import replacePlugin from "@open-xchange/vite-plugin-replace-pkg"
252
+ import { defineConfig } from 'vite' // or 'vitest/config'
253
+ import replacePlugin from '@open-xchange/vite-plugin-replace-pkg'
254
254
 
255
255
  export default defineConfig(() => {
256
256
  plugins: [
257
257
  replacePlugin({
258
- package: "external-package"
259
- replace: "https://example.org/path/to/index.js",
260
- exportGlobal: "TheModule",
258
+ package: 'external-package'
259
+ replace: 'https://example.org/path/to/index.js',
260
+ exportGlobal: 'TheModule',
261
261
  }),
262
262
  ],
263
263
  })
@@ -270,12 +270,12 @@ is equivalent to
270
270
 
271
271
  // ...
272
272
  replacePlugin({
273
- package: "external-package"
274
- replace: "https://example.org/path/to/index.js",
273
+ package: 'external-package'
274
+ replace: 'https://example.org/path/to/index.js',
275
275
  append: [
276
- "const __contents = globalThis.TheModule",
277
- "delete globalThis.TheModule",
278
- "export default __contents",
276
+ 'const __contents = globalThis.TheModule',
277
+ 'delete globalThis.TheModule',
278
+ 'export default __contents',
279
279
  ],
280
280
  }),
281
281
  ```
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Plugin } from "vite";
2
- import type { Dict } from "@open-xchange/vite-helper/utils";
1
+ import type { Plugin } from 'vite';
2
+ import type { Dict } from '@open-xchange/vite-helper/utils';
3
3
  /**
4
4
  * The configuration options for the `fetch` function needed to download a
5
5
  * source file.
@@ -24,7 +24,7 @@ export interface VitePluginReplacePkg_GitLabFile {
24
24
  server: string;
25
25
  /**
26
26
  * Name or identifier of the GitLab project (repository). The project name
27
- * may contain slashes (e.g. "my-group/my-project") which will be encoded
27
+ * may contain slashes (e.g. 'my-group/my-project') which will be encoded
28
28
  * internally.
29
29
  */
30
30
  project: string | number;
@@ -37,7 +37,7 @@ export interface VitePluginReplacePkg_GitLabFile {
37
37
  * The name of a branch or a tag, or a commit ID. Slashes in branch names
38
38
  * will be encoded internally.
39
39
  *
40
- * @default "HEAD"
40
+ * @default 'HEAD'
41
41
  */
42
42
  ref?: string;
43
43
  /**
@@ -61,10 +61,10 @@ export interface VitePluginReplacePkgOptions {
61
61
  */
62
62
  package: string;
63
63
  /**
64
- * The location of the source file to resolve the module import with. Can
65
- * be a simple URL as string, a configuration object for the `fetch`
66
- * function (with URL, headers, etc.), a specification for a file in a
67
- * GitLab project repository, or a custom callback function.
64
+ * The location of the source file to resolve the module import with. Can be
65
+ * a simple URL as string, a configuration object for the `fetch` function
66
+ * (with URL, headers, etc.), a specification for a file in a GitLab project
67
+ * repository, or a custom callback function.
68
68
  */
69
69
  replace: string | VitePluginReplacePkg_RequestInit | VitePluginReplacePkg_GitLabFile | VitePluginReplacePkg_Callback;
70
70
  /**
@@ -79,25 +79,24 @@ export interface VitePluginReplacePkgOptions {
79
79
  */
80
80
  transform?: (source: string) => string | string[] | Promise<string | string[]>;
81
81
  /**
82
- * Inserts arbitrary source code to the beginning of the replacement
83
- * module. An array of strings will be concatenated with newline
84
- * characters. This option will be applied after source code transformation
85
- * (option `transform`).
82
+ * Inserts arbitrary source code to the beginning of the replacement module.
83
+ * An array of strings will be concatenated with newline characters. This
84
+ * option will be applied after source code transformation (option
85
+ * `transform`).
86
86
  */
87
87
  prepend?: string | string[];
88
88
  /**
89
89
  * Inserts arbitrary source code to the end of the replacement module. An
90
- * array of strings will be concatenated with newline characters. This
91
- * option will be applied after source code transformation (option
92
- * `transform`).
90
+ * array of strings will be concatenated with newline characters. This option
91
+ * will be applied after source code transformation (option `transform`).
93
92
  */
94
93
  append?: string | string[];
95
94
  /**
96
95
  * Specifies the name of a global variable that will be defined by the
97
- * replacement module. This plugin will append source code to the
98
- * replacement module that will delete the variable from global scope, and
99
- * will export it as the default export instead. This option will be
100
- * applied after source code transformation (option `transform`).
96
+ * replacement module. This plugin will append source code to the replacement
97
+ * module that will delete the variable from global scope, and will export it
98
+ * as the default export instead. This option will be applied after source
99
+ * code transformation (option `transform`).
101
100
  */
102
101
  exportGlobal?: string;
103
102
  }
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { onceFn } from "@open-xchange/vite-helper/utils";
2
- import { Logger } from "@open-xchange/vite-helper/logger";
1
+ import { onceFn } from '@open-xchange/vite-helper/utils';
2
+ import { Logger } from '@open-xchange/vite-helper/logger';
3
3
  // constants ==================================================================
4
- export const PROJECT_NAME = "@open-xchange/vite-plugin-replace-pkg";
4
+ export const PROJECT_NAME = '@open-xchange/vite-plugin-replace-pkg';
5
5
  // functions ==================================================================
6
6
  /**
7
7
  * Joins a string array with newline characters, ensures trailing newline.
@@ -13,8 +13,8 @@ export const PROJECT_NAME = "@open-xchange/vite-plugin-replace-pkg";
13
13
  * The flattened contents.
14
14
  */
15
15
  function flatten(contents) {
16
- const result = Array.isArray(contents) ? contents.join("\n") : contents;
17
- return result.endsWith("\n") ? result : (result + "\n");
16
+ const result = Array.isArray(contents) ? contents.join('\n') : contents;
17
+ return result.endsWith('\n') ? result : (result + '\n');
18
18
  }
19
19
  // plugin =====================================================================
20
20
  /**
@@ -31,34 +31,34 @@ function flatten(contents) {
31
31
  */
32
32
  export default function vitePluginReplacePkg(options) {
33
33
  const pkgName = options.package;
34
- const moduleId = "\0" + PROJECT_NAME + "/" + pkgName;
34
+ const moduleId = '\0' + PROJECT_NAME + '/' + pkgName;
35
35
  // must not be instantiated globally (needs initialized environment variables)
36
36
  const logger = new Logger({
37
- loggerPrefix: "pkg",
38
- logLevelEnvVar: "PLUGIN_REPLACE_PKG_LOGLEVEL",
37
+ loggerPrefix: 'pkg',
38
+ logLevelEnvVar: 'PLUGIN_REPLACE_PKG_LOGLEVEL',
39
39
  });
40
40
  // resolves the URL of the replacement module to be fetched
41
41
  const resolveFetchOptions = () => {
42
- logger.info("resolving replacement module for %f", pkgName);
42
+ logger.info('resolving replacement module for %f', pkgName);
43
43
  const { replace } = options;
44
44
  // callback function
45
- if (typeof replace === "function") {
45
+ if (typeof replace === 'function') {
46
46
  return;
47
47
  }
48
48
  // plain string
49
- if (typeof replace === "string") {
49
+ if (typeof replace === 'string') {
50
50
  return replace ? { url: replace } : undefined;
51
51
  }
52
52
  // `RequestInit` object
53
- if ("url" in replace) {
53
+ if ('url' in replace) {
54
54
  return replace.url ? replace : undefined;
55
55
  }
56
56
  // `GitLabFile` object
57
- if ("project" in replace) {
57
+ if ('project' in replace) {
58
58
  // GitLab API token
59
59
  const token = replace.token ?? process.env.GITLAB_TOKEN;
60
60
  if (!token) {
61
- logger.warn("missing GitLab API token");
61
+ logger.warn('missing GitLab API token');
62
62
  return;
63
63
  }
64
64
  // build the file URL for the GitLab API
@@ -71,23 +71,23 @@ export default function vitePluginReplacePkg(options) {
71
71
  if (replace.ref) {
72
72
  query.ref = replace.ref;
73
73
  }
74
- const headers = { "PRIVATE-TOKEN": token };
74
+ const headers = { 'PRIVATE-TOKEN': token };
75
75
  return { url, headers, query };
76
76
  }
77
- logger.error("invalid configuration for package %f", pkgName);
77
+ logger.error('invalid configuration for package %f', pkgName);
78
78
  return undefined;
79
79
  };
80
- // fetches the replacement module ("onceFn" wrapped to be called from "resolveId" and "load")
80
+ // fetches the replacement module ('onceFn' wrapped to be called from 'resolveId' and 'load')
81
81
  const fetchSourceFile = onceFn(async () => {
82
82
  try {
83
- if (typeof options.replace === "function") {
83
+ if (typeof options.replace === 'function') {
84
84
  return await options.replace(pkgName);
85
85
  }
86
86
  const fetchOptions = resolveFetchOptions();
87
87
  if (fetchOptions) {
88
- const query = Object.entries(fetchOptions.query ?? {}).map(entry => entry.map(encodeURIComponent).join("=")).join("&");
89
- const url = fetchOptions.url + (query ? `?${query}` : "");
90
- logger.info("requesting package %f from %f", pkgName, url);
88
+ const query = Object.entries(fetchOptions.query ?? {}).map(entry => entry.map(encodeURIComponent).join('=')).join('&');
89
+ const url = fetchOptions.url + (query ? `?${query}` : '');
90
+ logger.info('requesting package %f from %f', pkgName, url);
91
91
  const response = await fetch(url, fetchOptions);
92
92
  if (response.ok) {
93
93
  return await response.text();
@@ -95,24 +95,24 @@ export default function vitePluginReplacePkg(options) {
95
95
  logger.warn(`requesting package %f failed: ${response.status} ${response.statusText}`, pkgName);
96
96
  }
97
97
  else {
98
- logger.info("no replacement available for package %f", pkgName);
98
+ logger.info('no replacement available for package %f', pkgName);
99
99
  }
100
100
  }
101
101
  catch (error) {
102
102
  logger.warn(`requesting package %f failed: ${error.message}`, pkgName);
103
103
  }
104
- return "";
104
+ return '';
105
105
  });
106
106
  // create and return the plugin object
107
107
  return {
108
108
  name: PROJECT_NAME,
109
109
  resolveId: {
110
- order: "pre",
110
+ order: 'pre',
111
111
  async handler(source) {
112
112
  if (source !== pkgName) {
113
113
  return;
114
114
  }
115
- // try to fetch source file, skip "load" hook if not available
115
+ // try to fetch source file, skip 'load' hook if not available
116
116
  const contents = await fetchSourceFile();
117
117
  return contents ? moduleId : undefined;
118
118
  },
@@ -134,9 +134,9 @@ export default function vitePluginReplacePkg(options) {
134
134
  }
135
135
  if (exportGlobal) {
136
136
  contents += flatten([
137
- `const __contents = globalThis["${exportGlobal}"];`,
138
- `delete globalThis["${exportGlobal}"];`,
139
- "export default __contents;",
137
+ `const __contents = globalThis['${exportGlobal}']`,
138
+ `delete globalThis['${exportGlobal}']`,
139
+ 'export default __contents',
140
140
  ]);
141
141
  }
142
142
  return contents;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-xchange/vite-plugin-replace-pkg",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Vite plugin replacing a specific external package with an arbitrary source code module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,32 +11,25 @@
11
11
  "engines": {
12
12
  "node": ">=20.18"
13
13
  },
14
- "packageManager": "yarn@4.9.4",
15
14
  "type": "module",
16
15
  "exports": "./dist/index.js",
17
16
  "files": [
18
17
  "dist",
19
18
  "CHANGELOG.*"
20
19
  ],
21
- "scripts": {
22
- "prepack": "yarn build && yarn lint",
23
- "clean": "premove dist",
24
- "build": "yarn clean && tsc --project src/tsconfig.json",
25
- "lint": "tsc && tsc --project src/tsconfig.json --noEmit && eslint ."
26
- },
27
20
  "dependencies": {
28
- "@open-xchange/vite-helper": "^1.1.1"
21
+ "@open-xchange/vite-helper": "^1.2.2"
29
22
  },
30
23
  "devDependencies": {
31
- "@open-xchange/linter-presets": "^1.9.0",
32
- "@types/node": "^24.3.1",
33
- "eslint": "^9.35.0",
34
- "jiti": "^2.5.1",
35
- "premove": "^4.0.0",
36
- "typescript": "^5.9.2",
37
- "vite": "^7.1.5"
24
+ "vite": "^8.0.0",
25
+ "@open-xchange/linter-presets": "^1.18.4"
38
26
  },
39
27
  "peerDependencies": {
40
- "vite": "^6.0.0 || ^7.0.0"
28
+ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
29
+ },
30
+ "scripts": {
31
+ "build": "pnpm clean && tsc --project src/tsconfig.json",
32
+ "clean": "premove dist",
33
+ "lint": "tsc && tsc --project src/tsconfig.json --noEmit && eslint ."
41
34
  }
42
35
  }