Skip to content

Commit 7e03d4b

Browse files
committed
Merge branch 'hotfix/1.0.1'
2 parents 46e32f7 + 0986565 commit 7e03d4b

File tree

7 files changed

+96
-32
lines changed

7 files changed

+96
-32
lines changed

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ covers a vast majority of projects where the main file isn't touched.
7373
### Use a headless browser for rendering
7474

7575
```bash
76-
? Use a headless browser to render the application (recommended) Yes
76+
? Use a headless browser to render the application? (recommended) Yes
7777
```
7878

7979
This option is there for debugging purposes, but **should not be enabled**
@@ -83,7 +83,8 @@ Since the plugin configuration object isn't available, it is available here.
8383

8484
#### What it does to your project
8585

86-
The `headless` value of the configuration object is set to the answer to the question.
86+
The `headless` value of the configuration object is set to the answer to the
87+
question.
8788

8889
### Only pre-render for production builds
8990

@@ -100,6 +101,38 @@ However, there may be cases where you want to test the pre-rendering itself,
100101
and switching to a production build isn't the solution - you may then turn off
101102
that option.
102103

104+
### Indirect options
105+
106+
#### Parallel / Mutli-threaded
107+
108+
This option is configured from within the Vue CLI itself, but serves to a whole
109+
host of plugins to determine whether to turn on parallel jobs / multi-threading.
110+
111+
This plugin uses it to tell `prerender-spa-plugin` to render pages concurently
112+
(meaning in parallel) or not by setting the `maxConcurrentRoutes` parameter to
113+
either 1 or 4, if the build is respectively single-threaded or multi-threaded.
114+
115+
### Custom configuration
116+
117+
After being invoked, the plugin saves a file named `.prerender-spa.json` in the
118+
root directory of the project; where you can specify custom options for the
119+
Puppeteer renderer. It will be merged, and its options will overwrite those set
120+
by the plugin itself.
121+
122+
Exemple configuration:
123+
124+
```json
125+
{
126+
"renderRoutes": ["/", "/about"],
127+
"useRenderEvent": true,
128+
"headless": true,
129+
"onlyProduction": true,
130+
"customRendererConfig": {
131+
"renderAfterDocumentEvent": "my-custom-event"
132+
}
133+
}
134+
```
135+
103136
## Contributing
104137

105138
You are very welcome to contribute. To ask for a feature, or submit a bug, use

generator.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

generator/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = (api, options) => {
2+
api.extendPackage({
3+
devDependencies: {
4+
"prerender-spa-plugin": "^3.2.1"
5+
}
6+
});
7+
8+
api.onCreateComplete(() => {
9+
const fs = require("fs");
10+
const configPath = api.resolve("./.prerender-spa.json");
11+
fs.writeFileSync(configPath, JSON.stringify(options), {
12+
encoding: "utf-8"
13+
});
14+
15+
if (options.useRenderEvent) {
16+
const ext = api.hasPlugin("typescript") ? "ts" : "js";
17+
const mainPath = api.resolve(`./src/main.${ext}`);
18+
19+
const mainFileLines = fs
20+
.readFileSync(mainPath, { encoding: "utf-8" })
21+
.split(/\r?\n/g)
22+
.reverse();
23+
const vueRenderIndex = mainFileLines.findIndex(line =>
24+
line.match(/render\:/)
25+
);
26+
if (!mainFileLines[vueRenderIndex].endsWith(","))
27+
mainFileLines[vueRenderIndex] += ",";
28+
mainFileLines[vueRenderIndex] += '\n mounted: () => document.dispatchEvent(new Event("x-app-rendered")),';
29+
fs.writeFileSync(mainPath, mainFileLines.reverse().join("\n"), {
30+
encoding: "utf-8"
31+
});
32+
}
33+
});
34+
};

index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,37 @@ const path = require("path");
22
const PrerenderSPAPlugin = require("prerender-spa-plugin");
33
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
44

5-
module.exports = api => {
5+
module.exports = (api, projectOptions) => {
6+
const fs = require("fs");
7+
const options = JSON.parse(
8+
fs.readFileSync(api.resolve("./.prerender-spa.json", { encoding: "utf-8" }))
9+
);
610
api.chainWebpack(config => {
711
if (options.onlyProduction && process.env.NODE_ENV !== "production") {
812
return;
913
}
1014
let rendererConfig = {
11-
headless: config.headless,
12-
maxConcurrentRoutes: config.parallel ? 4 : 1
15+
headless: options.headless,
16+
maxConcurrentRoutes: options.parallel ? 4 : 1
1317
};
14-
if (config.useRenderEvent) {
18+
if (options.useRenderEvent) {
1519
rendererConfig["renderAfterDocumentEvent"] = "x-app-rendered";
1620
}
1721

22+
if (options.customRendererConfig) {
23+
Object.assign(rendererConfig, options.customRendererConfig);
24+
}
25+
1826
config.plugin("pre-render").use(PrerenderSPAPlugin, [
1927
{
20-
staticDir: api.resolve('./dist'),
28+
outputDir: api.resolve(projectOptions.outputDir),
29+
staticDir: api.resolve(projectOptions.outputDir),
30+
assetsDir: api.resolve(
31+
path.join(projectOptions.outputDir, projectOptions.assetsDir)
32+
),
33+
indexPath: api.resolve(
34+
path.join(projectOptions.outputDir, projectOptions.indexPath)
35+
),
2136
routes: options.renderRoutes,
2237
renderer: new Renderer(rendererConfig)
2338
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "vue-cli-plugin-prerender-spa",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Vue CLI plugin to add prerendering to your application",
55
"main": "index.js",
66
"author": "Nathan Graule",
77
"license": "MIT",
88
"private": false,
99
"dependencies": {
10+
"javascript-stringify": "^1.6.0",
1011
"prerender-spa-plugin": "^3.2.1"
1112
}
1213
}

prompts.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ module.exports = [
1010
filter: input =>
1111
new Promise((resolve, reject) => {
1212
const dataArray = input.split(",").map(value => value.trim());
13-
const dataUnique = dataArray.filter((value, pos, self) => {
14-
return self.indexOf(value) === pos;
15-
})
16-
resolve(dataUnique);
13+
resolve(dataArray);
1714
})
1815
},
1916
{

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ isobject@^3.0.0, isobject@^3.0.1:
642642
version "3.0.1"
643643
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
644644

645+
javascript-stringify@^1.6.0:
646+
version "1.6.0"
647+
resolved "https://registry.yarnpkg.com/javascript-stringify/-/javascript-stringify-1.6.0.tgz#142d111f3a6e3dae8f4a9afd77d45855b5a9cce3"
648+
645649
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
646650
version "3.2.2"
647651
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"

0 commit comments

Comments
 (0)