Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/vite/configuration/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { normalizeModuleId } from '../helpers/normalize-id.js';
import { getHmrWatchIgnoreGlobs } from '../helpers/hmr-scope.js';
import { NS_OPTIMIZE_DEPS_EXCLUDE } from '../helpers/optimize-deps.js';
import { sbgClassNamesPlugin } from '../helpers/sbg-class-names.js';
import { chunkFileNames } from '../helpers/chunk-file-names.js';
const require = createRequire(import.meta.url);
// Optional PostCSS import plugin (only used if available)
// Try to load postcss-import to control CSS @import resolution simply
Expand Down Expand Up @@ -610,14 +611,7 @@ export const baseConfig = ({ mode, flavor }: { mode: string; flavor?: string }):
// Chrome DevTools can fetch them even though the running code comes
// from file:///app on the simulator/device.
sourcemapBaseUrl: pathToFileURL(path.resolve(projectRoot, distOutputFolder)).toString() + '/',
chunkFileNames: (chunk) => {
if (chunk.name === 'vendor') return 'vendor.mjs';
// Place worker files at root level, not in assets/
if (chunk.name && chunk.name.includes('worker')) {
return '[name]-[hash].js';
}
return '[name]-[hash].mjs';
},
chunkFileNames,
assetFileNames: 'assets/[name][extname]',
// Create single vendor chunk for most third-party modules.
// Keep critical NativeScript core bootstrap/lifecycle modules in the main bundle
Expand Down
40 changes: 40 additions & 0 deletions packages/vite/helpers/chunk-file-names.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { build } from 'vite';
import { afterAll, describe, expect, it } from 'vitest';

import { chunkFileNames } from './chunk-file-names.js';

describe('chunkFileNames', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ns-chunk-names-'));
afterAll(() => fs.rmSync(root, { recursive: true, force: true }));

const buildFileNames = async (activitySource: string): Promise<string[]> => {
fs.mkdirSync(path.join(root, 'ui/frame'), { recursive: true });
fs.writeFileSync(path.join(root, 'ui/frame/activity.android.js'), activitySource);
fs.writeFileSync(path.join(root, 'entry.js'), "import('./ui/frame/activity.android.js');\n");
const result: any = await build({
root,
configFile: false,
logLevel: 'silent',
build: {
write: false,
minify: false,
rolldownOptions: { input: path.join(root, 'entry.js'), output: { format: 'es', entryFileNames: 'bundle.mjs', chunkFileNames } },
},
});
return (Array.isArray(result) ? result[0] : result).output.map((file: any) => file.fileName).sort();
};

it('names the Android activity chunk without a hash, so a rebuild keeps the path the generated Java activity loads', async () => {
expect(await buildFileNames('globalThis.activity = 1;\n')).toEqual(['activity.android.mjs', 'bundle.mjs']);
expect(await buildFileNames('globalThis.activity = 2;\n')).toEqual(['activity.android.mjs', 'bundle.mjs']);
});

it('keeps the vendor, worker and hashed names for other chunks', () => {
expect(chunkFileNames({ name: 'vendor' })).toBe('vendor.mjs');
expect(chunkFileNames({ name: 'image-worker' })).toBe('[name]-[hash].js');
expect(chunkFileNames({ name: 'page' })).toBe('[name]-[hash].mjs');
});
});
9 changes: 9 additions & 0 deletions packages/vite/helpers/chunk-file-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function chunkFileNames(chunk: { name?: string }): string {
if (chunk.name === 'vendor') return 'vendor.mjs';
if (chunk.name?.startsWith('activity.android')) return '[name].mjs';
// Place worker files at root level, not in assets/
if (chunk.name && chunk.name.includes('worker')) {
return '[name]-[hash].js';
}
return '[name]-[hash].mjs';
}
Loading