├── .babelrc
├── .gitignore
├── README.md
├── dist
├── about
│ └── index.html
├── index.html
├── index.js
└── repos
│ └── index.html
├── index.js
├── modules
├── About.js
├── App.js
├── Home.js
├── NavLink.js
├── Repo.js
├── Repos.js
└── routes.js
├── package.json
├── server.js
├── template.js
├── webpack.config.js
└── webpack.server.config.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "es2015",
4 | "react",
5 | "stage-0"
6 | ],
7 | "plugins": []
8 | }
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | npm-debug.log
3 | server.bundle.js
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This repo is a demo of [static-site-generator-webpack-plugin](https://github.com/markdalgleish/static-site-generator-webpack-plugin), showing you how to generate a static site with React, React-Router, and Webpack.
2 |
3 | It is inspired by Jxnblk's article ["Static Site Generation With React Aad Webpack"](http://jxnblk.com/writing/posts/static-site-generation-with-react-and-webpack/).
4 |
5 | ## Usage
6 |
7 | First, clone the repo.
8 |
9 | ```bash
10 | $ git clone https://github.com/ruanyf/webpack-static-site-demo.git
11 | ```
12 |
13 | Second, install the dependencies.
14 |
15 | ```bash
16 | $ cd webpack-static-site-demo
17 | $ npm intall
18 | ```
19 |
20 | Third, generate the static files.
21 |
22 | ```bash
23 | $ npm run build
24 | ```
25 |
26 | Now, you should see the static files under the `dist` subdirectory.
27 |
28 | Finally, run the server.
29 |
30 | ```bash
31 | $ npm start
32 | ```
33 |
34 | Visit http://localhost:8080 . You will find it serves a static site, and at the same time has the experience of single page App.
35 |
36 | ## Explanation
37 |
38 | ### Webpack config
39 |
40 | The `static-site-generator-webpack-plugin` add a handler function into webpack's configure.
41 |
42 | ```javascript
43 | var StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
44 |
45 | module.exports = {
46 | // ...
47 | plugins: [
48 | new StaticSiteGeneratorPlugin('main', paths, {template: template, bundlejs: 'bundle.js'})
49 | ]
50 | };
51 | ```
52 |
53 | Note you have to compile the Webpack's output module into the format of UMD or CommonJS.
54 |
55 | ```javascript
56 | output: {
57 | filename: 'index.js',
58 | path: 'dist',
59 | /* IMPORTANT!
60 | * You must compile to UMD or CommonJS
61 | * so it can be required in a Node context: */
62 | libraryTarget: 'umd'
63 | },
64 | ```
65 |
66 | ### Constructor's Arguments
67 |
68 | `static-site-generator-webpack-plugin`'s constructor accepts three arguments.
69 |
70 | ```javascript
71 | function StaticSiteGeneratorWebpackPlugin(renderSrc, outputPaths, locals) {
72 | this.renderSrc = renderSrc;
73 | this.outputPaths = Array.isArray(outputPaths) ? outputPaths : [outputPaths];
74 | this.locals = locals;
75 | }
76 | ```
77 |
78 | (1) `renderSrc`
79 |
80 | `renderSrc` is asset file's name or chunk name. For example, `webpack.config.js` looks like the following.
81 |
82 | ```javascript
83 | // webpack.config.js
84 | entry: {
85 | main: './index.js'
86 | },
87 | output: {
88 | path: 'public',
89 | filename: 'bundle.js',
90 | libraryTarget: 'umd'
91 | }
92 | ```
93 |
94 | Then the `renderSrc` could be `main` or `bundle.js`.
95 |
96 | (2) `outputPaths`
97 |
98 | `outputPaths` is an array which comprises the static site's paths.
99 |
100 | ```javascript
101 | var paths = [
102 | '/',
103 | '/app/',
104 | '/inbox/',
105 | '/calendar/'
106 | ];
107 | ```
108 |
109 | if your `outputPaths` is the above, output will be the following.
110 |
111 | - /index.html
112 | - /app/index.html
113 | - /inbox/index.html
114 | - /calendar/index.html
115 |
116 | If the providing paths end in `.html`, you can generate custom file names other than the default `index.html`.
117 |
118 | ```javascript
119 | var paths = [
120 | '/a.html',
121 | '/app/b.html',
122 | '/inbox/c.html',
123 | '/calendar/d.html'
124 | ];
125 | ```
126 |
127 | (3) locals
128 |
129 | `locals` is an object which you put every extra property into.
130 |
131 | ```javascript
132 | plugins: [
133 | new StaticSiteGeneratorPlugin('index.js', paths, { template: template }),
134 | ]
135 | ```
136 |
137 | In the above code, a `template` property could be get from `locals`.
138 |
139 | You also can get three default properties from `locals`.
140 |
141 | - `locals.path`: The path currently being rendered
142 | - `locals.assets`: An object containing all assets
143 | - `locals.webpackStats`: Webpack's stats object
144 |
145 | ### Entry file
146 |
147 | The entry JavaScript file looks like the following.
148 |
149 | ```javascript
150 | // Client render (optional):
151 | if (typeof document !== 'undefined') {
152 | // Client render code goes here...
153 | }
154 |
155 | // Exported static site renderer:
156 | module.exports = function render(locals, callback) {
157 | callback(null, locals.template({ html: '
' + locals.path + '
' }));
158 | };
159 | ```
160 |
161 | The entry file should be a CommonJS module which exports a function to generate the static html file.
162 |
163 | The exported function receives two arguments:
164 |
165 | - `locals` object. We usually put the template into `locals.template`.
166 | - `callback` function. It receives two arguments: `err` object and html string provided by `static-site-generator-webpack-plugin`. If no error, `callback` transfers the html string to Webpack to write down on hard disks later.
167 |
168 | ## License
169 |
170 | MIT
171 |
172 |
--------------------------------------------------------------------------------
/dist/about/index.html:
--------------------------------------------------------------------------------
1 | My First React Router App
--------------------------------------------------------------------------------
/dist/index.html:
--------------------------------------------------------------------------------
1 | My First React Router App
--------------------------------------------------------------------------------
/dist/repos/index.html:
--------------------------------------------------------------------------------
1 | My First React Router App