@rozenite/vite-plugin 2.2.0 → 2.3.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.
@@ -1,3 +0,0 @@
1
- import { Plugin } from 'vite';
2
- export declare const rozeniteReactNativePlugin: () => Plugin;
3
- //# sourceMappingURL=react-native-plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-native-plugin.d.ts","sourceRoot":"","sources":["../src/react-native-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAG9B,eAAO,MAAM,yBAAyB,QAAO,MA+C5C,CAAC"}
@@ -1,3 +0,0 @@
1
- import { Plugin } from 'vite';
2
- export default function requirePlugin(): Plugin;
3
- //# sourceMappingURL=require-plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"require-plugin.d.ts","sourceRoot":"","sources":["../src/require-plugin.ts"],"names":[],"mappings":"AAGA,OAAO,EAAiB,MAAM,EAAE,MAAM,MAAM,CAAC;AA4F7C,MAAM,CAAC,OAAO,UAAU,aAAa,IAAI,MAAM,CAiH9C"}
@@ -1,3 +0,0 @@
1
- import { Plugin } from 'vite';
2
- export declare const rozeniteSdkPlugin: () => Plugin;
3
- //# sourceMappingURL=sdk-plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sdk-plugin.d.ts","sourceRoot":"","sources":["../src/sdk-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,eAAO,MAAM,iBAAiB,QAAO,MA2CpC,CAAC"}
@@ -1,3 +0,0 @@
1
- import { Plugin } from 'vite';
2
- export declare const rozeniteServerPlugin: () => Plugin;
3
- //# sourceMappingURL=server-plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server-plugin.d.ts","sourceRoot":"","sources":["../src/server-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAInC,eAAO,MAAM,oBAAoB,QAAO,MAkCvC,CAAC"}
package/src/bundle-dts.ts DELETED
@@ -1,117 +0,0 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { Extractor, ExtractorConfig, ExtractorLogLevel } from '@microsoft/api-extractor';
4
-
5
- type Target = 'react-native' | 'metro' | 'sdk';
6
-
7
- export const normalizeRolledUpDeclarations = (content: string) => {
8
- const valueDeclarationNames = new Set<string>();
9
-
10
- content.replace(
11
- /^\s*declare\s+(?:const|let|var|function)\s+([A-Za-z_$][\w$]*)\b/gm,
12
- (_, name: string) => {
13
- valueDeclarationNames.add(name);
14
- return _;
15
- },
16
- );
17
-
18
- const normalizeTypeReference = (name: string) =>
19
- valueDeclarationNames.has(name) ? `typeof ${name}` : name;
20
-
21
- return content
22
- .replace(
23
- /^(\s*(?:export\s+)?declare\s+(?:const|let|var)\s+[A-Za-z_$][\w$]*\s*:\s*)([A-Za-z_$][\w$]*)(\s*;)$/gm,
24
- (_, prefix: string, name: string, suffix: string) => {
25
- return `${prefix}${normalizeTypeReference(name)}${suffix}`;
26
- },
27
- )
28
- .replace(
29
- /^(\s*(?:export\s+)?declare\s+type\s+[A-Za-z_$][\w$]*\s*=\s*)([A-Za-z_$][\w$]*)(\s*;)$/gm,
30
- (_, prefix: string, name: string, suffix: string) => {
31
- return `${prefix}${normalizeTypeReference(name)}${suffix}`;
32
- },
33
- );
34
- };
35
-
36
- export const bundleTargetDeclarations = async (projectRoot: string, target: Target) => {
37
- const targetRoot = path.join(projectRoot, 'dist', target);
38
- const publicEntryPath = path.join(targetRoot, 'index.d.ts');
39
- const bundleEntryPath =
40
- target === 'sdk' ? path.join(targetRoot, `${target}.d.ts`) : publicEntryPath;
41
- const entryPath = await fs
42
- .access(bundleEntryPath)
43
- .then(() => bundleEntryPath)
44
- .catch(() => publicEntryPath);
45
- const tempOutputPath = path.join(targetRoot, 'index.public.d.ts');
46
- const srcDeclarationsPath = path.join(targetRoot, 'src');
47
- const configObjectFullPath = path.join(projectRoot, 'api-extractor.json');
48
- const packageJsonFullPath = path.join(projectRoot, 'package.json');
49
- const tsconfigFilePath = path.join(projectRoot, 'tsconfig.json');
50
-
51
- const extractorConfig = ExtractorConfig.prepare({
52
- configObject: {
53
- projectFolder: projectRoot,
54
- mainEntryPointFilePath: entryPath,
55
- compiler: {
56
- tsconfigFilePath,
57
- overrideTsconfig: {
58
- $schema: 'http://json.schemastore.org/tsconfig',
59
- compilerOptions: {
60
- skipLibCheck: true,
61
- },
62
- },
63
- },
64
- apiReport: {
65
- enabled: false,
66
- },
67
- docModel: {
68
- enabled: false,
69
- },
70
- dtsRollup: {
71
- enabled: true,
72
- publicTrimmedFilePath: tempOutputPath,
73
- },
74
- tsdocMetadata: {
75
- enabled: false,
76
- },
77
- messages: {
78
- compilerMessageReporting: {
79
- default: {
80
- logLevel: ExtractorLogLevel.None,
81
- },
82
- },
83
- extractorMessageReporting: {
84
- default: {
85
- logLevel: ExtractorLogLevel.None,
86
- },
87
- },
88
- },
89
- },
90
- configObjectFullPath,
91
- packageJsonFullPath,
92
- });
93
-
94
- const result = Extractor.invoke(extractorConfig, {
95
- localBuild: true,
96
- showVerboseMessages: false,
97
- showDiagnostics: false,
98
- });
99
-
100
- if (!result.succeeded) {
101
- throw new Error(`Failed to bundle ${target} declaration files.`);
102
- }
103
-
104
- if (entryPath !== publicEntryPath) {
105
- await fs.rm(publicEntryPath, { force: true });
106
- }
107
- await fs.rm(entryPath, { force: true });
108
- await fs.rename(tempOutputPath, publicEntryPath);
109
- const bundledContent = await fs.readFile(publicEntryPath, 'utf8');
110
- const normalizedContent = normalizeRolledUpDeclarations(bundledContent);
111
-
112
- if (normalizedContent !== bundledContent) {
113
- await fs.writeFile(publicEntryPath, normalizedContent);
114
- }
115
-
116
- await fs.rm(srcDeclarationsPath, { recursive: true, force: true });
117
- };
@@ -1,51 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import path from 'node:path';
3
-
4
- export const rozeniteReactNativePlugin = (): Plugin => {
5
- return {
6
- name: 'rozenite-react-native-plugin',
7
- config(config) {
8
- const projectRoot = config.root ?? process.cwd();
9
-
10
- config.build ??= {};
11
- if (process.env.ROZENITE_BUILD === '1') {
12
- config.build.emptyOutDir = false;
13
- }
14
- config.build.rollupOptions ??= {};
15
-
16
- config.build.lib = {
17
- entry: path.resolve(projectRoot, 'react-native.ts'),
18
- fileName: (format) => `react-native/index.${format === 'es' ? 'js' : 'cjs'}`,
19
- };
20
-
21
- config.build.rollupOptions.external = (id) => {
22
- if (id.startsWith('node:')) {
23
- return true;
24
- }
25
-
26
- return !id.startsWith('.') && !path.isAbsolute(id);
27
- };
28
-
29
- config.build.rollupOptions.output = [
30
- {
31
- format: 'es',
32
- exports: 'named',
33
- interop: 'auto',
34
- entryFileNames: 'react-native/index.js',
35
- chunkFileNames: 'react-native/chunks/[name].js',
36
- ...(config.build.rollupOptions.output ?? {}),
37
- },
38
- {
39
- format: 'cjs',
40
- exports: 'named',
41
- interop: 'auto',
42
- entryFileNames: 'react-native/index.cjs',
43
- chunkFileNames: 'react-native/chunks/[name].cjs',
44
- ...(config.build.rollupOptions.output ?? {}),
45
- },
46
- ];
47
-
48
- delete config.build.rollupOptions.input;
49
- },
50
- };
51
- };
@@ -1,209 +0,0 @@
1
- import assert from 'node:assert';
2
- import path from 'node:path';
3
- import { readFileSync } from 'node:fs';
4
- import { normalizePath, Plugin } from 'vite';
5
-
6
- const REQUIRE_REGEX = /require\s*\(\s*['"`]([^'"`]+)['"`]\s*\)/g;
7
- const IMPORT_PREFIX = '__import_';
8
- const VIRTUAL_REQUIRE_PREFIX = '\0virtual:rozenite-rn-require:';
9
- const REQUIRE_WRAPPER_SUFFIX = '.require';
10
-
11
- interface ModuleInfo {
12
- referenceId: string;
13
- virtualId: string;
14
- }
15
-
16
- interface TransformResult {
17
- code: string;
18
- }
19
-
20
- const extractModuleName = (filePath: string): string => {
21
- return path.basename(filePath).replace(/\.[^/.]+$/, '');
22
- };
23
-
24
- const sanitizeChunkName = (value: string): string => {
25
- return value.replace(/[^a-zA-Z0-9_-]/g, '-');
26
- };
27
-
28
- const findRequireStatements = (code: string): Set<string> => {
29
- const requires = new Set<string>();
30
- let match: RegExpExecArray | null;
31
-
32
- REQUIRE_REGEX.lastIndex = 0;
33
-
34
- while ((match = REQUIRE_REGEX.exec(code)) !== null) {
35
- const moduleName = match[1];
36
- if (moduleName && moduleName.trim()) {
37
- requires.add(moduleName.trim());
38
- }
39
- }
40
-
41
- return requires;
42
- };
43
-
44
- const transformRequireToImports = (
45
- code: string,
46
- moduleInfoMap: Map<string, ModuleInfo>,
47
- ): TransformResult => {
48
- const imports: string[] = [];
49
- const importMap = new Map<string, string>();
50
- const requires = findRequireStatements(code);
51
-
52
- requires.forEach((moduleName, index) => {
53
- const moduleInfo = moduleInfoMap.get(moduleName);
54
-
55
- if (!moduleInfo) {
56
- return;
57
- }
58
-
59
- const importName = `${IMPORT_PREFIX}${index}`;
60
- importMap.set(moduleName, importName);
61
- imports.push(`import * as ${importName} from '${moduleInfo.virtualId}';`);
62
- });
63
-
64
- let transformedCode = code.replace(REQUIRE_REGEX, (match, moduleName) => {
65
- const importName = importMap.get(moduleName.trim());
66
- return importName || match;
67
- });
68
-
69
- if (imports.length > 0) {
70
- transformedCode = imports.join('\n') + '\n' + transformedCode;
71
- }
72
-
73
- return { code: transformedCode };
74
- };
75
-
76
- const transformRequireToChunkReferences = (
77
- code: string,
78
- moduleInfoMap: Map<string, ModuleInfo>,
79
- getFileName: (referenceId: string) => string,
80
- ): string => {
81
- return code.replace(REQUIRE_REGEX, (match, moduleName) => {
82
- const moduleInfo = moduleInfoMap.get(moduleName.trim());
83
-
84
- if (!moduleInfo) {
85
- return match;
86
- }
87
-
88
- const outFileName = getFileName(moduleInfo.referenceId);
89
- const relPath = normalizePath(path.posix.relative('react-native', outFileName));
90
- const requirePath = relPath.startsWith('.') ? relPath : `./${relPath}`;
91
-
92
- return `require('${requirePath}')`;
93
- });
94
- };
95
-
96
- export default function requirePlugin(): Plugin {
97
- let input = '';
98
- let inputName = '';
99
- let isDevMode = false;
100
-
101
- const moduleInfoMap = new Map<string, ModuleInfo>();
102
- const virtualModuleSources = new Map<string, string>();
103
-
104
- return {
105
- name: 'vite-require-plugin',
106
-
107
- configResolved(config) {
108
- isDevMode = config.command === 'serve';
109
- },
110
-
111
- resolveId(id) {
112
- if (virtualModuleSources.has(id)) {
113
- return id;
114
- }
115
-
116
- return null;
117
- },
118
-
119
- load(id) {
120
- return virtualModuleSources.get(id) ?? null;
121
- },
122
-
123
- transform(code, id) {
124
- if (!isDevMode || id !== input) {
125
- return null;
126
- }
127
-
128
- try {
129
- const result = transformRequireToImports(code, moduleInfoMap);
130
-
131
- return {
132
- code: result.code,
133
- map: null,
134
- };
135
- } catch (error) {
136
- console.error('Error transforming require statements:', error);
137
- return null;
138
- }
139
- },
140
-
141
- async buildStart(options) {
142
- try {
143
- assert(Array.isArray(options.input), 'input must be an array');
144
- assert(options.input.length === 1, 'input must be an array with one entry');
145
-
146
- input = options.input[0];
147
- inputName = extractModuleName(input);
148
- moduleInfoMap.clear();
149
- virtualModuleSources.clear();
150
-
151
- const code = readFileSync(input, 'utf-8');
152
- const requires = findRequireStatements(code);
153
-
154
- for (const req of requires) {
155
- try {
156
- const resolved = await this.resolve(req, input, { skipSelf: true });
157
-
158
- if (!resolved) {
159
- console.warn(`Could not resolve module: ${req}`);
160
- continue;
161
- }
162
-
163
- this.addWatchFile(resolved.id);
164
-
165
- const exportName = sanitizeChunkName(extractModuleName(resolved.id));
166
- const wrapperName = `${exportName}${REQUIRE_WRAPPER_SUFFIX}`;
167
- const virtualId = `${VIRTUAL_REQUIRE_PREFIX}${wrapperName}`;
168
-
169
- virtualModuleSources.set(virtualId, `export * from ${JSON.stringify(req)};`);
170
-
171
- const referenceId = this.emitFile({
172
- type: 'chunk',
173
- id: virtualId,
174
- name: wrapperName,
175
- });
176
-
177
- moduleInfoMap.set(req, {
178
- referenceId,
179
- virtualId,
180
- });
181
- } catch (error) {
182
- console.error(`Error resolving module ${req}:`, error);
183
- }
184
- }
185
- } catch (error) {
186
- console.error('Error in buildStart:', error);
187
- throw error;
188
- }
189
- },
190
-
191
- renderChunk(code, chunk) {
192
- try {
193
- if (chunk.name !== inputName) {
194
- return null;
195
- }
196
-
197
- return {
198
- code: transformRequireToChunkReferences(code, moduleInfoMap, (referenceId) =>
199
- normalizePath(this.getFileName(referenceId)),
200
- ),
201
- map: null,
202
- };
203
- } catch (error) {
204
- console.error('Error in renderChunk:', error);
205
- return null;
206
- }
207
- },
208
- };
209
- }
package/src/sdk-plugin.ts DELETED
@@ -1,47 +0,0 @@
1
- import type { Plugin } from 'vite';
2
- import path from 'node:path';
3
-
4
- export const rozeniteSdkPlugin = (): Plugin => {
5
- return {
6
- name: 'rozenite-sdk-plugin',
7
- config(config) {
8
- const projectRoot = config.root ?? process.cwd();
9
-
10
- config.build ??= {};
11
- if (process.env.ROZENITE_BUILD === '1') {
12
- config.build.emptyOutDir = false;
13
- }
14
- config.build.lib = {
15
- entry: path.resolve(projectRoot, 'sdk.ts'),
16
- formats: ['es' as const, 'cjs' as const],
17
- fileName: (format) => `sdk/index.${format === 'es' ? 'js' : 'cjs'}`,
18
- };
19
- config.build.rollupOptions ??= {};
20
- config.build.rollupOptions.external = (id) => {
21
- if (id.startsWith('node:')) {
22
- return true;
23
- }
24
-
25
- return !id.startsWith('.') && !path.isAbsolute(id);
26
- };
27
- config.build.rollupOptions.output = [
28
- {
29
- format: 'es',
30
- exports: 'named',
31
- interop: 'auto',
32
- entryFileNames: 'sdk/index.js',
33
- chunkFileNames: 'sdk/chunks/[name].js',
34
- },
35
- {
36
- format: 'cjs',
37
- exports: 'named',
38
- interop: 'auto',
39
- entryFileNames: 'sdk/index.cjs',
40
- chunkFileNames: 'sdk/chunks/[name].cjs',
41
- },
42
- ];
43
-
44
- delete config.build.rollupOptions.input;
45
- },
46
- };
47
- };
@@ -1,39 +0,0 @@
1
- import type { Plugin } from 'vite';
2
- import process from 'node:process';
3
- import path from 'node:path';
4
-
5
- export const rozeniteServerPlugin = (): Plugin => {
6
- return {
7
- name: 'rozenite-server-plugin',
8
-
9
- config(config) {
10
- const projectRoot = config.root ?? process.cwd();
11
-
12
- config.build ??= {};
13
- if (process.env.ROZENITE_BUILD === '1') {
14
- config.build.emptyOutDir = false;
15
- }
16
- config.build.lib = {
17
- entry: path.resolve(projectRoot, 'metro.ts'),
18
- formats: ['es' as const, 'cjs' as const],
19
- fileName: (format) => `metro/index.${format === 'es' ? 'js' : 'cjs'}`,
20
- };
21
- config.build.ssr = true;
22
- config.build.rollupOptions ??= {};
23
- config.build.rollupOptions.output = [
24
- {
25
- format: 'es',
26
- entryFileNames: 'metro/index.js',
27
- exports: 'named',
28
- interop: 'auto',
29
- },
30
- {
31
- format: 'cjs',
32
- entryFileNames: 'metro/index.cjs',
33
- exports: 'named',
34
- interop: 'auto',
35
- },
36
- ];
37
- },
38
- };
39
- };