Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 98ede4c

Browse files
committed
feat(rollup): dependency tracking for svelte2
1 parent aae7d6c commit 98ede4c

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

rollup/svelte2.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,33 @@ module.exports = (args = {}) => {
2828
return null;
2929
}
3030

31-
const result = options.preprocess ?
32-
await preprocess(code, { ...options.preprocess, filename }) :
33-
await Promise.resolve({ toString : () => code });
31+
const dependencies = new Set();
32+
33+
let result;
34+
35+
if(options.preprocess) {
36+
// Monkey-patch all potential preprocess tags (style, script, markup)
37+
// to capture any dependency info they might return
38+
const facade = Object.create(null);
39+
40+
facade.filename = filename;
41+
42+
Object.keys(options.preprocess).forEach((key) => {
43+
facade[key] = async (...params) => {
44+
const out = await options.preprocess[key](...params);
45+
46+
if(out.dependencies) {
47+
out.dependencies.forEach((dep) => dependencies.add(dep));
48+
}
49+
50+
return out;
51+
};
52+
});
53+
54+
result = await preprocess(code, facade);
55+
} else {
56+
result = await Promise.resolve({ toString : () => code });
57+
}
3458

3559
const { js } = compile(result.toString(), {
3660
onwarn : (warning) => this.warn(warning),
@@ -42,6 +66,8 @@ module.exports = (args = {}) => {
4266
name : sanitize(filename),
4367
});
4468

69+
dependencies.forEach((dep) => this.addWatchFile(dep));
70+
4571
return js;
4672
},
4773
};

0 commit comments

Comments
 (0)