Skip to content

Commit 7aa36da

Browse files
matheus1lvashellscape
authored andcommitted
docs(recipes): add publicPath + external/static html file (#87)
* docs(recipes): add publicPath + external html file * fix: typo * refactor: reword * chore: rename recipe
1 parent 0e5c377 commit 7aa36da

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

recipes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Recipes are snippets of code and useful tidbits that may be helpful to developer
88
[Custom Headers](./custom-headers.md)<br/>
99
[Multi Entry Setup](./multi-entry.md)<br/>
1010
[Proxies](./proxies.md)<br/>
11+
[Static HTML File](./external-html.md)<br/>
1112
[Using a Dynamic Port](./dynamic-port.md)<br/>
1213
[Using an Internal IP Address](./internal-ip.md)<br/>
1314
[Watching Static Content](./watch-static-content.md)

recipes/static-html-files.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Static HTML Files
2+
3+
When setting up an HTML file to bootstrap a Single Page Application users typically choose between two popular options:
4+
5+
1. [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin)
6+
1. A static HTML file
7+
8+
This recipe addresses setting up a static HTML file, rather than using a plugin to generate an HTML file to serve a bundle.
9+
10+
### Meat and Potatoes
11+
12+
To get started, your `webpack` configuration should be setup and building successfully. Once that's done, you'll need to create an HTML file somewhere in your project directory structure. Be sure to place the file _outside of the `output` directory_.
13+
14+
Let's use the following directory structure as an example:
15+
16+
```
17+
/the-app
18+
|_ dist
19+
|_ js
20+
|_ app.js
21+
|_ static
22+
|_ index.html
23+
```
24+
25+
Where `/the-app/dist` is the `output` webpack output directory. A configuration for this example setup would then resemble the following:
26+
27+
```js
28+
const path = require('path');
29+
const { WebpackPluginServe: Serve } = require('webpack-plugin-serve');
30+
const outputPath = path.resolve('./dist');
31+
32+
module.exports = {
33+
entry: ['./js/app.js', 'webpack-plugin-serve/client'],
34+
output: {
35+
filename: 'bundle.js'
36+
path: outputPath
37+
},
38+
plugins: [
39+
new Serve({
40+
static: [outputPath, path.resolve('./static')]
41+
})
42+
]
43+
}
44+
```
45+
46+
An HTML file for this setup might resemble:
47+
48+
```html
49+
<!doctype>
50+
<html>
51+
<body>
52+
<main></main>
53+
<script src="/bundle.js"></script>
54+
</body>
55+
</html>
56+
```
57+
58+
### 🍰 Dessert
59+
60+
Webpack provides for a multitude of different output configurations, and your needs will likely vary from the examples in this recipe. The important parts to assert are configured correctly are:
61+
62+
- the `static` path(s) for `WebpackPluginServe. Any path(s) specified will be be accessible from the website root.
63+
- the `output.publicPath` property, which should be set if your build directory is within a directory specified in `static`
64+
- the `src` property for the `<script>` tag in the static HTML file. This needs to point to where the bundle is located, as defined by all other configuration. In this recipe, we're adding the `outputPath` as a static directory, so the bundle is served from the website root.
65+
66+

0 commit comments

Comments
 (0)