Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples): add universal-config example #1810

Merged
merged 1 commit into from Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
},
];