Skip to content

Commit

Permalink
chore(examples): add universal-config example (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy authored and evilebottnawi committed Apr 23, 2019
1 parent da907f9 commit 0766c32
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/.eslintrc
@@ -1,4 +1,7 @@
{
"env": {
"browser": true
},
"rules": {
"no-console": "off"
}
Expand Down
12 changes: 12 additions & 0 deletions examples/general/universal-config/README.md
@@ -0,0 +1,12 @@
# General: Webpack Universal Config

This example demonstrates using a `webpack` config containing a `target: web` config and `target:node` config.

```console
npm run webpack-dev-server -- --open
```

## What Should Happen

1. The script should open `http://localhost:8080/` in your default browser.
2. You should see the text on the page itself change to read `[client.js, server.js]: Success!`.
25 changes: 25 additions & 0 deletions examples/general/universal-config/client.js
@@ -0,0 +1,25 @@
'use strict';

const target = document.querySelector('#target');

if (!window.fetch) {
target.classList.add('fail');
target.innerHTML = 'fetch is not supported';
} else {
fetch('/server.js')
.then((res) => {
if (res.status === 404) throw new Error('[server.js]: Not Found');
return res;
})
.then((res) => res.text())
.then((res) => {
if (res.includes("console.log('webpack-dev-server/server');")) {
target.classList.add('pass');
target.innerHTML = '[client.js, server.js]: Success!';
}
})
.catch((e) => {
target.classList.add('fail');
target.innerHTML = e.message;
});
}
3 changes: 3 additions & 0 deletions examples/general/universal-config/server.js
@@ -0,0 +1,3 @@
'use strict';

console.log('webpack-dev-server/server');
24 changes: 24 additions & 0 deletions examples/general/universal-config/webpack.config.js
@@ -0,0 +1,24 @@
'use strict';

const { setup } = require('../../util');

module.exports = [
setup({
mode: 'development',
entry: './client.js',
output: {
filename: 'client.js',
},
context: __dirname,
}),
{
mode: 'development',
target: 'node',
entry: './server.js',
output: {
filename: 'server.js',
},
context: __dirname,
node: false,
},
];

0 comments on commit 0766c32

Please sign in to comment.