Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let error: Error | null = null
nodeLess.render(
source,
{ ...options, syncImport: true },
(err: Error | null, output: any) => {
error = err
result = output
}
)
if (error) return { code: '', errors: [error] }
if (map) {
return {
code: result.css.toString(),
map: merge(map, result.map),
errors: []
}
}
return { code: result.css.toString(), errors: [] }
}
}
'RESOLVE_FAILED',
`Transformer '${transformer.name}' returned invalid result: ${error.errors.join(', ')}`,
)
}
throw error
}
let newSourceMap = null
if (transformed.sourceMap === false) {
newSourceMap = false
} else if (result.sourceMap === false) {
newSourceMap = false
} else if (result.sourceMap && !transformed.sourceMap) {
newSourceMap = result.sourceMap
} else if (result.sourceMap && transformed.sourceMap) {
newSourceMap = mergeSourceMap(transformed.sourceMap, result.sourceMap)
}
transformed = { sourceMap: newSourceMap, contents: result.contents }
}
const transformedSourceMap = transformed.sourceMap
if (transformedSourceMap && Array.isArray(transformedSourceMap.sources)) {
const sourceIndex = transformedSourceMap.sources.findIndex(source => source && filePath.endsWith(source))
if (sourceIndex !== -1) {
if (!transformedSourceMap.sourcesContent) {
transformedSourceMap.sourcesContent = []
}
transformedSourceMap.sourcesContent[sourceIndex] = typeof contents === 'string' ? contents : contents.toString()
}
}
return {
const aggregatedRoot = rootsArr.slice(1).reduce((entry, curr) => entry.append(curr), rootsArr[0])
if (options.scoped) {
aggregatedRoot.nodes.forEach(node => {
node.selector = `${node.selector}.${randomId}`
})
}
const results = aggregatedRoot.toResult({
map: options.sourceMap ? { inline: false, annotation: false } : false,
})
let css = results.css
if (results.map) {
const currentSourceMap = results.map.toJSON()
const sourceMap = file.sourceMap ? mergeSourceMap(file.sourceMap, currentSourceMap) : currentSourceMap
css += `\n${sourceMapToComment(sourceMap, { type: 'css' })}`
}
const processModule = await context.resolveSimple('process', file.filePath)
const ast = template.ast`
${file.imports.map(i => `require(${JSON.stringify(i)})`).join('\n')}
var style = document.createElement('style')
style.type = 'text/css'
if (require(${JSON.stringify(processModule)}).env.NODE_ENV === "development" && module.hot && module.hot.dispose) {
module.hot.dispose(function() {
style.remove()
})
}
style.textContent = ${JSON.stringify(css)}
document.head.appendChild(style)
module.exports = ${JSON.stringify(options.scoped ? randomId : null)}
...options,
compress: {
defaults: false,
...options.compress,
},
})
if (error) {
throw new Error(error)
}
return {
contents: code,
sourceMap:
sourceMap && map
? {
contents: JSON.stringify(mergeSourceMap(sourceMap.contents, map)),
}
: null,
}
},
})
const nodeSass = require('sass')
const finalOptions = {
...options,
data: source,
file: options.filename,
outFile: options.filename,
sourceMap: !!map
}
try {
const result = nodeSass.renderSync(finalOptions)
if (map) {
return {
code: result.css.toString(),
map: merge(map, JSON.parse(result.map.toString())),
errors: []
}
}
return { code: result.css.toString(), errors: [] }
} catch (e) {
return { code: '', errors: [e] }
}
}
}
mergeTransformation(contents: string, sourceMap: ?Object): void {
if (this.sourceMap && !sourceMap) {
this.sourceMap = null
} else if (!this.sourceMap && sourceMap) {
this.sourceMap = sourceMap
} else if (this.sourceMap && sourceMap) {
this.sourceMap = mergeSourceMap(this.sourceMap, sourceMap)
}
const newSourceMap = this.sourceMap
if (newSourceMap) {
newSourceMap.sources = [this.fileName]
newSourceMap.sourcesContent = [this.sourceContents.toString('utf8')]
}
this.contents = contents
}
addChunk(entry: Chunk): void {
mergeTransformation(contents: string, sourceMap: ?Object): void {
if (this.sourceMap && !sourceMap) {
this.sourceMap = null
} else if (!this.sourceMap && sourceMap) {
this.sourceMap = sourceMap
} else if (this.sourceMap && sourceMap) {
this.sourceMap = mergeSourceMap(this.sourceMap, sourceMap)
}
this.contents = contents
}
serialize(): Object {
export function mergeResult(file: File, result: ?{ contents: string, sourceMap: ?Object }): void {
if (!result) {
return
}
if (file.sourceMap && !result.sourceMap) {
file.sourceMap = null
} else if (file.sourceMap && result.sourceMap) {
file.sourceMap = mergeSourceMap(file.sourceMap, result.sourceMap)
} else if (!file.sourceMap && result.sourceMap) {
file.sourceMap = result.sourceMap
}
file.contents = result.contents
}
render(source, map, options) {
const nodeStylus = require('stylus')
try {
const ref = nodeStylus(source)
Object.keys(options).forEach(key => ref.set(key, options[key]))
if (map) ref.set('sourcemap', { inline: false, comment: false })
const result = ref.render()
if (map) {
return {
code: result,
map: merge(map, ref.sourcemap),
errors: []
}
}
return { code: result, errors: [] }
} catch (e) {
return { code: '', errors: [e] }
}
}
}
export function mergeResult(file: { contents: string, sourceMap: ?Object }, result: ?{ contents: string, sourceMap: ?Object }): void {
if (!result) {
return
}
if (file.sourceMap && !result.sourceMap) {
file.sourceMap = null
} else if (file.sourceMap && result.sourceMap) {
file.sourceMap = mergeSourceMap(file.sourceMap, result.sourceMap)
} else if (!file.sourceMap && result.sourceMap) {
file.sourceMap = result.sourceMap
}
file.contents = result.contents
}