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

feat: add sockPath option (options.sockPath) #1289

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client-src/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* global __resourceQuery WorkerGlobalScope self */
/* eslint prefer-destructuring: off */

const qs = require('querystring');
const url = require('url');
const stripAnsi = require('strip-ansi');
const log = require('loglevel').getLogger('webpack-dev-server');
Expand Down Expand Up @@ -194,7 +194,7 @@ const socketUrl = url.format({
auth: urlParts.auth,
hostname,
port: urlParts.port,
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : urlParts.path
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : (qs.parse(urlParts.path).sockPath || urlParts.path)
});

socket(socketUrl, onSocketMsg);
Expand Down
15 changes: 15 additions & 0 deletions examples/node-api-sock-path/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Node.js API - Simple

```shell
node server.js
```

Starts a simple webpack-dev-server setup via the Node API. Open `http://localhost:8080/` to go the app.

## What should happen

In the app you should see "It's working."

In `app.js`, uncomment the code that results in an error and save. This error should be visible in the CLI and devtools.

Then, in `app.js`, uncomment the code that results in a warning. This warning should be visible in the CLI and devtools.
3 changes: 3 additions & 0 deletions examples/node-api-sock-path/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

document.write('It\'s working under a subapp');
9 changes: 9 additions & 0 deletions examples/node-api-sock-path/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<script src="/subapp/bundle.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Example: Node.js API - Simple</h1>
</body>
</html>
25 changes: 25 additions & 0 deletions examples/node-api-sock-path/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const path = require('path');
const Webpack = require('webpack');
const WebpackDevServer = require('../../lib/Server');
const webpackConfig = require('./webpack.config');

const compiler = Webpack(webpackConfig);
const server = new WebpackDevServer(compiler, {
stats: {
colors: true
},
contentBase: path.resolve(__dirname),
watchContentBase: true,
sockPath: '/subapp/sockjs-node',
publicPath: '/subapp/',
historyApiFallback: {
disableDotRule: true,
index: '/subapp/'
}
});

server.listen(8080, '127.0.0.1', () => {
console.log('Starting server on http://localhost:8080');
});
10 changes: 10 additions & 0 deletions examples/node-api-sock-path/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = {
context: __dirname,
entry: ['./app.js', '../../client/index.js?http://localhost:8080/&sockPath=subapp/sockjs-node'],
output: {
filename: 'bundle.js',
publicPath: '/subapp/'
}
};
3 changes: 2 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function Server(compiler, options) {
this.sockets = [];
this.contentBaseWatchers = [];
this.watchOptions = options.watchOptions || {};
this.sockPath = `/${options.sockPath ? options.sockPath.replace(/^\/|\/$/, '') : 'sockjs-node'}`;

// Listening for events
const invalidPlugin = () => {
Expand Down Expand Up @@ -608,7 +609,7 @@ Server.prototype.listen = function (port, hostname, fn) {
});

sockServer.installHandlers(this.listeningApp, {
prefix: '/sockjs-node'
prefix: this.sockPath
});

if (fn) {
Expand Down
4 changes: 4 additions & 0 deletions lib/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"description": "The Unix socket to listen to (instead of on a host).",
"type": "string"
},
"sockPath": {
"description": "URL path where the sockjs-node is served from (default is sockjs-node).",
"type": "string"
},
"watchOptions": {
"description": "Options for changing the watch behavior.",
"type": "object"
Expand Down
3 changes: 2 additions & 1 deletion lib/util/addDevServerEntrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptio
}
};
const domain = createDomain(devServerOptions, app);
const devClient = [`${require.resolve('../../client/')}?${domain}`];
const sockPath = devServerOptions.sockPath ? `&sockPath=${devServerOptions.sockPath}` : '';
const devClient = [`${require.resolve('../../client/')}?${domain}${sockPath}`];

if (devServerOptions.hotOnly) { devClient.push('webpack/hot/only-dev-server'); } else if (devServerOptions.hot) { devClient.push('webpack/hot/dev-server'); }

Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Validation', () => {
config: { asdf: true },
message: [
" - configuration has an unknown property 'asdf'. These properties are valid:",
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, ' +
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, sockPath?, ' +
'watchOptions?, headers?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
'inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, ' +
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, before?, after?, stats?, reporter?, reportTime?, ' +
Expand Down
28 changes: 28 additions & 0 deletions test/addDevServerEntrypoints.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const assert = require('assert');
const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
const config = require('./fixtures/simple-config/webpack.config');

describe('addDevServerEntrypoints', () => {
it('Entrypoints that are a single file will become an array', () => {
addDevServerEntrypoints(Object.assign({}, config), {});
assert(config.entry.length);
});

it('Entrypoints that are an array will become a longer array', () => {
addDevServerEntrypoints(
Object.assign({}, config, { entry: [config.entry] }),
{}
);
assert(config.entry.length > 1);
});

it('Entrypoints that are objects will have more keys', () => {
addDevServerEntrypoints(
Object.assign({}, config, { entry: { app: config.entry } }),
{}
);
assert(Object.keys(config.entry).length > 1);
});
});