Repository navigation
Expand file tree
/
Copy pathtest.mjs
More file actions
108 lines (99 loc) · 3.16 KB
/
Copy pathtest.mjs
File metadata and controls
108 lines (99 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { rollup } from 'rollup';
import path from 'node:path';
// Plugin that logs all module IDs it encounters
function idInspectorPlugin() {
return {
name: 'id-inspector',
resolveId(source, importer) {
// Don't resolve, just observe the default resolution
return null;
},
transform(code, id) {
console.log(`[transform] id = ${JSON.stringify(id)}`);
console.log(`[transform] has backslash = ${id.includes('\\')}`);
console.log(`[transform] has forward slash = ${id.includes('/')}`);
console.log(`[transform] path.sep = ${JSON.stringify(path.sep)}`);
console.log('---');
return null;
},
buildEnd() {
console.log('[buildEnd] All module IDs processed');
}
};
}
// Also test what resolveId returns by default
function resolveIdLoggerPlugin() {
return {
name: 'resolve-id-logger',
resolveId: {
order: 'post',
async handler(source, importer, options) {
// Let Rollup resolve first, then log the result
const resolved = await this.resolve(source, importer, {
...options,
skipSelf: true
});
if (resolved) {
console.log(`[resolveId] source=${JSON.stringify(source)} -> id=${JSON.stringify(resolved.id)}`);
console.log(`[resolveId] resolved id has backslash = ${resolved.id.includes('\\')}`);
console.log(`[resolveId] resolved id has forward slash = ${resolved.id.includes('/')}`);
console.log('---');
}
return null; // Don't interfere with resolution
}
}
};
}
// Test watchChange event IDs
function watchFilePlugin() {
return {
name: 'watch-file',
buildStart() {
// Add a watch file using native path
const watchPath = path.resolve('src/utils/hello.js');
console.log(`[watchFile] Adding watch file with native path: ${JSON.stringify(watchPath)}`);
this.addWatchFile(watchPath);
},
watchChange(id, change) {
console.log(`[watchChange] id = ${JSON.stringify(id)}`);
}
};
}
async function main() {
console.log('=== Rollup Module ID Test on', process.platform, '===');
console.log(`path.sep = ${JSON.stringify(path.sep)}`);
console.log(`cwd = ${JSON.stringify(process.cwd())}`);
console.log('');
const bundle = await rollup({
input: 'src/main.js',
plugins: [
resolveIdLoggerPlugin(),
idInspectorPlugin(),
watchFilePlugin()
]
});
// Check the module graph
console.log('=== Module Graph ===');
for (const moduleId of bundle.watchFiles) {
console.log(`[watchFiles] ${JSON.stringify(moduleId)}`);
console.log(` has backslash = ${moduleId.includes('\\')}`);
console.log(` has forward slash = ${moduleId.includes('/')}`);
}
// Also check getModuleInfo
console.log('');
console.log('=== Module Info ===');
const cache = bundle.cache;
if (cache?.modules) {
for (const mod of cache.modules) {
console.log(`[cache.module] id = ${JSON.stringify(mod.id)}`);
console.log(` has backslash = ${mod.id.includes('\\')}`);
}
}
await bundle.close();
console.log('');
console.log('=== Test Complete ===');
}
main().catch(err => {
console.error(err);
process.exit(1);
});