Skip to content

Commit

Permalink
Merge pull request #41 from Conduitry/gh-40
Browse files Browse the repository at this point in the history
Pass through dependencies from preprocessor
  • Loading branch information
Rich-Harris committed Dec 15, 2018
2 parents c2127d5 + 0e315f2 commit dd4f86c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export default {
```


## Preprocessing and dependencies

If you are using the `preprocess` feature, then your callback responses may — in addition to the `code` and `map` values described in the Svelte compile docs — also optionally include a `dependencies` array. This should be the paths of additional files that the preprocessor result in some way depends upon. In Rollup 0.61+ in watch mode, any changes to these additional files will also trigger re-builds.


## `pkg.svelte`

If you're importing a component from your node_modules folder, and that component's package.json has a `"svelte"` property...
Expand Down
25 changes: 23 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,28 @@ module.exports = function svelte(options = {}) {

if (!~extensions.indexOf(extension)) return null;

return (options.preprocess ? preprocess(code, Object.assign({}, options.preprocess, { filename : id })) : Promise.resolve(code)).then(code => {
const dependencies = [];
let preprocessPromise;
if (options.preprocess) {
const preprocessOptions = {};
for (const key in options.preprocess) {
preprocessOptions[key] = (...args) => {
return Promise.resolve(options.preprocess[key](...args)).then(resp => {
if (resp && resp.dependencies) {
dependencies.push(...resp.dependencies);
}
return resp;
});
};
}
preprocessPromise = preprocess(code, Object.assign(preprocessOptions, { filename: id })).then(code => code.toString());
} else {
preprocessPromise = Promise.resolve(code);
}

return preprocessPromise.then(code => {
const compiled = compile(
code.toString(),
code,
Object.assign({}, {
onwarn: warning => {
if ((options.css || !options.emitCss) && warning.code === 'css-unused-selector') return;
Expand All @@ -225,6 +244,8 @@ module.exports = function svelte(options = {}) {
cssLookup.set(fname, compiled.css);
}

compiled.js.dependencies = dependencies;

return compiled.js;
});
},
Expand Down
14 changes: 9 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ describe('rollup-plugin-svelte', () => {
);
});

it('creates a {code, map} object, excluding the AST etc', () => {
it('creates a {code, map, dependencies} object, excluding the AST etc', () => {
const { transform } = plugin();
return transform('', 'test.html').then(compiled => {
assert.deepEqual(Object.keys(compiled), ['code', 'map']);
assert.deepEqual(Object.keys(compiled), ['code', 'map', 'dependencies']);
});
});

Expand Down Expand Up @@ -141,18 +141,22 @@ describe('rollup-plugin-svelte', () => {
return {
code: content
.replace('__REPLACEME__', 'replaced')
.replace('__FILENAME__', filename)
.replace('__FILENAME__', filename),
dependencies: ['foo'],
};
}
},
style: () => null,
}
});

return transform(`
<h1>Hello __REPLACEME__!</h1>
<h2>file: __FILENAME__</h2>
`, 'test.html').then(({ code }) => {
<style>h1 { color: red; }</style>
`, 'test.html').then(({ code, dependencies }) => {
assert.equal(code.indexOf('__REPLACEME__'), -1, 'content not modified');
assert.notEqual(code.indexOf('file: test.html'), -1, 'filename not replaced');
assert.deepEqual(dependencies, ['foo']);
});
});

Expand Down

0 comments on commit dd4f86c

Please sign in to comment.