-
-
Notifications
You must be signed in to change notification settings - Fork 81
Drop dependency on CssWriter (discussion) #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is this change intended to fix a particular bug or implement a particular feature? Is this suggesting some code cleanup? |
I would say, it's a little bit all three // simplified
const MEMORY = {/* [filename]: code */}
const server = await polka() // http server
server.use(serveMemory(MEMORY)) // middleware
// ...
// cssWriterRef workaround to get to css
import config, { cssWriterRef } from './rollupConfig'
// destruct regular Rollup config for Rollup API
const { output: outputOptions, ...inputOptions } = config
const handleChange = () => {
//chokidar change callback
const bundle = await rollup.rollup(inputOptions)
const { output } = await bundle.generate(outputOptions)
// update MEMORY with output
} With such setup I can serve files from memory though only for browser (I wanted to update backend scripts dynamically with Rollup output as well, but as I use es modules, there is no way to create modules from strings yet, but it is different problem) 1) With plugin's current functionality I have to track outputs from |
Here is the updates to the code with more proper way of using plugins API. // ...
// css(writer);
// outputOptions as the second argument allows css function
// to use the options for custom css file name
let cssFilename = css(writer, outputOptions)
// If css returns nothing then do nothing more
// Backward compatibility
if (!cssFilename)
return
let cssName
if (cssFilename === true) {
const {file, name} = outputOptions
cssName = file ? {fileName : path.basename(file).replace(/\.[^/.]+$/, '') + '.css'}
: name ? {name : name.replace(/\.[^/.]+$/, '') + '.css'}
: null
} else {
cssName = {fileName: cssFilename}
}
if (outputOptions.sourcemap) {
const isInline = outputOptions.sourcemap === 'inline'
const map = {
version: 3 , sourcesContent,
file: null , sources,
names: [] , mappings: encode(mappings),
toString: () => { return JSON.stringify(map) },
toUrl: () => {
const encoding = 'base64'
return `data:application/json;charset=utf-8;${encoding},` +
Buffer.from(map.mappings).toString(encoding)
}
}
let mapURL
if (isInline) {
mapURL = map.toUrl()
} else {
const { fileName, name } = cssName || {}
const mapName = fileName ? {fileName: fileName + '.map'}
: name ? {name: name + '.map'}
: null
const mapAssetId =
this.emitFile({type: 'asset', source: map.toString(), ...mapName})
mapURL = this.getFileName(mapAssetId)
}
result += `\n/*# sourceMappingURL=${mapURL} */`
if (!isInline && !(cssName && (cssName.fileName || cssName.name))) // <------- (1)
this.warn(`output.sourcemap was set to true. Since neither output.(file|name) ` +
`was provided nor exact file name was returned from svelte.css, ` +
`default names for css and css.map were generated`)
}
this.emitFile({type: 'asset', source: result, ...cssName}) A couple of questions arose: // Current
/*1*/ css: false|null|undefined // -> do nothing
/*2*/ css: true // -> embed styles into js
/*3*/ css: (writer) => { /* use writer */ } // -> if return nothing, do nothing
// With my modification
/*4*/ css: (_, outputOptions) => { return "exact-filename.css" } // -> let Rollup save files
/*5*/ css: (writer) => { return true } // -> let the plugin derive names, let Rollup save files
// It would be convenient to have
/*5*/ css: "exact-filename.css" // -> similar to /*4*/ but the following does not allow The whole |
The This plugin no longer writes CSS output files at all. Instead, use Thank you for the ideas & feedback! |
Eliminate the catching of
CssWriter
in a scope around Rollup configuration and let Rollup handle files....
1)
Change
generateBundle()
togenerateBundle(outputOptions, bundle)
2)
Replace
css(writer);
at the end ofgenerateBundle
withthen use either of two approaches (as continuation of code above)
or
With this modification svelte plugin adds its output to Rollup bundle.
I did not research what actually svelte plugin does in
transform
. I suppose the plugin should be capable to prepare css files for Rollup chunks (code splitting) in the ideal case, which it does not do for now.The text was updated successfully, but these errors were encountered: