Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async write(generated: Object) {
let contents = generated.contents
let writeSourceMap = false
if (generated.sourceMap) {
if (this.config.cli.sourceMapInline) {
contents += `\n${sourceMapToComment(generated.sourceMap)}`
} else if (this.config.cli.sourceMapOutputFile) {
contents += `\n//# sourceMappingURL=${Path.relative(Path.dirname(this.config.cli.outputFile), this.config.cli.sourceMapOutputFile)}`
writeSourceMap = true
}
}
await writeFile(this.config.cli.outputFile, contents)
debugWrite(this.config.cli.outputFile)
if (writeSourceMap) {
await writeFile(this.config.cli.sourceMapOutputFile, JSON.stringify(generated.sourceMap, null, 2))
debugWrite(this.config.cli.sourceMapOutputFile)
}
}
dispose() {
const mappings = Object.assign({}, config.mappings, {
files: Object.assign({}, Helpers.getFileMappings(context, chunk, config), config.mappings.files),
})
chunks.push(`__sbPundle.registerMappings(${JSON.stringify(mappings)})`)
chunks.push(`__sbPundle.registerLoaded(${JSON.stringify(config.label)})`)
for (let i = 0, length = entries.length; i < length; i++) {
invariant(entries[i].resolved, `Entry file '${entries[i].request}' was not resolved`)
chunks.push(`__sbPundle.require('${Helpers.getFilePath(context, config, entries[i].resolved)}')`)
}
chunks.push('})();\n')
const sourceMap = chunksMap.toJSON()
if (config.sourceMap) {
if (config.sourceMapPath === 'inline') {
chunks.push(sourceMapToComment(sourceMap))
}
}
return {
chunk,
contents: chunks.join('\n'),
sourceMap,
filesGenerated,
}
}, {
label: '',
? Helpers.getChunkFilePath(entry.chunk, outputConfig.sourceMapTemplate)
: null
const resolved = path.resolve(outputConfig.rootDirectory, outputPath)
let resolvedSourceMapPath = sourceMapPath
if (resolvedSourceMapPath && resolvedSourceMapPath !== 'inline') {
resolvedSourceMapPath = path.resolve(outputConfig.rootDirectory, resolvedSourceMapPath)
}
let contentsToWrite = Buffer.from(entry.generated.contents)
if (resolvedSourceMapPath && entry.generated.sourceMap) {
const type = entry.chunk.format === '.css' ? 'css' : 'js'
let bottomLine
if (resolvedSourceMapPath === 'inline') {
bottomLine = sourceMapToComment(entry.generated.sourceMap, { type })
} else {
const bottomContents = `sourceMappingURL=${path.relative(path.dirname(resolved), resolvedSourceMapPath)}`
bottomLine = type === 'css' ? `/*# ${bottomContents} */` : `//# ${bottomContents}`
}
bottomLine = `\n${bottomLine}`
contentsToWrite = Buffer.concat([contentsToWrite, Buffer.from(bottomLine, 'utf8')])
}
invariant(
!outputPaths[entry.chunk.label],
'More than one outputs would use the same path. Try using some variables in output templates',
)
outputPaths[entry.chunk.label] = {
outputPath: resolved,
sourceMapPath: resolvedSourceMapPath,
}
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)}
`
generateSourceMap(options: ?ProcessorConfig, asComment: boolean = false): string {
if (!options) {
options = this.getProcessorOptions()
}
const sourceMap = generateSourceMap(this.compilation.pundle, options, this.gatherAllImports())
if (asComment) {
return sourceMapToComment(sourceMap)
}
return JSON.stringify(sourceMap)
}
getProcessorOptions(): ProcessorConfig {