Skip to content

Commit

Permalink
fix: 🐛 custom transformer not working with external src files
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Apr 2, 2019
1 parent 88e3ee9 commit cc037c3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-preprocess",
"version": "2.5.0",
"version": "2.5.1",
"license": "MIT",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down
12 changes: 6 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ exports.addLanguageAlias = entries =>
entries.forEach(entry => LANG_DICT.set(...entry))

exports.getLanguage = (attributes, defaultLang) => {
let lang
let lang = defaultLang

if (attributes.src) {
if (attributes.lang) {
lang = attributes.lang
} else if (attributes.type) {
lang = attributes.type.replace(/^(text|application)\/(.*)$/, '$2')
} else if (attributes.src) {
lang = attributes.src.match(/\.([^/.]+)$/)
lang = lang ? lang[1] : defaultLang
} else {
lang = attributes.type
? attributes.type.replace(/^(text|application)\/(.*)$/, '$2')
: attributes.lang || defaultLang
}

return {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/template.custom
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
23 changes: 13 additions & 10 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,11 @@ describe('detect - mimetype', () => {
{ type: 'application/ld+json', parser: 'ld+json' },
{ type: 'text/some-other', parser: 'some-other' },
{ lang: 'stylus', parser: 'stylus' },
{ src: '../foo.custom', lang: 'customLanguage', parser: 'customLanguage' },
]
MIMETYPES.forEach(({ type, lang, parser }) => {
it(`should detect ${type || lang} as ${parser}`, async () => {
expect(getLanguage({ type, lang }, 'javascript')).toEqual({
MIMETYPES.forEach(({ type, lang, src, parser }) => {
it(`should detect '${src || type || lang}' as '${parser}'`, async () => {
expect(getLanguage({ type, lang, src }, 'javascript')).toEqual({
lang: parser,
alias: parser,
})
Expand Down Expand Up @@ -330,17 +331,19 @@ describe('external files', () => {

describe('options', () => {
it('should accept custom method for a transformer', async () => {
const input = `<template src="./fixtures/template.pug"></template>`
const input = `<template lang="customTransformer">${getFixtureContent(
'template.custom',
)}</template>`
const opts = getPreprocess({
pug: (content, filename) => {
const code = require('pug').render(content, opts)
return {
code,
}
transformers: {
customTransformer({ content, filename }) {
content = content.replace('foo', 'bar')
return { code: content, map: null }
},
},
})
const preprocessed = (await preprocess(input, opts)).trim()
expect(preprocessed).toBe(parsedMarkup)
expect(preprocessed).toBe('bar')
})

it('should accept an options object as transformer value', async () => {
Expand Down

0 comments on commit cc037c3

Please sign in to comment.