fuse-box
Version:
Fuse-Box a bundler that does it right
78 lines (69 loc) • 1.82 kB
text/typescript
import { File } from "../core/File";
import { WorkFlowContext } from "../core/WorkflowContext";
import { Plugin } from "../core/WorkflowContext";
export interface HTMLPluginOptions {
useDefault?: boolean;
}
/**
*
*
* @export
* @class FuseBoxHTMLPlugin
* @implements {Plugin}
*/
export class FuseBoxHTMLPlugin implements Plugin {
private useDefault = true;
constructor(opts: HTMLPluginOptions = {}) {
if (opts.useDefault !== undefined) {
this.useDefault = opts.useDefault;
}
}
/**
*
*
* @type {RegExp}
* @memberOf FuseBoxHTMLPlugin
*/
public test: RegExp = /\.html$/;
/**
*
*
* @param {WorkFlowContext} context
*
* @memberOf FuseBoxHTMLPlugin
*/
public init(context: WorkFlowContext) {
context.allowExtension(".html");
}
/**
*
*
* @param {File} file
*
* @memberOf FuseBoxHTMLPlugin
*/
public transform(file: File) {
let context = file.context;
if (context.useCache) {
let cached = context.cache.getStaticCache(file);
if (cached) {
file.isLoaded = true;
file.contents = cached.contents;
return;
}
}
file.loadContents();
if (this.useDefault) {
file.contents = `module.exports.default = ${JSON.stringify(file.contents)}`;
} else {
file.contents = `module.exports = ${JSON.stringify(file.contents)}`;
}
if (context.useCache) {
context.emitJavascriptHotReload(file);
context.cache.writeStaticCache(file, file.sourceMap);
}
}
};
export const HTMLPlugin = (options?: HTMLPluginOptions) => {
return new FuseBoxHTMLPlugin(options);
};