Skip to content

Instantly share code, notes, and snippets.

@kasper573
Created December 8, 2017 14:04
Show Gist options
  • Save kasper573/0985bcb34beb11e741455fd4afef30c5 to your computer and use it in GitHub Desktop.
Save kasper573/0985bcb34beb11e741455fd4afef30c5 to your computer and use it in GitHub Desktop.
import {
FuseBox, EnvPlugin, QuantumPlugin, WebIndexPlugin, CSSPlugin,
JSONPlugin, Sparky, File, CopyPlugin, WorkFlowContext, BabelPlugin
} from "fuse-box";
import {BuildOptions, getCommonBuildOptions, getEnvironments, NodeEnv, OcastEnv} from "./BuildOptions";
/**
* Sets up a FuseBox ready to either run a development server or produce builds.
* All required configuration is already hardcoded in this function.
* You may however choose to pass in additional options to customize your dev or build experience to your liking.
*
* NOTE: Running this function only sets up and configures the Bundle and FuseBox interfaces.
* You will have to interact with these interfaces to proceed with serving or building.
* See fuse-box docs for more information.
*
* @returns {{vendor: Bundle; app: Bundle; fuse: FuseBox}}
*/
function setupFuse (ocastEnv: OcastEnv, nodeEnv: NodeEnv, options: BuildOptions = {}) {
const fuse = FuseBox.init({
homeDir: "app",
sourceMaps: {project: options.sourceMaps, vendor: false},
hash: options.hashFilenames,
modulesFolder: "app",
alias: {
src: "~/src/",
config: "~/config/"
},
target: "browser",
output: `${options.outputFolder}/$name.js`,
warnings: true,
cache: options.hmr, // fuse-box requires cache to be on to support hmr
log: options.log,
debug: options.debug,
tsConfig: "tsconfig.json",
useTypescriptCompiler: false,
plugins: [
EnvPlugin({
NODE_ENV: JSON.stringify(nodeEnv),
OCAST_CONFIG: JSON.stringify(ocastEnv)
}),
BabelPlugin({
test: /\.jsx?$/,
extensions: [".jsx", ".js"],
config: {
sourceMaps: options.sourceMaps,
presets: ["flow", "react", "stage-0"]
}
}),
WebIndexPlugin({title: "Ocast", path: "."}),
CopyPlugin({files: ["*.png", "*.jpg", "*.ogg"], dest: "assets", useDefault: false}),
CSSPlugin(),
JSONPlugin(),
options.minify ?
QuantumPlugin({
treeshake: true,
uglify: true
}) : undefined
]
});
const vendor = fuse.bundle("vendor");
//.instructions(`~ src/client.js`);
const app = fuse.bundle("app")
.instructions(`!> [src/client.js]`);
if (options.hmr) {
app.hmr();
}
return {vendor, app, fuse};
}
/**
* An extension of `setupFuse()` that also reads the ocast and node environment settings
* from the system environment variables and defaults BuildOptions to options
* common for the environments found (See `getCommonBuildOptions()`).
* You can extend these options using `additionalOptions`.
*/
function autoSetupFuse (additionalOptions: BuildOptions = {}) {
const {ocastEnv, nodeEnv} = getEnvironments();
const options = {
...getCommonBuildOptions(ocastEnv, nodeEnv),
...additionalOptions
};
return {
...setupFuse(ocastEnv, nodeEnv, options),
options
};
}
/**
* Serves ocast through the fuse-box development server.
*/
Sparky.task("dev-server", () => {
const {fuse, app} = autoSetupFuse();
app.watch();
fuse.dev();
return fuse.run();
});
/**
* Builds ocast to the configured output folder
*/
Sparky.task("build", () => {
const {fuse, options} = autoSetupFuse();
Sparky.src(options.outputFolder).clean(options.outputFolder);
return fuse.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment