-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
conditional-fsevents-import.ts
33 lines (31 loc) · 1.04 KB
/
conditional-fsevents-import.ts
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
import MagicString from 'magic-string';
import { Plugin } from 'rollup';
const FSEVENTS_REQUIRE = "require('fsevents')";
const REPLACEMENT = "require('../../../src/watch/fsevents-importer').getFsEvents()";
export default function conditionalFsEventsImport(): Plugin {
let transformed = false;
return {
buildEnd(error) {
if (!(error || transformed)) {
throw new Error('Could not find "fsevents-handler.js", was the file renamed?');
}
},
name: 'conditional-fs-events-import',
transform(code, id) {
if (id.endsWith('fsevents-handler.js')) {
transformed = true;
const requireStatementPos = code.indexOf(FSEVENTS_REQUIRE);
if (requireStatementPos < 0) {
throw new Error(`Could not find expected fsevents import "${FSEVENTS_REQUIRE}"`);
}
const magicString = new MagicString(code);
magicString.overwrite(
requireStatementPos,
requireStatementPos + FSEVENTS_REQUIRE.length,
REPLACEMENT
);
return { code: magicString.toString(), map: magicString.generateMap({ hires: true }) };
}
}
};
}