Skip to content

feat: pass filename to svelte.preprocess #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default function svelte(options = {}) {
if (!filter(id)) return null;
if (!~extensions.indexOf(path.extname(id))) return null;

return (options.preprocess ? preprocess(code, options.preprocess) : Promise.resolve(code)).then(code => {
return (options.preprocess ? preprocess(code, Object.assign({}, options.preprocess, { filename : id })) : Promise.resolve(code)).then(code => {
const compiled = compile(
code.toString(),
Object.assign({}, {
Expand Down
10 changes: 7 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,22 @@ describe('rollup-plugin-svelte', () => {
it('preprocesses components', () => {
const { transform } = plugin({
preprocess: {
markup: ({ content }) => {
markup: ({ content, filename }) => {
return {
code: content.replace('__REPLACEME__', 'replaced')
code: content
.replace('__REPLACEME__', 'replaced')
.replace('__FILENAME__', filename)
};
}
}
});

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