Skip to content

Commit

Permalink
feat(LayoutPlugin): allow to pass manifest content to plugin, read ma…
Browse files Browse the repository at this point in the history
…nifest file on initialization (#40)
  • Loading branch information
ValeraS authored Mar 20, 2024
1 parent d0719d2 commit d2465b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export type {
GoogleAnalyticsCounter,
GoogleAnalyticsPluginOptions,
} from './plugins/google-analytics/index.js';
export type {LayoutPluginOptions} from './plugins/layout/index.js';
export type {LayoutPluginOptions, Manifest} from './plugins/layout/index.js';
40 changes: 28 additions & 12 deletions src/plugins/layout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,52 @@ import type {CommonOptions, Plugin} from '../../types.js';
import {getAbsoluteUrl, getJSONContent} from './helpers.js';
import type {LayoutPluginOptions, Manifest} from './types.js';

export type {LayoutPluginOptions} from './types.js';
export type {LayoutPluginOptions, Manifest} from './types.js';

export interface LayoutInitOptions {
publicPath?: string;
manifest: string | ((commonOptions: CommonOptions) => string);
manifest: string | Manifest | ((commonOptions: CommonOptions) => string | Manifest);
}
export function createLayoutPlugin({
publicPath = '/build/',
manifest,
}: LayoutInitOptions): Plugin<LayoutPluginOptions, 'layout'> {
let manifestContent: Manifest | undefined;
if (typeof manifest === 'string') {
manifestContent = getJSONContent(manifest, (err) => {
throw new Error(`Layout: Unable to read manifest file. ${err}`);
});
} else if (typeof manifest === 'object') {
manifestContent = manifest;
}
return {
name: 'layout',
apply({options, renderContent, commonOptions}) {
if (!options) {
return;
}

const manifestPath = typeof manifest === 'string' ? manifest : manifest(commonOptions);

const manifestFile: Manifest = getJSONContent(manifestPath, (err) => {
console.error('Unable to read manifest file', err);
process.exit(1);
});
if (typeof manifest === 'function') {
const m = manifest(commonOptions);
if (typeof m === 'string') {
manifestContent = getJSONContent(m, (err) => {
throw new Error(`Layout: Unable to read manifest file. ${err}`);
});
} else {
manifestContent = m;
}
}

function getWebpackAssetUrl(name: string) {
return getAbsoluteUrl(publicPath, manifestFile[name], options?.prefix);
if (!manifestContent || typeof manifestContent !== 'object') {
throw new Error('Layout: manifest content is not defined.');
}

renderContent.inlineScripts.unshift(
`window.__PUBLIC_PATH__ = ${htmlescape(publicPath)}`,
);

const entrypointSpec = manifestFile.entrypoints
? manifestFile.entrypoints[options.name]
const entrypointSpec = manifestContent.entrypoints
? manifestContent.entrypoints[options.name]
: undefined;
if (entrypointSpec && entrypointSpec.assets) {
const jsAssets = Array.isArray(entrypointSpec.assets.js)
Expand All @@ -63,6 +75,10 @@ export function createLayoutPlugin({
})),
);
} else {
const manifestEntries = manifestContent;
const getWebpackAssetUrl = (name: string) => {
return getAbsoluteUrl(publicPath, manifestEntries[name], options?.prefix);
};
renderContent.scripts.push(
{src: getWebpackAssetUrl('runtime.js'), defer: true, crossOrigin: 'anonymous'},
{src: getWebpackAssetUrl('vendors.js'), defer: true, crossOrigin: 'anonymous'},
Expand Down

0 comments on commit d2465b0

Please sign in to comment.