Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit b764f1e

Browse files
committed
Initial commit
0 parents  commit b764f1e

20 files changed

+1247
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true
6+
indent_style = space
7+
charset = utf-8
8+
9+
[*.js]
10+
indent_size = 2
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.editorconfig
2+
.gitignore
3+
.gitattributes
4+
.prettierrc
5+
example/

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true
4+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Paul Johnson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# webpack-dev-server-inject-scripts
2+
3+
webpack-dev-server is amazing when you're working on a SPA but it can be a pain to setup
4+
when you want to use it with a traditional backend application that serves markup
5+
instead of json, for example, a CMS that isn't headless.
6+
7+
- Which files do I need to reference in my master layout / template?
8+
- What happens if I add a new entry or rename one of the output files in webpack config?
9+
10+
When running a production build things are easy, you can point HtmlWebpackPlugin at your layout
11+
files and it will happily inject the necessary script and link elements and output a file you can deploy.
12+
13+
This project aims to provide similar functionality in development by injecting references to the code built by webpack into the markup served by your backend application when viewing through the http-proxy-middleware.
14+
15+
Please note that the API is likely to change without any concern for backwards compatability
16+
until 1.0.0
17+
18+
## Install
19+
20+
```bash
21+
npm i --save-dev webpack-dev-server-inject-scripts
22+
```
23+
24+
## Usage
25+
26+
Add the following to dev webpack config
27+
28+
```js
29+
const injectScripts = require("webpack-dev-server-inject-scripts");
30+
31+
...
32+
33+
devServer: {
34+
index: "",
35+
port: 8080,
36+
hot: true,
37+
historyApiFallback: true,
38+
proxy: {
39+
"/": {
40+
target: "http://localhost:1234", // Your backend application here
41+
changeOrigin: true // play nice with upstream https
42+
}
43+
},
44+
before: function(app, server, compiler) {
45+
app.use(injectScripts(compiler));
46+
}
47+
}
48+
49+
```
50+
51+
The middleware function can take an options argument for additional configuration
52+
53+
```js
54+
const options = {
55+
ignoredPaths: [/\/umbraco/, /\/wp-admin/]
56+
};
57+
app.use(injectScripts(compiler, options));
58+
```
59+
60+
- run backend application
61+
- run webpack-dev-server
62+
63+
View through the webpack-dev-server proxy
64+
65+
## In Production
66+
67+
Turn this off and use [HtmlWebpackPlugin](https://github.com/jantimon/html-webpack-plugin) instead
68+
69+
## tl;dr
70+
71+
Screenshot from example project
72+
73+
<img src="https://user-images.githubusercontent.com/2056399/110414325-942f4c00-8087-11eb-8278-abc26b15ab91.png" width="640" alt="example" />

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock.json

example/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# webpack-dev-server-inject-scripts example project
2+
To run the example
3+
4+
```bash
5+
$ npm install
6+
$ npm start
7+
```

example/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "webpack-dev-server-inject-scripts-example",
3+
"version": "0.0.0",
4+
"description": "demo the point of this thing",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "webpack serve"
8+
},
9+
"dependencies": {
10+
"css-loader": "^5.1.1",
11+
"style-loader": "^2.0.0",
12+
"webpack": "^5.24.4",
13+
"webpack-cli": "^4.5.0",
14+
"webpack-dev-server": "^3.11.2",
15+
"webpack-dev-server-inject-scripts": "file:.."
16+
}
17+
}

0 commit comments

Comments
 (0)