|
1 | 1 | import test from 'ava';
|
2 | 2 | import path from 'node:path';
|
3 | 3 | import { fileURLToPath } from 'node:url';
|
| 4 | +import { readFile } from 'node:fs/promises'; |
4 | 5 | import { rollup } from 'rollup';
|
5 | 6 | import { customImport } from '../dist/index.mjs';
|
6 | 7 | import { execESM } from './helpers/eval.mjs';
|
@@ -77,3 +78,64 @@ test('throw error when importing a not existing file', async (t) => {
|
77 | 78 | }
|
78 | 79 | t.fail();
|
79 | 80 | });
|
| 81 | + |
| 82 | +test('original content', async (t) => { |
| 83 | + let originalContent; |
| 84 | + const bundle = await rollup({ |
| 85 | + input: path.resolve(__dirname, 'fixtures/existing.js'), |
| 86 | + plugins: [ |
| 87 | + customImport({ |
| 88 | + include: ['**/file.js'], |
| 89 | + content: (id, original) => { |
| 90 | + originalContent = original; |
| 91 | + return { |
| 92 | + code: original, |
| 93 | + }; |
| 94 | + }, |
| 95 | + }), |
| 96 | + ], |
| 97 | + }); |
| 98 | + const realContent = await readFile( |
| 99 | + path.resolve(__dirname, 'fixtures/file.js'), |
| 100 | + 'utf-8' |
| 101 | + ); |
| 102 | + t.is(originalContent, realContent); |
| 103 | +}); |
| 104 | + |
| 105 | +const pluginContextKeys = [ |
| 106 | + 'addWatchFile', |
| 107 | + 'cache', |
| 108 | + 'debug', |
| 109 | + 'emitFile', |
| 110 | + 'error', |
| 111 | + 'getFileName', |
| 112 | + 'getModuleIds', |
| 113 | + 'getModuleInfo', |
| 114 | + 'getWatchFiles', |
| 115 | + 'info', |
| 116 | + 'load', |
| 117 | + 'meta', |
| 118 | + 'parse', |
| 119 | + 'resolve', |
| 120 | + 'setAssetSource', |
| 121 | + 'warn', |
| 122 | + 'getCombinedSourcemap', |
| 123 | +]; |
| 124 | +test('plugin context', async (t) => { |
| 125 | + let keys; |
| 126 | + const bundle = await rollup({ |
| 127 | + input: path.resolve(__dirname, 'fixtures/existing.js'), |
| 128 | + plugins: [ |
| 129 | + customImport({ |
| 130 | + include: ['**/file.js'], |
| 131 | + content(id, original) { |
| 132 | + keys = Object.keys(this); |
| 133 | + return { |
| 134 | + code: original, |
| 135 | + }; |
| 136 | + }, |
| 137 | + }), |
| 138 | + ], |
| 139 | + }); |
| 140 | + t.deepEqual(keys, pluginContextKeys); |
| 141 | +}); |
0 commit comments