@roots/sage
Version:
@roots/sage preset for @roots/bud
62 lines (61 loc) • 1.99 kB
JavaScript
import { __decorate } from "tslib";
import { urlToHttpOptions } from 'node:url';
import { Extension } from '@roots/bud-framework/extension';
import { bind, label } from '@roots/bud-framework/extension/decorators';
/**
* Acorn adapter
*
* @see {@link https://github.com/roots/acorn}
*/
let Acorn = class Acorn extends Extension {
/**
* Writes hmr.json for use with {@link https://github.com/roots/acorn/}
*/
addAcornHotManifest(compiler, compilation) {
const data = {
publicPath: this.app.publicPath().replace(/auto$/, ``),
};
if (this.app.root?.server?.publicUrl)
data.dev = urlToHttpOptions(this.app.root.server.publicUrl);
if (this.app.root?.server?.publicProxyUrl)
data.proxy = urlToHttpOptions(this.app.root.server.publicProxyUrl);
return () => {
const source = new compiler.webpack.sources.RawSource(JSON.stringify(data, null, 2));
compilation.emitAsset(`hmr.json`, source);
};
}
/**
* {@link WebpackPluginInstance.apply}
*/
apply(compiler) {
if (!this.app.isDevelopment)
return false;
compiler.hooks.thisCompilation.tap(`@roots/sage/acorn`, compilation => {
compilation.hooks.processAssets.tap({
name: `@roots/sage/acorn`,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
}, this.addAcornHotManifest(compiler, compilation));
});
return true;
}
/**
* {@link Extension.buildBefore}
*/
async buildBefore(bud) {
bud.entrypoints.set(`publicPath`, ``);
bud.manifest.set(`publicPath`, ``);
}
};
__decorate([
bind
], Acorn.prototype, "addAcornHotManifest", null);
__decorate([
bind
], Acorn.prototype, "apply", null);
__decorate([
bind
], Acorn.prototype, "buildBefore", null);
Acorn = __decorate([
label(`@roots/sage/acorn`)
], Acorn);
export default Acorn;