Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
prerender-vue-webpack-plugin
Advanced tools
A webpack plugin that allows you to pre-render your Vue applications and optionally inline critical CSS at build time without a headless browser
A Webpack plugin that allows you to prerender your Vue applications and optionally inline critical CSS at build time without a headless browser. For prerendering, this plugin depends on the bundle renderer from vue-server-renderer
and the bundle-source map file generated by vue-server-renderer/server-plugin
. For inlining critical CSS, the plugin uses Critters as a client.
As mentioned, this plugin depends on vue-server-renderer@>=2.x
to work. That package is defined as a peer dependency of this plugin, meaning you'll need to install it separately before using this plugin.
$ npm i -D vue-server-renderer
Next, bring the plugin into your project
$ npm i -D prerender-vue-webpack-plugin
Finally, import VueSSRServerPlugin and the plugin into your Webpack configuration and add it to your array of plugins.
// webpack.config.js
+const VueSSRServerPlugin = require('vue-server-renderer/server-plugin');
+const PrerenderVueWebpackPlugin = require('prerender-vue-webpack-plugin');
module.exports = {
plugins: [
+ new PrerenderVueWebpackPlugin({
+ // Configuration (see below)
+ }),
+ new VueSSRServerPlugin()
]
}
entry
<required, String> The entry corresponding to the emitted asset representing the Vue applicationtemplate
<required, String> The path to the template to be transformed.templateContext
<optional, Object, default: {}> The context data used in the bundle rendering processroot
<optional, String, default: {@link VUE_APP_ROOT}> The root element in the template used by the Vue applicationserverBundleFileName
<required, String, default: {@link SERVER_BUNDLE_FILE_NAME}> The file generated by vue-server-renderer/server-plugin
.overwrite
<optional, Boolean, default: false> If true, prioritizes the {@link this.template} path as the output path.outputPath
<optional, String, default: {@link this.compilerOutput}> The path to output the transformed HTML tooutputFileName
<optional, String, default: {@link this.entry}.html> The outputted file name for the transformed htmlhook
<optional, Function> Hook into the compilation process before any transformations begin.critters
<optional, Object> An object that allows you to set/overwrite options passed to the Critters client.inlineCSS
<optional, Object> Use Critters to inline critical CSS into the {@link this.template}.inlineCSS.entries
<optional, String[]> An array of entries/chunks whose CSS assets are used by the Vue app.inlineCSS.externals
<optional, String[]> An array of external assets that should be dynamically included in the compilation process via {@link this.hook}.inlineCSS.mergeCSS
<optional, Boolean, default: true> Merges the {@link entries} and {@link externals} into a single sheet and is passes to Critters. If false
, individual sheets from both groups are processed separately by Critters.new PrerenderVueWebpackPlugin({
entry: "details", // Vue application entry point
root: "#app", // The element from the template that the app hooks
template: "src/main/resources/templates/details.html", // path to template
})
plugins: [
new PrerenderVueWebpackPlugin({...}),
new PrerenderVueWebpackPlugin({...}),
]
new PrerenderVueWebpackPlugin({
entry: "details",
root: "#app",
template: "src/main/resources/templates/details.html",
+ templateContext: mockData, // Data passed to the template during bundle rendering
})
new PrerenderVueWebpackPlugin({
entry: "details",
root: "#app",
template: "src/main/resources/templates/details.html",
templateContext: mockData,
+ inlineCSS: {
+ // Entries/chunks corresponding to the app styles
+ entries: [
+ "global-style",
+ "details-style"
+ ],
+ }
})
new PrerenderVueWebpackPlugin({
entry: "details",
root: "#app",
template: "src/main/resources/templates/details.html",
templateContext: mockData,
inlineCSS: {
entries: [
"global-style",
"details-style"
],
},
+ critters: {
+ inlineFonts: true
+ }
})
Critters relies on stylesheets built into the Webpack compilation process. If we want to inline critical external stylesheets, we must add them to the compilation assets before Critters runs. This plugin provides a client hook
into the compilation process before transformations begin.
Perhaps our external stylesheets live on a server. Here is an example of incorporating them into the compilation for Critters. Here is the hook
:
const phin = require('phin');
const https = require('https');
const path = require('path');
const fsPromises = require('fs').promises;
/**
* Brings in a custom Webpack manifest to pull the URLs of the external
* CSS assets used by the application that we want to inline. Then, adds
* them to the {@link compilation} so that Critters can process them.
*
* @param {Object} compiler Webpack compiler object
* @param {Object} compilation Webpack compilation object
*
* @return {void}
*/
async function addExternalStylesheets(compiler, compilation) {
const { externals = {} } = config;
const customManifest = JSON.parse(await fsPromises.readFile(
path.resolve(compiler.options.output.path, 'manifest.json')
));
return Promise.all(
Object.keys(externals).map(async (externalKey) => {
const cssUrl = Object.values(customManifest[externalKey]).find(
asset => /\.css(\?[^.]+)?$/.test(asset)
) || '';
try {
const { body } = await phin({
url: cssUrl,
timeout: 2000,
core: {
agent: new https.Agent({
rejectUnauthorized: false,
}),
},
});
const cssSource = body.toString();
// eslint-disable-next-line no-param-reassign
compilation.assets[externalKey] = {
source() {
return cssSource;
},
size() {
return cssSource.length;
},
};
} catch (error) {
console.log(error);
}
})
);
}
Now we use the hook
:
new PrerenderVueWebpackPlugin({
entry: "details",
root: "#app",
template: "src/main/resources/templates/details.html",
templateContext: mockData,
+ // Add the hook
+ hook: addExternalStylesheets,
inlineCSS: {
entries: [
"global-style",
"details-style"
],
},
critters: {
inlineFonts: true
},
})
Lastly, we need to tell PrerenderVueWebpackPlugin
to pass these external stylesheets to Critters. The name of the sheets should match the asset/file name(s) that we included in the compilation. So, for the case of using the above hook, whatever the externalKey
s happen to be.
new PrerenderVueWebpackPlugin({
entry: "details",
root: "#app",
template: "src/main/resources/templates/details.html",
templateContext: mockData,
// Add the hook
hook: addExternalStylesheets,
inlineCSS: {
entries: [
"global-style",
"details-style"
],
+ externals: [
+ 'externalKey' // This should be an actual asset name
+ ]
},
critters: {
inlineFonts: true
},
})
[2.1.0] - 2020-11-20
serverBundle
when instantiating a bundle renderer rather than the raw bundle source. This ensures that dependent bundles of the Vue app are properly referenced and can be rendered. Associating serverBundle.entry
with {@link this.entry}
in the case where we have multiple Vue applications.apply
webpack hook, we write the contents of {@link this.template}
to the output path. This helps downstream processes that may rely on some form of output existing.mkdirp
to attempt to recursively create the directory passed as an option to the plugin instance (this.outputPath
). This helps guarantee that the directoy exists before attempting to write there.FAQs
A webpack plugin that allows you to pre-render your Vue applications and optionally inline critical CSS at build time without a headless browser
The npm package prerender-vue-webpack-plugin receives a total of 0 weekly downloads. As such, prerender-vue-webpack-plugin popularity was classified as not popular.
We found that prerender-vue-webpack-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.