@nativescript/vite 0.0.1-alpha.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/dist/configuration/angular.d.ts +4 -0
- package/dist/configuration/angular.js +30 -0
- package/dist/configuration/base.d.ts +4 -0
- package/dist/configuration/base.js +270 -0
- package/dist/configuration/old-without-merge-base.d.ts +13 -0
- package/dist/configuration/old-without-merge-base.js +249 -0
- package/dist/configuration/react.d.ts +4 -0
- package/dist/configuration/react.js +85 -0
- package/dist/configuration/solid.d.ts +4 -0
- package/dist/configuration/solid.js +48 -0
- package/dist/configuration/vue.d.ts +4 -0
- package/dist/configuration/vue.js +45 -0
- package/dist/helpers/commonjs-plugins.d.ts +6 -0
- package/dist/helpers/commonjs-plugins.js +75 -0
- package/dist/helpers/config-as-json.d.ts +2 -0
- package/dist/helpers/config-as-json.js +35 -0
- package/dist/helpers/css-tree.d.ts +4 -0
- package/dist/helpers/css-tree.js +21 -0
- package/dist/helpers/dynamic-import-plugin.d.ts +4 -0
- package/dist/helpers/dynamic-import-plugin.js +62 -0
- package/dist/helpers/external-configs.d.ts +6 -0
- package/dist/helpers/external-configs.js +33 -0
- package/dist/helpers/flavor.d.ts +5 -0
- package/dist/helpers/flavor.js +40 -0
- package/dist/helpers/global-defines.d.ts +14 -0
- package/dist/helpers/global-defines.js +18 -0
- package/dist/helpers/main-entry.d.ts +5 -0
- package/dist/helpers/main-entry.js +75 -0
- package/dist/helpers/module-resolution.d.ts +1 -0
- package/dist/helpers/module-resolution.js +17 -0
- package/dist/helpers/nativescript-package-resolver.d.ts +5 -0
- package/dist/helpers/nativescript-package-resolver.js +139 -0
- package/dist/helpers/ns-cli-plugins.d.ts +19 -0
- package/dist/helpers/ns-cli-plugins.js +162 -0
- package/dist/helpers/package-platform-aliases.d.ts +4 -0
- package/dist/helpers/package-platform-aliases.js +83 -0
- package/dist/helpers/project.d.ts +23 -0
- package/dist/helpers/project.js +28 -0
- package/dist/helpers/resolver.d.ts +4 -0
- package/dist/helpers/resolver.js +31 -0
- package/dist/helpers/ts-config-paths.d.ts +4 -0
- package/dist/helpers/ts-config-paths.js +241 -0
- package/dist/helpers/utils.d.ts +29 -0
- package/dist/helpers/utils.js +101 -0
- package/dist/helpers/workers.d.ts +20 -0
- package/dist/helpers/workers.js +86 -0
- package/dist/hmr/hmr-angular.d.ts +1 -0
- package/dist/hmr/hmr-angular.js +34 -0
- package/dist/hmr/hmr-bridge.d.ts +18 -0
- package/dist/hmr/hmr-bridge.js +154 -0
- package/dist/hmr/hmr-client.d.ts +5 -0
- package/dist/hmr/hmr-client.js +93 -0
- package/dist/hmr/hmr-server.d.ts +20 -0
- package/dist/hmr/hmr-server.js +179 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/polyfills/mdn-data-at-rules.d.ts +7 -0
- package/dist/polyfills/mdn-data-at-rules.js +7 -0
- package/dist/polyfills/mdn-data-properties.d.ts +7 -0
- package/dist/polyfills/mdn-data-properties.js +7 -0
- package/dist/polyfills/mdn-data-syntaxes.d.ts +7 -0
- package/dist/polyfills/mdn-data-syntaxes.js +7 -0
- package/dist/polyfills/module.d.ts +17 -0
- package/dist/polyfills/module.js +29 -0
- package/dist/shims/react-reconciler-constants.d.ts +14 -0
- package/dist/shims/react-reconciler-constants.js +20 -0
- package/dist/shims/react-reconciler.d.ts +8 -0
- package/dist/shims/react-reconciler.js +14 -0
- package/dist/shims/set-value.d.ts +4 -0
- package/dist/shims/set-value.js +21 -0
- package/dist/transformers/NativeClass/index.d.ts +5 -0
- package/dist/transformers/NativeClass/index.js +46 -0
- package/package.json +31 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { mergeConfig } from "vite";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import angular from "@analogjs/vite-plugin-angular";
|
|
4
|
+
import { baseConfig } from "./base.js";
|
|
5
|
+
const plugins = [
|
|
6
|
+
// Allow external html template changes to trigger hot reload: Make .ts files depend on their .html templates
|
|
7
|
+
{
|
|
8
|
+
name: "angular-template-deps",
|
|
9
|
+
transform(code, id) {
|
|
10
|
+
// For .ts files that reference templateUrl, add the .html file as a dependency
|
|
11
|
+
if (id.endsWith(".ts") && code.includes("templateUrl")) {
|
|
12
|
+
const templateUrlMatch = code.match(/templateUrl:\s*['"]\.\/(.*?\.html)['"]/);
|
|
13
|
+
if (templateUrlMatch) {
|
|
14
|
+
const htmlPath = path.resolve(path.dirname(id), templateUrlMatch[1]);
|
|
15
|
+
// Add the HTML file as a dependency so Vite watches it
|
|
16
|
+
this.addWatchFile(htmlPath);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
angular({
|
|
23
|
+
liveReload: false, // Disable live reload in favor of HMR
|
|
24
|
+
}),
|
|
25
|
+
];
|
|
26
|
+
export const angularConfig = ({ mode }) => {
|
|
27
|
+
return mergeConfig(baseConfig({ mode }), {
|
|
28
|
+
plugins,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { mergeConfig } from "vite";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
import minimist from "minimist";
|
|
5
|
+
import commonjs from "@rollup/plugin-commonjs";
|
|
6
|
+
import { viteStaticCopy } from "vite-plugin-static-copy";
|
|
7
|
+
import NativeScriptPlugin from "../helpers/resolver.js";
|
|
8
|
+
import nsConfigAsJsonPlugin from "../helpers/config-as-json.js";
|
|
9
|
+
import { getProjectRootPath } from "../helpers/project.js";
|
|
10
|
+
import { aliasCssTree } from "../helpers/css-tree.js";
|
|
11
|
+
import { packagePlatformAliases } from "../helpers/package-platform-aliases.js";
|
|
12
|
+
import { getGlobalDefines } from "../helpers/global-defines.js";
|
|
13
|
+
import { getWorkerPlugins, workerUrlPlugin } from "../helpers/workers.js";
|
|
14
|
+
import { getTsConfigData } from "../helpers/ts-config-paths.js";
|
|
15
|
+
import { commonjsPlugins } from "../helpers/commonjs-plugins.js";
|
|
16
|
+
import { nativescriptPackageResolver } from "../helpers/nativescript-package-resolver.js";
|
|
17
|
+
import { hmrPlugin, cliPlugin } from "../helpers/ns-cli-plugins.js";
|
|
18
|
+
import { dynamicImportPlugin } from "../helpers/dynamic-import-plugin.js";
|
|
19
|
+
import { mainEntryPlugin } from "../helpers/main-entry.js";
|
|
20
|
+
import { determineProjectFlavor } from "../helpers/flavor.js";
|
|
21
|
+
import { externalConfigMerges, applyExternalConfigs, } from "../helpers/external-configs.js";
|
|
22
|
+
const debugViteLogs = !!process.env.VITE_DEBUG_PATHS;
|
|
23
|
+
const projectRoot = getProjectRootPath();
|
|
24
|
+
/**
|
|
25
|
+
* Plugins can define nativescript.vite.mjs
|
|
26
|
+
* which export Vite configs to merge into the base config.
|
|
27
|
+
*/
|
|
28
|
+
applyExternalConfigs();
|
|
29
|
+
export const baseConfig = ({ mode }) => {
|
|
30
|
+
const platform = mode;
|
|
31
|
+
const targetMode = process.env.production ? "production" : "development";
|
|
32
|
+
const cliArgs = minimist(process.argv.slice(2), { "--": true });
|
|
33
|
+
const cliFlags = (cliArgs["--"] || []).reduce((obj, flag) => {
|
|
34
|
+
// remove env prefix
|
|
35
|
+
const [rawKey, ...rest] = flag.replace(/^--env\./, "").split("=");
|
|
36
|
+
obj[rawKey] = rest.length === 0 ? true : rest.join("=");
|
|
37
|
+
return obj;
|
|
38
|
+
}, {});
|
|
39
|
+
console.log("cliFlags:", cliFlags);
|
|
40
|
+
const debug = !!cliFlags.viteDebug || !!process.env.DEBUG || targetMode === "development";
|
|
41
|
+
if (debug) {
|
|
42
|
+
console.log("--------------");
|
|
43
|
+
console.log("Vite config mode:", mode);
|
|
44
|
+
console.log("Target mode:", targetMode);
|
|
45
|
+
console.log("Platform:", platform);
|
|
46
|
+
console.log("--------------");
|
|
47
|
+
}
|
|
48
|
+
const flavor = determineProjectFlavor();
|
|
49
|
+
console.log(`Building for ${flavor}.`);
|
|
50
|
+
// Create TypeScript aliases with platform support
|
|
51
|
+
const tsConfigData = getTsConfigData(debugViteLogs, platform);
|
|
52
|
+
// Common resolve configuration for both main and worker builds
|
|
53
|
+
const resolveConfig = {
|
|
54
|
+
// ensures Vite prefers ESM entry‑points
|
|
55
|
+
mainFields: ["module", "main"],
|
|
56
|
+
// Make sure ESM conditions win during resolution
|
|
57
|
+
conditions: ["module", "import", "browser", "default"],
|
|
58
|
+
// use this with merge config instead or could list common dedupes in base here altogether
|
|
59
|
+
dedupe: ["@nativescript/core", "nativescript-vue", "vue"],
|
|
60
|
+
// Alias "@" and "~" to your src directory for cleaner imports
|
|
61
|
+
alias: [
|
|
62
|
+
...aliasCssTree,
|
|
63
|
+
// 1) Catch exactly `~/package.json` → virtual module (MUST be first!)
|
|
64
|
+
{ find: /^~\/package\.json$/, replacement: "~/package.json" },
|
|
65
|
+
// TypeScript path aliases from tsconfig.json
|
|
66
|
+
...tsConfigData.aliases,
|
|
67
|
+
// Generic platform resolution for any npm package
|
|
68
|
+
packagePlatformAliases(tsConfigData, platform,
|
|
69
|
+
// skipCommonjsPackages,
|
|
70
|
+
debugViteLogs),
|
|
71
|
+
// 2) Catch everything else under "~/" → your src/
|
|
72
|
+
{ find: /^~\/(.*)$/, replacement: path.resolve(projectRoot, "src/$1") },
|
|
73
|
+
// optional: "@" → src/
|
|
74
|
+
{ find: "@", replacement: path.resolve(projectRoot, "src") },
|
|
75
|
+
],
|
|
76
|
+
extensions: [
|
|
77
|
+
".ios.tsx",
|
|
78
|
+
".android.tsx",
|
|
79
|
+
".tsx",
|
|
80
|
+
".ios.jsx",
|
|
81
|
+
".android.jsx",
|
|
82
|
+
".jsx",
|
|
83
|
+
".ios.ts",
|
|
84
|
+
".android.ts",
|
|
85
|
+
".ts",
|
|
86
|
+
".ios.js",
|
|
87
|
+
".android.js",
|
|
88
|
+
".js",
|
|
89
|
+
".mjs",
|
|
90
|
+
".json",
|
|
91
|
+
],
|
|
92
|
+
preserveSymlinks: true,
|
|
93
|
+
};
|
|
94
|
+
// Common define configuration for both main and worker builds
|
|
95
|
+
const defineConfig = getGlobalDefines(platform, targetMode);
|
|
96
|
+
// Default static copy: copy/merge assets and fonts when present
|
|
97
|
+
const assetsSrcDir = path.resolve(projectRoot, "src/assets");
|
|
98
|
+
const assetsAppDir = path.resolve(projectRoot, "app/assets");
|
|
99
|
+
const fontsSrcDir = path.resolve(projectRoot, "src/fonts");
|
|
100
|
+
const fontsAppDir = path.resolve(projectRoot, "app/fonts");
|
|
101
|
+
const staticCopyTargets = [];
|
|
102
|
+
if (existsSync(assetsSrcDir)) {
|
|
103
|
+
staticCopyTargets.push({ src: `${assetsSrcDir}/**/*`, dest: "assets" });
|
|
104
|
+
}
|
|
105
|
+
else if (existsSync(assetsAppDir)) {
|
|
106
|
+
staticCopyTargets.push({ src: `${assetsAppDir}/**/*`, dest: "assets" });
|
|
107
|
+
}
|
|
108
|
+
if (existsSync(fontsSrcDir)) {
|
|
109
|
+
staticCopyTargets.push({ src: `${fontsSrcDir}/**/*`, dest: "fonts" });
|
|
110
|
+
}
|
|
111
|
+
else if (existsSync(fontsAppDir)) {
|
|
112
|
+
staticCopyTargets.push({ src: `${fontsAppDir}/**/*`, dest: "fonts" });
|
|
113
|
+
}
|
|
114
|
+
let baseViteConfig = {
|
|
115
|
+
resolve: resolveConfig,
|
|
116
|
+
define: defineConfig,
|
|
117
|
+
// Vite's built-in solution for CommonJS/ESM compatibility issues
|
|
118
|
+
optimizeDeps: {
|
|
119
|
+
// Force pre-bundling of problematic CommonJS packages
|
|
120
|
+
include: [
|
|
121
|
+
// "source-map-js",
|
|
122
|
+
"@valor/nativescript-websockets",
|
|
123
|
+
// React and related packages for proper module resolution
|
|
124
|
+
"react",
|
|
125
|
+
"react-reconciler",
|
|
126
|
+
"react-nativescript",
|
|
127
|
+
// ...(optimizeDeps || []),
|
|
128
|
+
// Add any other problematic packages here
|
|
129
|
+
],
|
|
130
|
+
// Handle Node.js built-ins and other edge cases
|
|
131
|
+
esbuildOptions: {
|
|
132
|
+
// Pass the same conditions to ESBuild for css-tree compatibility
|
|
133
|
+
conditions: ["module", "import", "browser", "default"],
|
|
134
|
+
// Define globals for Node.js built-ins if needed
|
|
135
|
+
define: {
|
|
136
|
+
global: "globalThis",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
plugins: [
|
|
141
|
+
// TODO: make flavor plugins dynamic
|
|
142
|
+
// ...flavorPlugins,
|
|
143
|
+
...commonjsPlugins,
|
|
144
|
+
// Platform-specific package resolver - MUST come before commonjs plugin
|
|
145
|
+
nativescriptPackageResolver(platform),
|
|
146
|
+
// Simplified CommonJS handling - let Vite's optimizeDeps do the heavy lifting
|
|
147
|
+
commonjs({
|
|
148
|
+
include: [/node_modules/],
|
|
149
|
+
// Force specific problematic modules to be treated as CommonJS
|
|
150
|
+
requireReturnsDefault: "auto",
|
|
151
|
+
defaultIsModuleExports: "auto",
|
|
152
|
+
transformMixedEsModules: true,
|
|
153
|
+
// Ignore optional dependencies that are meant to fail gracefully
|
|
154
|
+
ignore: [
|
|
155
|
+
"@nativescript/android",
|
|
156
|
+
"@nativescript/ios",
|
|
157
|
+
],
|
|
158
|
+
}),
|
|
159
|
+
nsConfigAsJsonPlugin(),
|
|
160
|
+
NativeScriptPlugin({ platform }),
|
|
161
|
+
mainEntryPlugin(cliFlags, debug),
|
|
162
|
+
dynamicImportPlugin(),
|
|
163
|
+
// Transform Vite worker URLs to NativeScript format AFTER bundling
|
|
164
|
+
workerUrlPlugin(),
|
|
165
|
+
// Copy static assets and fonts when present in project src/
|
|
166
|
+
...((staticCopyTargets == null ? void 0 : staticCopyTargets.length)
|
|
167
|
+
? [
|
|
168
|
+
viteStaticCopy({
|
|
169
|
+
targets: staticCopyTargets,
|
|
170
|
+
watch: { reloadPageOnChange: true },
|
|
171
|
+
}),
|
|
172
|
+
]
|
|
173
|
+
: []),
|
|
174
|
+
// NativeScript HMR integration - track changes for incremental builds
|
|
175
|
+
hmrPlugin(targetMode),
|
|
176
|
+
// NativeScript CLI integration - enhanced IPC communication for HMR
|
|
177
|
+
cliPlugin(targetMode, cliFlags),
|
|
178
|
+
],
|
|
179
|
+
css: {
|
|
180
|
+
postcss: "./postcss.config.js",
|
|
181
|
+
},
|
|
182
|
+
// Development server configuration for HMR
|
|
183
|
+
server: targetMode === "development"
|
|
184
|
+
? {
|
|
185
|
+
hmr: {
|
|
186
|
+
// Enable Vite's built-in HMR
|
|
187
|
+
port: 24678, // Different port to avoid conflicts with our custom WebSocket
|
|
188
|
+
},
|
|
189
|
+
// CORS for development
|
|
190
|
+
cors: true,
|
|
191
|
+
}
|
|
192
|
+
: {},
|
|
193
|
+
// Configure worker builds to bundle everything into standalone workers
|
|
194
|
+
worker: {
|
|
195
|
+
format: "es",
|
|
196
|
+
plugins: () => getWorkerPlugins(platform),
|
|
197
|
+
rollupOptions: {
|
|
198
|
+
// Don't externalize anything - bundle everything into the worker
|
|
199
|
+
external: [],
|
|
200
|
+
output: {
|
|
201
|
+
// Inline all dynamic imports to create standalone bundle
|
|
202
|
+
inlineDynamicImports: true,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
build: {
|
|
207
|
+
target: "esnext",
|
|
208
|
+
minify: !debug,
|
|
209
|
+
// Generate source maps for debugging
|
|
210
|
+
// Note: just disabling for now until can hook up to angular vite/analog plugin
|
|
211
|
+
sourcemap: false, //debug,
|
|
212
|
+
// Disable module preloading to avoid browser APIs
|
|
213
|
+
modulePreload: false,
|
|
214
|
+
// Optimize for development speed
|
|
215
|
+
...(targetMode === "development" && {
|
|
216
|
+
// Faster builds in development
|
|
217
|
+
reportCompressedSize: false,
|
|
218
|
+
chunkSizeWarningLimit: 2000,
|
|
219
|
+
}),
|
|
220
|
+
commonjsOptions: {
|
|
221
|
+
include: [/node_modules/],
|
|
222
|
+
},
|
|
223
|
+
rollupOptions: {
|
|
224
|
+
input: "virtual:entry-with-polyfills",
|
|
225
|
+
output: {
|
|
226
|
+
dir: path.resolve(projectRoot, "dist"),
|
|
227
|
+
format: "es", // Emit ES modules (.mjs)
|
|
228
|
+
entryFileNames: "bundle.mjs", // <- no hash here
|
|
229
|
+
chunkFileNames: (chunk) => {
|
|
230
|
+
if (chunk.name === "vendor")
|
|
231
|
+
return "vendor.mjs";
|
|
232
|
+
// Place worker files at root level, not in assets/
|
|
233
|
+
if (chunk.name && chunk.name.includes("worker")) {
|
|
234
|
+
return "[name]-[hash].js";
|
|
235
|
+
}
|
|
236
|
+
return "[name]-[hash].mjs";
|
|
237
|
+
},
|
|
238
|
+
assetFileNames: "assets/[name][extname]",
|
|
239
|
+
// Create single vendor chunk - no separate globals chunk to avoid circular deps
|
|
240
|
+
manualChunks(id) {
|
|
241
|
+
if (id.includes("node_modules")) {
|
|
242
|
+
// Note: this is work in progress on best setup
|
|
243
|
+
// Keep polyfills and Zone-dependent packages in main bundle to ensure correct execution order
|
|
244
|
+
if ([
|
|
245
|
+
"@nativescript/core",
|
|
246
|
+
"@nativescript/angular",
|
|
247
|
+
"@nativescript/zone-js",
|
|
248
|
+
"@valor/nativescript-websockets",
|
|
249
|
+
"make-error",
|
|
250
|
+
// "source-map-js",
|
|
251
|
+
"zone.js",
|
|
252
|
+
"/globals",
|
|
253
|
+
"/polyfills",
|
|
254
|
+
].includes(id)) {
|
|
255
|
+
return undefined; // Keep in main bundle
|
|
256
|
+
}
|
|
257
|
+
return "vendor";
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
if (externalConfigMerges?.length) {
|
|
265
|
+
externalConfigMerges.forEach((config) => {
|
|
266
|
+
baseViteConfig = mergeConfig(baseViteConfig, config);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return baseViteConfig;
|
|
270
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { UserConfig } from "vite";
|
|
2
|
+
export declare const baseConfig: ({ mode, skipCommonjsPackages, customCommonjsPlugins, commonjsIgnoreList, optimizeDeps, }: {
|
|
3
|
+
mode: string;
|
|
4
|
+
skipCommonjsPackages?: string[];
|
|
5
|
+
customCommonjsPlugins?: {
|
|
6
|
+
name: string;
|
|
7
|
+
enforce: string;
|
|
8
|
+
resolveId(id: any): any;
|
|
9
|
+
load(id: any): string;
|
|
10
|
+
}[];
|
|
11
|
+
commonjsIgnoreList?: string[];
|
|
12
|
+
optimizeDeps?: string[];
|
|
13
|
+
}) => UserConfig;
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import minimist from "minimist";
|
|
3
|
+
import commonjs from "@rollup/plugin-commonjs";
|
|
4
|
+
import NativeScriptPlugin from "../helpers/resolver.js";
|
|
5
|
+
import nsConfigAsJsonPlugin from "../helpers/config-as-json.js";
|
|
6
|
+
import { getProjectRootPath } from "../helpers/project.js";
|
|
7
|
+
import { aliasCssTree } from "../helpers/css-tree.js";
|
|
8
|
+
import { packagePlatformAliases } from "../helpers/package-platform-aliases.js";
|
|
9
|
+
import { getGlobalDefines } from "../helpers/global-defines.js";
|
|
10
|
+
import { getWorkerPlugins, workerUrlPlugin } from "../helpers/workers.js";
|
|
11
|
+
import { getTsConfigData } from "../helpers/ts-config-paths.js";
|
|
12
|
+
import { commonjsPlugins } from "../helpers/commonjs-plugins.js";
|
|
13
|
+
import { nativescriptPackageResolver } from "../helpers/nativescript-package-resolver.js";
|
|
14
|
+
import { hmrPlugin, cliPlugin } from "../helpers/ns-cli-plugins.js";
|
|
15
|
+
import { dynamicImportPlugin } from "../helpers/dynamic-import-plugin.js";
|
|
16
|
+
import { mainEntryPlugin } from "../helpers/main-entry.js";
|
|
17
|
+
import { determineProjectFlavor } from "../helpers/flavor.js";
|
|
18
|
+
const debugViteLogs = !!process.env.VITE_DEBUG_PATHS;
|
|
19
|
+
const projectRoot = getProjectRootPath();
|
|
20
|
+
// NOTE: just testing with angular directly temporarily
|
|
21
|
+
// change this to pick up environment for flavors dynamically
|
|
22
|
+
// import { angularPlugins } from "./angular.js";
|
|
23
|
+
// import { getReactPlugins } from "./react.js";
|
|
24
|
+
// import { getSolidPlugins } from "./solid.js";
|
|
25
|
+
// import { vuePlugins } from "./vue.js";
|
|
26
|
+
export const baseConfig = ({ mode, skipCommonjsPackages, customCommonjsPlugins, commonjsIgnoreList, optimizeDeps, }) => {
|
|
27
|
+
const platform = mode;
|
|
28
|
+
const targetMode = process.env.production ? "production" : "development";
|
|
29
|
+
const cliArgs = minimist(process.argv.slice(2), { "--": true });
|
|
30
|
+
const cliFlags = (cliArgs["--"] || []).reduce((obj, flag) => {
|
|
31
|
+
// remove env prefix
|
|
32
|
+
const [rawKey, ...rest] = flag.replace(/^--env\./, "").split("=");
|
|
33
|
+
obj[rawKey] = rest.length === 0 ? true : rest.join("=");
|
|
34
|
+
return obj;
|
|
35
|
+
}, {});
|
|
36
|
+
console.log("cliFlags:", cliFlags);
|
|
37
|
+
const debug = !!cliFlags.viteDebug || !!process.env.DEBUG || targetMode === "development";
|
|
38
|
+
if (debug) {
|
|
39
|
+
console.log("--------------");
|
|
40
|
+
console.log("Vite config mode:", mode);
|
|
41
|
+
console.log("Target mode:", targetMode);
|
|
42
|
+
console.log("Platform:", platform);
|
|
43
|
+
console.log("--------------");
|
|
44
|
+
}
|
|
45
|
+
const flavor = determineProjectFlavor();
|
|
46
|
+
console.log(`Building with ${flavor}`);
|
|
47
|
+
// let flavorPlugins: any[] = [];
|
|
48
|
+
switch (flavor) {
|
|
49
|
+
// case "angular":
|
|
50
|
+
// flavorPlugins = angularPlugins;
|
|
51
|
+
// break;
|
|
52
|
+
// case "react":
|
|
53
|
+
// flavorPlugins = getReactPlugins(!debug);
|
|
54
|
+
// break;
|
|
55
|
+
// case "vue":
|
|
56
|
+
// flavorPlugins = vuePlugins;
|
|
57
|
+
// break;
|
|
58
|
+
// case "solid":
|
|
59
|
+
// flavorPlugins = getSolidPlugins(!debug);
|
|
60
|
+
// break;
|
|
61
|
+
}
|
|
62
|
+
// Create TypeScript aliases with platform support
|
|
63
|
+
const tsConfigData = getTsConfigData(debugViteLogs, platform);
|
|
64
|
+
// Common resolve configuration for both main and worker builds
|
|
65
|
+
const resolveConfig = {
|
|
66
|
+
// ensures Vite prefers ESM entry‑points
|
|
67
|
+
mainFields: ["module", "main"],
|
|
68
|
+
// Make sure ESM conditions win during resolution
|
|
69
|
+
conditions: ["module", "import", "browser", "default"],
|
|
70
|
+
// use this with merge config instead or could list common dedupes in base here altogether
|
|
71
|
+
dedupe: ["@nativescript/core", "nativescript-vue", "vue"],
|
|
72
|
+
// Alias "@" and "~" to your src directory for cleaner imports
|
|
73
|
+
alias: [
|
|
74
|
+
...aliasCssTree,
|
|
75
|
+
// 1) Catch exactly `~/package.json` → virtual module (MUST be first!)
|
|
76
|
+
{ find: /^~\/package\.json$/, replacement: "~/package.json" },
|
|
77
|
+
// TypeScript path aliases from tsconfig.json
|
|
78
|
+
...tsConfigData.aliases,
|
|
79
|
+
// Generic platform resolution for any npm package
|
|
80
|
+
packagePlatformAliases(tsConfigData, platform, true),
|
|
81
|
+
// 2) Catch everything else under "~/" → your src/
|
|
82
|
+
{ find: /^~\/(.*)$/, replacement: path.resolve(projectRoot, "src/$1") },
|
|
83
|
+
// optional: "@" → src/
|
|
84
|
+
{ find: "@", replacement: path.resolve(projectRoot, "src") },
|
|
85
|
+
],
|
|
86
|
+
extensions: [
|
|
87
|
+
".ios.tsx",
|
|
88
|
+
".android.tsx",
|
|
89
|
+
".tsx",
|
|
90
|
+
".ios.jsx",
|
|
91
|
+
".android.jsx",
|
|
92
|
+
".jsx",
|
|
93
|
+
".ios.ts",
|
|
94
|
+
".android.ts",
|
|
95
|
+
".ts",
|
|
96
|
+
".ios.js",
|
|
97
|
+
".android.js",
|
|
98
|
+
".js",
|
|
99
|
+
".mjs",
|
|
100
|
+
".json",
|
|
101
|
+
],
|
|
102
|
+
preserveSymlinks: true,
|
|
103
|
+
};
|
|
104
|
+
// Common define configuration for both main and worker builds
|
|
105
|
+
const defineConfig = getGlobalDefines(platform, targetMode);
|
|
106
|
+
return {
|
|
107
|
+
resolve: resolveConfig,
|
|
108
|
+
define: defineConfig,
|
|
109
|
+
// Vite's built-in solution for CommonJS/ESM compatibility issues
|
|
110
|
+
optimizeDeps: {
|
|
111
|
+
// Force pre-bundling of problematic CommonJS packages
|
|
112
|
+
include: [
|
|
113
|
+
// "source-map-js",
|
|
114
|
+
"@valor/nativescript-websockets",
|
|
115
|
+
// React and related packages for proper module resolution
|
|
116
|
+
"react",
|
|
117
|
+
"react-reconciler",
|
|
118
|
+
"react-nativescript",
|
|
119
|
+
...(optimizeDeps || []),
|
|
120
|
+
// Add any other problematic packages here
|
|
121
|
+
],
|
|
122
|
+
// Handle Node.js built-ins and other edge cases
|
|
123
|
+
esbuildOptions: {
|
|
124
|
+
// Pass the same conditions to ESBuild for css-tree compatibility
|
|
125
|
+
conditions: ["module", "import", "browser", "default"],
|
|
126
|
+
// Define globals for Node.js built-ins if needed
|
|
127
|
+
define: {
|
|
128
|
+
global: "globalThis",
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
plugins: [
|
|
133
|
+
// TODO: make flavor plugins dynamic
|
|
134
|
+
// ...flavorPlugins,
|
|
135
|
+
...commonjsPlugins,
|
|
136
|
+
...(customCommonjsPlugins || []),
|
|
137
|
+
// Platform-specific package resolver - MUST come before commonjs plugin
|
|
138
|
+
nativescriptPackageResolver(platform),
|
|
139
|
+
// Simplified CommonJS handling - let Vite's optimizeDeps do the heavy lifting
|
|
140
|
+
commonjs({
|
|
141
|
+
include: [/node_modules/],
|
|
142
|
+
// Force specific problematic modules to be treated as CommonJS
|
|
143
|
+
requireReturnsDefault: "auto",
|
|
144
|
+
defaultIsModuleExports: "auto",
|
|
145
|
+
transformMixedEsModules: true,
|
|
146
|
+
// Ignore optional dependencies that are meant to fail gracefully
|
|
147
|
+
ignore: [
|
|
148
|
+
"@nativescript/android",
|
|
149
|
+
"@nativescript/ios",
|
|
150
|
+
...(commonjsIgnoreList || []),
|
|
151
|
+
],
|
|
152
|
+
}),
|
|
153
|
+
nsConfigAsJsonPlugin(),
|
|
154
|
+
NativeScriptPlugin({ platform }),
|
|
155
|
+
mainEntryPlugin(cliFlags, debug),
|
|
156
|
+
// NativeScript HMR integration - track changes for incremental builds
|
|
157
|
+
hmrPlugin(targetMode),
|
|
158
|
+
// NativeScript CLI integration - enhanced IPC communication for HMR
|
|
159
|
+
cliPlugin(targetMode, cliFlags),
|
|
160
|
+
dynamicImportPlugin(),
|
|
161
|
+
// Transform Vite worker URLs to NativeScript format AFTER bundling
|
|
162
|
+
workerUrlPlugin(),
|
|
163
|
+
],
|
|
164
|
+
css: {
|
|
165
|
+
postcss: "./postcss.config.js",
|
|
166
|
+
},
|
|
167
|
+
// Development server configuration for HMR
|
|
168
|
+
server: targetMode === "development"
|
|
169
|
+
? {
|
|
170
|
+
hmr: {
|
|
171
|
+
// Enable Vite's built-in HMR
|
|
172
|
+
port: 24678, // Different port to avoid conflicts with our custom WebSocket
|
|
173
|
+
},
|
|
174
|
+
// CORS for development
|
|
175
|
+
cors: true,
|
|
176
|
+
}
|
|
177
|
+
: {},
|
|
178
|
+
// Configure worker builds to bundle everything into standalone workers
|
|
179
|
+
worker: {
|
|
180
|
+
format: "es",
|
|
181
|
+
plugins: () => getWorkerPlugins(platform),
|
|
182
|
+
rollupOptions: {
|
|
183
|
+
// Don't externalize anything - bundle everything into the worker
|
|
184
|
+
external: [],
|
|
185
|
+
output: {
|
|
186
|
+
// Inline all dynamic imports to create standalone bundle
|
|
187
|
+
inlineDynamicImports: true,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
build: {
|
|
192
|
+
target: "esnext",
|
|
193
|
+
minify: !debug,
|
|
194
|
+
// Generate source maps for debugging
|
|
195
|
+
// Note: just disabling for now until can hook up to angular vite/analog plugin
|
|
196
|
+
sourcemap: false, //debug,
|
|
197
|
+
// Disable module preloading to avoid browser APIs
|
|
198
|
+
modulePreload: false,
|
|
199
|
+
// Optimize for development speed
|
|
200
|
+
...(targetMode === "development" && {
|
|
201
|
+
// Faster builds in development
|
|
202
|
+
reportCompressedSize: false,
|
|
203
|
+
chunkSizeWarningLimit: 2000,
|
|
204
|
+
}),
|
|
205
|
+
commonjsOptions: {
|
|
206
|
+
include: [/node_modules/],
|
|
207
|
+
},
|
|
208
|
+
rollupOptions: {
|
|
209
|
+
input: "virtual:entry-with-polyfills",
|
|
210
|
+
output: {
|
|
211
|
+
dir: path.resolve(projectRoot, "dist"),
|
|
212
|
+
format: "es", // Emit ES modules (.mjs)
|
|
213
|
+
entryFileNames: "bundle.mjs", // <- no hash here
|
|
214
|
+
chunkFileNames: (chunk) => {
|
|
215
|
+
if (chunk.name === "vendor")
|
|
216
|
+
return "vendor.mjs";
|
|
217
|
+
// Place worker files at root level, not in assets/
|
|
218
|
+
if (chunk.name && chunk.name.includes("worker")) {
|
|
219
|
+
return "[name]-[hash].js";
|
|
220
|
+
}
|
|
221
|
+
return "[name]-[hash].mjs";
|
|
222
|
+
},
|
|
223
|
+
assetFileNames: "assets/[name][extname]",
|
|
224
|
+
// Create single vendor chunk - no separate globals chunk to avoid circular deps
|
|
225
|
+
manualChunks(id) {
|
|
226
|
+
if (id.includes("node_modules")) {
|
|
227
|
+
// Note: this is work in progress on best setup
|
|
228
|
+
// Keep polyfills and Zone-dependent packages in main bundle to ensure correct execution order
|
|
229
|
+
if ([
|
|
230
|
+
"@nativescript/core",
|
|
231
|
+
"@nativescript/angular",
|
|
232
|
+
"@nativescript/zone-js",
|
|
233
|
+
"@valor/nativescript-websockets",
|
|
234
|
+
"make-error",
|
|
235
|
+
// "source-map-js",
|
|
236
|
+
"zone.js",
|
|
237
|
+
"/globals",
|
|
238
|
+
"/polyfills",
|
|
239
|
+
].includes(id)) {
|
|
240
|
+
return undefined; // Keep in main bundle
|
|
241
|
+
}
|
|
242
|
+
return "vendor";
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import alias from "@rollup/plugin-alias";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { mergeConfig } from "vite";
|
|
5
|
+
import { baseConfig } from "./base.js";
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
const plugins = [
|
|
9
|
+
{
|
|
10
|
+
...alias({
|
|
11
|
+
entries: {
|
|
12
|
+
// Essential aliases for React NativeScript compatibility
|
|
13
|
+
// Only alias react-dom related imports, NOT React itself
|
|
14
|
+
"react-dom": "react-nativescript",
|
|
15
|
+
"react-dom/client": "react-nativescript",
|
|
16
|
+
"react-dom/server": "react-nativescript",
|
|
17
|
+
// Handle react-reconciler exports issue
|
|
18
|
+
// This addresses the "DefaultEventPriority" and "LegacyRoot" not exported errors
|
|
19
|
+
"react-reconciler/constants": resolve(__dirname, "../shims/react-reconciler-constants.js"),
|
|
20
|
+
// Additional React ecosystem compatibility
|
|
21
|
+
"react-reconciler/src/ReactFiberHostConfig": "react-nativescript/dist/client/HostConfig",
|
|
22
|
+
// Fix React reconciler namespace issue
|
|
23
|
+
"react-reconciler": resolve(__dirname, "../shims/react-reconciler.js"),
|
|
24
|
+
// Additional shims
|
|
25
|
+
"set-value": resolve(__dirname, "../shims/set-value.js"),
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
enforce: "pre",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "react-nativescript-resolver",
|
|
32
|
+
resolveId(id) {
|
|
33
|
+
// Ensure React core exports are properly resolved
|
|
34
|
+
if (id === "react") {
|
|
35
|
+
return { id: "react", external: false };
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
load(id) {
|
|
39
|
+
// Handle React module loading to ensure all exports are available
|
|
40
|
+
if (id === "react") {
|
|
41
|
+
return `
|
|
42
|
+
// Re-export all React exports to ensure they're available
|
|
43
|
+
import * as React from 'react/index.js';
|
|
44
|
+
|
|
45
|
+
// Explicitly export the commonly used React APIs
|
|
46
|
+
export const {
|
|
47
|
+
Component,
|
|
48
|
+
PureComponent,
|
|
49
|
+
Fragment,
|
|
50
|
+
createElement,
|
|
51
|
+
createRef,
|
|
52
|
+
forwardRef,
|
|
53
|
+
useState,
|
|
54
|
+
useEffect,
|
|
55
|
+
useContext,
|
|
56
|
+
useReducer,
|
|
57
|
+
useCallback,
|
|
58
|
+
useMemo,
|
|
59
|
+
useRef,
|
|
60
|
+
useImperativeHandle,
|
|
61
|
+
useLayoutEffect,
|
|
62
|
+
useDebugValue,
|
|
63
|
+
createContext,
|
|
64
|
+
Children,
|
|
65
|
+
cloneElement,
|
|
66
|
+
isValidElement,
|
|
67
|
+
version,
|
|
68
|
+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
|
|
69
|
+
} = React;
|
|
70
|
+
|
|
71
|
+
// Export everything else
|
|
72
|
+
export * from 'react/index.js';
|
|
73
|
+
|
|
74
|
+
// Default export
|
|
75
|
+
export default React;
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
export const reactConfig = ({ mode }) => {
|
|
82
|
+
return mergeConfig(baseConfig({ mode }), {
|
|
83
|
+
plugins,
|
|
84
|
+
});
|
|
85
|
+
};
|