Skip to content

Commit

Permalink
add dependencies to result
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasily Mikhaylovsky authored and kaisermann committed Feb 12, 2019
1 parent 1488dae commit f3d1ef1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
35 changes: 22 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
isFn,
parseAttributes,
getSrcContent,
resolveSrc,
throwUnsupportedError,
getTagPattern,
sliceReplace,
Expand Down Expand Up @@ -48,12 +49,13 @@ module.exports = ({
return optionsCache[alias]
}

const getTransformerTo = targetLanguage => ({
const getTransformerTo = targetLanguage => async ({
content = '',
attributes,
filename,
}) => {
const { lang, alias } = getLanguage(attributes, targetLanguage)
const dependencies = []

if (preserve.includes(lang) || preserve.includes(alias)) {
return
Expand All @@ -63,13 +65,15 @@ module.exports = ({
if (attributes.src.match(/^(https?)?:?\/\/.*$/)) {
return
}

content = getSrcContent(filename, attributes.src)
const file = resolveSrc(filename, attributes.src)
content = await getSrcContent(file)
dependencies.push(file)
}

if (lang === targetLanguage) {
return {
code: content,
dependencies,
}
}

Expand All @@ -80,13 +84,15 @@ module.exports = ({
throwUnsupportedError(alias, filename)
}

return runTransformer(lang, getTransformerOpts(lang, alias), {
const result = await runTransformer(lang, getTransformerOpts(lang, alias), {
content: stripIndent(content),
filename,
})
result.dependencies = dependencies
return result
}

const markupTransformer = ({ content, filename }) => {
const markupTransformer = async ({ content, filename }) => {
if (isFn(onBefore)) {
content = onBefore({ content, filename })
}
Expand All @@ -95,30 +101,34 @@ module.exports = ({

/** If no <template> was found, just return the original markup */
if (!templateMatch) {
return { code: content }
return { code: content, }
}

let [, attributes, templateCode] = templateMatch

attributes = parseAttributes(attributes)
const { lang, alias } = getLanguage(attributes, 'html')
const dependencies = []

if (attributes.src) {
templateCode = getSrcContent(filename, attributes.src)
const file = resolveSrc(filename, attributes.src)
templateCode = await getSrcContent(file)
dependencies.push(file)
}

/** If language is HTML, just remove the <template></template> tags */
if (lang === 'html') {
return {
code: sliceReplace(templateMatch, content, templateCode),
dependencies,
}
}

if (transformers[lang] === false) {
throwUnsupportedError(lang, filename)
}

const preProcessedContent = runTransformer(
const { code } = await runTransformer(
lang,
getTransformerOpts(lang, alias),
{
Expand All @@ -127,11 +137,10 @@ module.exports = ({
},
)

return Promise.resolve(preProcessedContent).then(({ code }) => {
return {
code: sliceReplace(templateMatch, content, code),
}
})
return {
code: sliceReplace(templateMatch, content, code),
dependencies,
}
}

const scriptTransformer = getTransformerTo('javascript')
Expand Down
15 changes: 12 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { readFileSync } = require('fs')
const { readFile } = require('fs')
const { resolve, dirname } = require('path')

const transformers = {}
Expand Down Expand Up @@ -42,8 +42,17 @@ exports.sliceReplace = (match, str, replaceValue) =>
replaceValue +
str.slice(match.index + match[0].length)

exports.getSrcContent = (importerFile, srcPath) =>
readFileSync(resolve(dirname(importerFile), srcPath)).toString()
exports.resolveSrc = (importerFile, srcPath) =>
resolve(dirname(importerFile), srcPath)

exports.getSrcContent = (file) => {
return new Promise((resolve, reject) => {
readFile(file, (error, data) => {
if (error) reject(error)
else resolve(data.toString())
})
})
}

exports.parseAttributes = attrsStr =>
attrsStr
Expand Down

0 comments on commit f3d1ef1

Please sign in to comment.