Last active
May 4, 2019 13:17
-
-
Save FND/61abb2f10f46a08ae46f0ef1e313ca30 to your computer and use it in GitHub Desktop.
Svelte with faucet-pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
let sveltify = require("./faucet_svelte"); | |
module.exports = sveltify({ | |
svelte: [{ | |
source: "./src/index.js", | |
target: "./dist/bundle.js", | |
styles: "./dist/components.css", | |
dev: process.env.NODE_ENV !== "production" | |
}] | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
let svelte = require("rollup-plugin-svelte"); | |
let BasicBundle = require("faucet-pipeline-js/lib/bundle/basic"); | |
// Svelte bundles are just JavaScript bundles with additional configuration | |
// options (see below), so we extend the `js` section accordingly | |
// * `styles` is an optional file path for extracting CSS | |
// * `ssr` (boolean) activates server-side rendering | |
// * `dev` (boolean) enables run-time checks for development purposes | |
module.exports = config => { | |
let svelteConfig = config.svelte; | |
if(!svelteConfig) { | |
return; | |
} | |
injectPlugin(); | |
svelteConfig = svelteConfig.map((cfg, i) => { | |
let pluginConfig = { dev: !!cfg.dev }; | |
if(cfg.ssr) { | |
pluginConfig.generate = "ssr"; | |
} | |
let { styles } = cfg; | |
if(styles) { | |
// XXX: file path should be relative to config | |
pluginConfig.css = css => void css.write(styles, !!cfg.sourcemaps); | |
} | |
// TODO: use faucet-js's `extract`? | |
cfg = Object.assign({ | |
// XXX: hack to communicate between `BasicBundle` and individual | |
// bundle configurations (cf. `injectPlugin`) | |
sourcemaps: { | |
svelte: svelte(pluginConfig), | |
actual: cfg.sourcemaps | |
} | |
}, cfg); | |
delete cfg.styles; | |
delete cfg.ssr; | |
delete cfg.dev; | |
return cfg; | |
}); | |
config = Object.assign({}, config); | |
config.js = (config.js || []).concat(svelteConfig); | |
delete config.svelte; | |
return config; | |
}; | |
// hijacks faucet internals to inject plugin | |
function injectPlugin() { | |
let original = BasicBundle.prototype.compile; | |
BasicBundle.prototype.compile = function() { | |
if(this._svelte === undefined) { // initialize | |
let config = this._config; | |
// XXX: hacky; relies on faucet internals | |
let { sourcemap } = config.writeConfig; | |
let plugin = sourcemap && sourcemap.svelte; | |
if(plugin) { | |
config.readConfig.plugins.unshift(plugin); | |
config.writeConfig.sourcemap = sourcemap.actual; | |
this._svelte = true; | |
} else { | |
this._svelte = false; | |
} | |
} | |
return original.apply(this, arguments); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment