svelte-preprocess
Advanced tools
Comparing version 2.0.3 to 2.0.4
{ | ||
"name": "svelte-preprocess", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
# Svelte Preprocess | ||
> A customizable [Svelte](https://svelte.technology) preprocessor with support for: PostCSS, SCSS, Less, Stylus, Coffeescript and Pug. | ||
> A [Svelte](https://svelte.technology) preprocessor wrapper with support for: PostCSS, SCSS, Less, Stylus, Coffeescript and Pug. | ||
@@ -149,2 +149,2 @@ ## Installation | ||
</style> | ||
``` | ||
``` |
@@ -8,22 +8,13 @@ const stripIndent = require('strip-indent') | ||
isFn, | ||
parseXMLAttrString, | ||
parseAttributes, | ||
getSrcContent, | ||
throwUnsupportedError, | ||
getTagPattern, | ||
sliceReplace, | ||
} = require('./utils.js') | ||
const throwError = msg => { | ||
throw new Error(`[svelte-preprocess] ${msg}`) | ||
} | ||
const throwUnsupportedError = (lang, filename) => | ||
throwError(`Unsupported script language '${lang}' in file '${filename}'`) | ||
const TEMPLATE_PATTERN = getTagPattern('template') | ||
const templateTagPattern = new RegExp( | ||
'(<template([\\s\\S]*?)>([\\s\\S]*?)<\\/template>)', | ||
) | ||
module.exports = ({ | ||
onBefore, | ||
transformers = {}, | ||
aliases = undefined, | ||
} = {}) => { | ||
if (aliases) { | ||
module.exports = ({ onBefore, transformers = {}, aliases } = {}) => { | ||
if (aliases && aliases.length) { | ||
addLanguageAlias(aliases) | ||
@@ -44,3 +35,5 @@ } | ||
if (lang === targetLanguage) { | ||
return { code: content } | ||
return { | ||
code: content, | ||
} | ||
} | ||
@@ -63,3 +56,3 @@ | ||
const templateMatch = content.match(templateTagPattern) | ||
const templateMatch = content.match(TEMPLATE_PATTERN) | ||
@@ -71,5 +64,5 @@ /** If no <template> was found, just return the original markup */ | ||
let [, wholeTemplateTag, unparsedAttrs, templateCode] = templateMatch | ||
let [, attributes, templateCode] = templateMatch | ||
const attributes = parseXMLAttrString(unparsedAttrs) | ||
attributes = parseAttributes(attributes) | ||
const lang = getLanguage(attributes, 'html') | ||
@@ -83,3 +76,5 @@ | ||
if (lang === 'html') { | ||
return { code: content.replace(wholeTemplateTag, templateCode) } | ||
return { | ||
code: sliceReplace(templateMatch, content, templateCode), | ||
} | ||
} | ||
@@ -98,3 +93,3 @@ | ||
return { | ||
code: content.replace(wholeTemplateTag, code), | ||
code: sliceReplace(templateMatch, content, code), | ||
} | ||
@@ -101,0 +96,0 @@ }) |
const { readFileSync } = require('fs') | ||
const { resolve, dirname } = require('path') | ||
const transformers = {} | ||
const CWD = process.cwd() | ||
const PATHS = { | ||
CWD, | ||
MODULES: resolve(CWD, 'node_modules'), | ||
} | ||
const LANG_DICT = new Map([ | ||
@@ -12,32 +18,36 @@ ['postcss', 'css'], | ||
const cwd = process.cwd() | ||
const paths = { | ||
cwd, | ||
modules: resolve(cwd, 'node_modules'), | ||
exports.throwError = msg => { | ||
throw new Error(`[svelte-preprocess] ${msg}`) | ||
} | ||
/** Paths used by preprocessors to resolve @imports */ | ||
exports.getIncludePaths = fromFilename => | ||
[ | ||
paths.cwd, | ||
fromFilename.length && dirname(fromFilename), | ||
paths.modules, | ||
].filter(Boolean) | ||
exports.throwUnsupportedError = (lang, filename) => | ||
exports.throwError( | ||
`Unsupported script language '${lang}' in file '${filename}'`, | ||
) | ||
exports.isFn = maybeFn => typeof maybeFn === 'function' | ||
exports.getSrcContent = (rootFile, path) => | ||
readFileSync(resolve(dirname(rootFile), path)).toString() | ||
/** Gets a pattern for mathing <(tag) (attrs="values")>(content)</tag> */ | ||
exports.getTagPattern = type => | ||
new RegExp(`<${type}([\\s\\S]*?)>([\\s\\S]*?)<\\/${type}>`) | ||
exports.parseXMLAttrString = unparsedAttrStr => { | ||
unparsedAttrStr = unparsedAttrStr.trim() | ||
return unparsedAttrStr.length > 0 | ||
? unparsedAttrStr.split(' ').reduce((acc, entry) => { | ||
const [key, value] = entry.split('=') | ||
acc[key] = value.replace(/['"]/g, '') | ||
return acc | ||
}, {}) | ||
: {} | ||
} | ||
/** Replace a string with another value by slicing it based on a regexp match */ | ||
exports.sliceReplace = (match, str, replaceValue) => | ||
str.slice(0, match.index) + | ||
replaceValue + | ||
str.slice(match.index + match[0].length) | ||
exports.getSrcContent = (importerFile, srcPath) => | ||
readFileSync(resolve(dirname(importerFile), srcPath)).toString() | ||
exports.parseAttributes = attrsStr => | ||
attrsStr | ||
.split(/\s+/) | ||
.filter(Boolean) | ||
.reduce((acc, attr) => { | ||
const [name, value] = attr.split('=') | ||
acc[name] = value ? value.replace(/['"]/g, '') : true | ||
return acc | ||
}, {}) | ||
exports.addLanguageAlias = entries => | ||
@@ -61,3 +71,9 @@ entries.forEach(entry => LANG_DICT.set(...entry)) | ||
const transformers = {} | ||
/** Paths used by preprocessors to resolve @imports */ | ||
exports.getIncludePaths = fromFilename => | ||
[ | ||
PATHS.CWD, | ||
fromFilename.length && dirname(fromFilename), | ||
PATHS.MODULES, | ||
].filter(Boolean) | ||
@@ -79,8 +95,4 @@ exports.runTransformer = (name, maybeFn, { content, filename }) => { | ||
} catch (e) { | ||
throw new Error( | ||
`[svelte-preprocess] Error transforming '${name}'. Message:\n${ | ||
e.message | ||
}`, | ||
) | ||
exports.throwError(`Error transforming '${name}'. Message:\n${e.message}`) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12800
286
150