|
| 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" /> |
0 commit comments