svelte-preprocess
Advanced tools
Comparing version 1.1.2 to 1.1.3
{ | ||
"name": "svelte-preprocess", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"license": "MIT", | ||
@@ -22,3 +22,5 @@ "main": "src/index.js", | ||
"test:watch": "jest --no-cache --verbose --watchAll", | ||
"release": "npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish", | ||
"lint": "eslint \"*.js\" \"src/**/*.js\"", | ||
"format": "prettier --loglevel silent --write \"*.js\" \"src/**/*.js\" && eslint --fix \"*.js\" \"src/**/*.js\"", | ||
"release": "npm run format && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish", | ||
"prepublishOnly": "npm run test" | ||
@@ -25,0 +27,0 @@ }, |
@@ -10,22 +10,21 @@ const stripIndent = require('strip-indent') | ||
parseXMLAttrString, | ||
getSrcContent | ||
getSrcContent, | ||
} = require('./utils.js') | ||
const throwError = (msg) => { throw new Error(`[svelte-preprocess] ${msg}`) } | ||
const throwError = msg => { | ||
throw new Error(`[svelte-preprocess] ${msg}`) | ||
} | ||
const throwUnsupportedError = (lang, filename) => | ||
throwError(`Unsupported script language '${lang}' in file '${filename}'`) | ||
const templateTagPattern = new RegExp('(<template([\\s\\S]*?)>([\\s\\S]*?)<\\/template>)') | ||
const templateTagPattern = new RegExp( | ||
'(<template([\\s\\S]*?)>([\\s\\S]*?)<\\/template>)', | ||
) | ||
module.exports = ({ | ||
onBefore, | ||
languages = {}, | ||
aliases = undefined | ||
} = {}) => { | ||
if(aliases) { | ||
module.exports = ({ onBefore, languages = {}, aliases = undefined } = {}) => { | ||
if (aliases) { | ||
appendLanguageAliases(aliases) | ||
} | ||
const getAssetParser = (targetLanguage) => { | ||
const getAssetParser = targetLanguage => { | ||
return ({ content = '', attributes, filename }) => { | ||
@@ -44,3 +43,8 @@ const lang = getLanguage(attributes, targetLanguage) | ||
const preProcessedContent = runPreprocessor(lang, languages[lang], content, filename) | ||
const preProcessedContent = runPreprocessor( | ||
lang, | ||
languages[lang], | ||
content, | ||
filename, | ||
) | ||
@@ -59,5 +63,5 @@ if (isPromise(preProcessedContent)) { | ||
const markupParser = ({content, filename}) => { | ||
const markupParser = ({ content, filename }) => { | ||
if (isFn(onBefore)) { | ||
content = onBefore({content, filename}) | ||
content = onBefore({ content, filename }) | ||
} | ||
@@ -81,6 +85,7 @@ const templateMatch = content.match(templateTagPattern) | ||
} else if (lang !== 'html') { | ||
const preProcessedContent = runPreprocessor(lang, | ||
const preProcessedContent = runPreprocessor( | ||
lang, | ||
languages[lang], | ||
stripIndent(templateCode), | ||
filename | ||
filename, | ||
) | ||
@@ -109,4 +114,4 @@ | ||
style: getAssetParser('css'), | ||
markup: markupParser | ||
markup: markupParser, | ||
} | ||
} |
const coffeescript = require('coffeescript') | ||
module.exports = function (content, filename, opts) { | ||
module.exports = function(content, filename, opts) { | ||
const { js: code, sourceMap: map } = coffeescript.compile(content, { | ||
filename, | ||
sourceMap: true, | ||
...opts | ||
...opts, | ||
}) | ||
@@ -9,0 +9,0 @@ |
const less = require('less/lib/less-node') | ||
module.exports = function (content, filename, opts) { | ||
return less.render(content, { sourceMap: {}, ...opts }) | ||
.then(output => ({ code: output.css, map: output.map })) | ||
module.exports = function(content, filename, opts) { | ||
return less | ||
.render(content, { | ||
sourceMap: {}, | ||
...opts, | ||
}) | ||
.then(output => ({ | ||
code: output.css, | ||
map: output.map, | ||
})) | ||
} |
const pug = require('pug') | ||
module.exports = function (content, filename, opts) { | ||
module.exports = function(content, filename, opts) { | ||
const code = pug.render(content, opts) | ||
return { code } | ||
return { | ||
code, | ||
} | ||
} |
const sass = require('node-sass') | ||
module.exports = function (content, filename, opts = { | ||
includePaths: ['node_modules'] | ||
}) { | ||
const { getIncludePaths } = require('../utils.js') | ||
module.exports = function( | ||
content, | ||
filename, | ||
opts = { | ||
includePaths: getIncludePaths(filename), | ||
}, | ||
) { | ||
return new Promise((resolve, reject) => { | ||
@@ -12,3 +18,3 @@ sass.render( | ||
outFile: filename + '.css', | ||
...opts | ||
...opts, | ||
}, | ||
@@ -20,7 +26,7 @@ (err, result) => { | ||
code: result.css.toString(), | ||
map: result.map.toString() | ||
map: result.map.toString(), | ||
}) | ||
} | ||
}, | ||
) | ||
}) | ||
} |
const stylus = require('stylus') | ||
module.exports = function (content, filename, opts = { | ||
paths: ['node_modules'] | ||
}) { | ||
const { getIncludePaths } = require('../utils.js') | ||
module.exports = function( | ||
content, | ||
filename, | ||
opts = { | ||
paths: getIncludePaths(filename), | ||
}, | ||
) { | ||
return new Promise((resolve, reject) => { | ||
@@ -10,3 +16,3 @@ const style = stylus(content, { | ||
sourcemap: true, | ||
...opts | ||
...opts, | ||
}) | ||
@@ -18,3 +24,3 @@ style.render((err, css) => { | ||
code: css, | ||
map: style.sourcemap | ||
map: style.sourcemap, | ||
}) | ||
@@ -21,0 +27,0 @@ }) |
const { existsSync, readFileSync } = require('fs') | ||
const { resolve, dirname } = require('path') | ||
const cwd = process.cwd() | ||
const paths = { | ||
cwd, | ||
modules: resolve(cwd, 'node_modules'), | ||
} | ||
/** Paths used by preprocessors to resolve @imports */ | ||
exports.getIncludePaths = fromFilename => | ||
[ | ||
paths.cwd, | ||
fromFilename.length && dirname(fromFilename), | ||
paths.modules, | ||
].filter(Boolean) | ||
exports.isFn = maybeFn => typeof maybeFn === 'function' | ||
exports.isPromise = maybePromise => Promise.resolve(maybePromise) === maybePromise | ||
exports.isPromise = maybePromise => | ||
Promise.resolve(maybePromise) === maybePromise | ||
exports.getSrcContent = (rootFile, path) => readFileSync(resolve(dirname(rootFile), path)).toString() | ||
exports.getSrcContent = (rootFile, path) => | ||
readFileSync(resolve(dirname(rootFile), path)).toString() | ||
@@ -14,6 +30,6 @@ exports.parseXMLAttrString = unparsedAttrStr => { | ||
? unparsedAttrStr.split(' ').reduce((acc, entry) => { | ||
const [key, value] = entry.split('=') | ||
acc[key] = value.replace(/['"]/g, '') | ||
return acc | ||
}, {}) | ||
const [key, value] = entry.split('=') | ||
acc[key] = value.replace(/['"]/g, '') | ||
return acc | ||
}, {}) | ||
: {} | ||
@@ -26,6 +42,7 @@ } | ||
['js', 'javascript'], | ||
['coffee', 'coffeescript'] | ||
['coffee', 'coffeescript'], | ||
]) | ||
exports.appendLanguageAliases = entries => entries.forEach(entry => LANG_DICT.set(...entry)) | ||
exports.appendLanguageAliases = entries => | ||
entries.forEach(entry => LANG_DICT.set(...entry)) | ||
@@ -41,3 +58,3 @@ exports.getLanguage = (attributes, defaultLang) => { | ||
'text/', | ||
'' | ||
'', | ||
) | ||
@@ -56,14 +73,12 @@ } | ||
let preprocessOpts = (maybeFn && maybeFn.constructor === Object) | ||
? maybeFn | ||
: {} | ||
const preprocessOpts = | ||
maybeFn && maybeFn.constructor === Object ? maybeFn : undefined | ||
try { | ||
preprocessorModules[lang] = preprocessorModules[lang] || require(`./langs/${lang}.js`) | ||
preprocessorModules[lang] = | ||
preprocessorModules[lang] || require(`./langs/${lang}.js`) | ||
return preprocessorModules[lang](content, filename, preprocessOpts) | ||
} catch (e) { | ||
throw new Error( | ||
`[svelte-preprocess] Error processing '${lang}'. Message:\n${ | ||
e.message | ||
}` | ||
`[svelte-preprocess] Error processing '${lang}'. Message:\n${e.message}`, | ||
) | ||
@@ -73,3 +88,3 @@ } | ||
exports.findPackageJson = function (startDir) { | ||
exports.findPackageJson = function(startDir) { | ||
let dir | ||
@@ -85,3 +100,3 @@ let nextDir = startDir | ||
filename: pkgfile, | ||
data: JSON.parse(readFileSync(pkgfile)) | ||
data: JSON.parse(readFileSync(pkgfile)), | ||
} | ||
@@ -88,0 +103,0 @@ } |
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
12069
273