Skip to content

Commit 3dbbaa4

Browse files
test: static output
1 parent 4666425 commit 3dbbaa4

File tree

9 files changed

+87
-30
lines changed

9 files changed

+87
-30
lines changed

client-src/default/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const webpack = require('webpack');
45

56
module.exports = {
67
mode: 'production',
@@ -9,7 +10,8 @@ module.exports = {
910
path: path.resolve(__dirname, '../../client/default'),
1011
filename: 'index.bundle.js',
1112
},
12-
target: ['web', 'es5'],
13+
// Workaround for webpack@4 installation
14+
target: webpack.webpack ? ['web', 'es5'] : 'web',
1315
module: {
1416
rules: [
1517
{

client-src/sockjs/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const webpack = require('webpack');
45

56
module.exports = {
67
mode: 'production',
@@ -11,5 +12,6 @@ module.exports = {
1112
library: 'SockJS',
1213
libraryTarget: 'umd',
1314
},
14-
target: ['web', 'es5'],
15+
// Workaround for webpack@4 installation
16+
target: webpack.webpack ? ['web', 'es5'] : 'web',
1517
};

client-src/transpiled-modules/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const webpack = require('webpack');
45
const { merge } = require('webpack-merge');
56

67
const base = {
@@ -9,7 +10,7 @@ const base = {
910
path: path.resolve(__dirname, '../../client/transpiled-modules'),
1011
libraryTarget: 'commonjs2',
1112
},
12-
target: ['web', 'es5'],
13+
target: webpack.webpack ? ['web', 'es5'] : 'web',
1314
module: {
1415
rules: [
1516
{

test/cli/__snapshots__/cli.test.js.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`CLI --hot webpack 4 1`] = `
44
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
5+
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
56
<i> [webpack-dev-middleware] Hash: X
67
<i> Version: webpack x.x.x Time: Xms
78
<i> Built at: Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT
@@ -16,19 +17,20 @@ exports[`CLI --hot webpack 4 1`] = `
1617
<i> [../../../client/default/utils/log.js] Xdir/client/default/utils/log.js X bytes {main} [built]
1718
<i> [../../../client/default/utils/reloadApp.js] Xdir/client/default/utils/reloadApp.js X KiB {main} [built]
1819
<i> [../../../client/default/utils/sendMessage.js] Xdir/client/default/utils/sendMessage.js X bytes {main} [built]
19-
<i> [../../../client/transpiled-modules/strip-ansi.js] Xdir/client/transpiled-modules/strip-ansi.js X bytes {main} [built]
20+
<i> [../../../client/transpiled-modules/strip-ansi.js] Xdir/client/transpiled-modules/strip-ansi.js X KiB {main} [built]
2021
<i> [../../../node_modules/webpack/hot sync ^\\\\.\\\\/log$] (webpack)/hot sync nonrecursive ^\\\\.\\\\/log$ X bytes {main} [built]
2122
<i> [../../../node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js X KiB {main} [built]
2223
<i> [../../../node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js X bytes {main} [built]
2324
<i> [../../../node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js X KiB {main} [built]
2425
<i> [../../../node_modules/webpack/hot/log.js] (webpack)/hot/log.js X KiB {main} [built]
2526
<i> [./foo.js] X bytes {main} [built]
26-
<i> + 19 hidden modules
27+
<i> + 18 hidden modules
2728
<i> [webpack-dev-middleware] Compiled successfully."
2829
`;
2930
3031
exports[`CLI --hot webpack 5 1`] = `
3132
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
33+
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
3234
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
3335
<i> runtime modules X KiB 10 modules
3436
<i> cacheable modules X KiB
@@ -47,6 +49,7 @@ exports[`CLI --hot webpack 5 1`] = `
4749
4850
exports[`CLI --no-hot webpack 4 1`] = `
4951
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
52+
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
5053
<i> [webpack-dev-middleware] Hash: X
5154
<i> Version: webpack x.x.x Time: Xms
5255
<i> Built at: Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT
@@ -64,16 +67,17 @@ exports[`CLI --no-hot webpack 4 1`] = `
6467
<i> [../../../client/default/utils/reloadApp.js] Xdir/client/default/utils/reloadApp.js X KiB {main} [built]
6568
<i> [../../../client/default/utils/sendMessage.js] Xdir/client/default/utils/sendMessage.js X bytes {main} [built]
6669
<i> [../../../client/transpiled-modules/log.js] Xdir/client/transpiled-modules/log.js X KiB {main} [built]
67-
<i> [../../../client/transpiled-modules/strip-ansi.js] Xdir/client/transpiled-modules/strip-ansi.js X bytes {main} [built]
70+
<i> [../../../client/transpiled-modules/strip-ansi.js] Xdir/client/transpiled-modules/strip-ansi.js X KiB {main} [built]
6871
<i> [../../../node_modules/ansi-html/index.js] Xdir/node_modules/ansi-html/index.js X KiB {main} [built]
6972
<i> [../../../node_modules/webpack/hot sync ^\\\\.\\\\/log$] (webpack)/hot sync nonrecursive ^\\\\.\\\\/log$ X bytes {main} [built]
7073
<i> [./foo.js] X bytes {main} [built]
71-
<i> + 17 hidden modules
74+
<i> + 16 hidden modules
7275
<i> [webpack-dev-middleware] Compiled successfully."
7376
`;
7477
7578
exports[`CLI --no-hot webpack 5 1`] = `
7679
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
80+
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
7781
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
7882
<i> runtime modules X bytes 3 modules
7983
<i> cacheable modules X KiB

test/cli/cli.test.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { join, resolve } = require('path');
3+
const path = require('path');
44
const execa = require('execa');
55
const internalIp = require('internal-ip');
66
const testBin = require('../helpers/test-bin');
@@ -10,10 +10,9 @@ const isWebpack5 = require('../helpers/isWebpack5');
1010
let runCLITest = describe;
1111
let basePath;
1212
try {
13-
basePath = join(require.resolve('webpack-dev-server'), '..', '..').replace(
14-
/\\/g,
15-
'/'
16-
);
13+
basePath = path
14+
.join(require.resolve('webpack-dev-server'), '..', '..')
15+
.replace(/\\/g, '/');
1716
} catch {
1817
runCLITest = describe.skip;
1918
}
@@ -178,7 +177,7 @@ runCLITest('CLI', () => {
178177
it('should log public path', (done) => {
179178
testBin(
180179
false,
181-
resolve(__dirname, '../fixtures/dev-public-path/webpack.config.js')
180+
path.resolve(__dirname, '../fixtures/dev-public-path/webpack.config.js')
182181
)
183182
.then((output) => {
184183
expect(output.exitCode).toEqual(0);
@@ -194,10 +193,35 @@ runCLITest('CLI', () => {
194193
});
195194
});
196195

196+
it('should log static', (done) => {
197+
testBin(
198+
false,
199+
path.resolve(__dirname, '../fixtures/static/webpack.config.js')
200+
)
201+
.then((output) => {
202+
console.log(output);
203+
expect(output.exitCode).toEqual(0);
204+
done();
205+
})
206+
.catch((err) => {
207+
const staticDirectory = path.resolve(
208+
__dirname,
209+
'../fixtures/static/static'
210+
);
211+
212+
// for windows
213+
expect(err.stderr).toContain(
214+
`Content not from webpack is served from '${staticDirectory}' directory`
215+
);
216+
expect(err.stderr).toContain('Compiled successfully.');
217+
done();
218+
});
219+
});
220+
197221
it('should accept the promise function of webpack.config.js', (done) => {
198222
testBin(
199223
false,
200-
resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
224+
path.resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
201225
)
202226
.then((output) => {
203227
expect(output.exitCode).toEqual(0);
@@ -211,8 +235,8 @@ runCLITest('CLI', () => {
211235
});
212236

213237
it('should exit the process when SIGINT is detected', (done) => {
214-
const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js');
215-
const examplePath = resolve(__dirname, '../../examples/cli/public');
238+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
239+
const examplePath = path.resolve(__dirname, '../../examples/cli/public');
216240
const cp = execa('node', [cliPath], { cwd: examplePath });
217241

218242
cp.stderr.on('data', (data) => {
@@ -231,8 +255,8 @@ runCLITest('CLI', () => {
231255
});
232256

233257
it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => {
234-
const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js');
235-
const cwd = resolve(__dirname, '../fixtures/cli');
258+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
259+
const cwd = path.resolve(__dirname, '../fixtures/cli');
236260
const cp = execa('node', [cliPath], { cwd });
237261

238262
let killed = false;
@@ -253,8 +277,8 @@ runCLITest('CLI', () => {
253277
});
254278

255279
it('should exit the process when stdin ends if --stdin', (done) => {
256-
const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js');
257-
const examplePath = resolve(__dirname, '../../examples/cli/public');
280+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
281+
const examplePath = path.resolve(__dirname, '../../examples/cli/public');
258282
const cp = execa('node', [cliPath, '--stdin'], { cwd: examplePath });
259283

260284
cp.stderr.on('data', (data) => {
@@ -274,8 +298,8 @@ runCLITest('CLI', () => {
274298
});
275299

276300
it('should exit the process when stdin ends if --stdin, even before the compilation is done', (done) => {
277-
const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js');
278-
const cwd = resolve(__dirname, '../fixtures/cli');
301+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
302+
const cwd = path.resolve(__dirname, '../fixtures/cli');
279303
const cp = execa('node', [cliPath, '--stdin'], { cwd });
280304

281305
let killed = false;
@@ -299,8 +323,8 @@ runCLITest('CLI', () => {
299323
// TODO: do not skip after @webpack-cli/serve passes null port by default
300324
// https://github.com/webpack/webpack-cli/pull/2126
301325
it.skip('should use different random port when multiple instances are started on different processes', (done) => {
302-
const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js');
303-
const cwd = resolve(__dirname, '../fixtures/cli');
326+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
327+
const cwd = path.resolve(__dirname, '../fixtures/cli');
304328

305329
const cp = execa('node', [cliPath, '--colors=false'], { cwd });
306330
const cp2 = execa('node', [cliPath, '--colors=false'], { cwd });

test/fixtures/static/foo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('i am foo!');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
8+
/>
9+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
10+
<title>Document</title>
11+
</head>
12+
<body>
13+
Test
14+
</body>
15+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
module.exports = {
6+
mode: 'development',
7+
entry: path.resolve(__dirname, 'foo.js'),
8+
devServer: {
9+
static: path.resolve(__dirname, 'static'),
10+
},
11+
};

test/helpers/test-bin.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ function testBin(testArgs, configPath) {
2626
testArgs = testArgs.split(' ');
2727
}
2828

29-
const args = [
30-
webpackDevServerPath,
31-
'--config',
32-
configPath,
33-
'--no-static',
34-
].concat(testArgs);
29+
const args = [webpackDevServerPath, '--config', configPath].concat(testArgs);
3530

3631
return execa('node', args, { cwd, env, timeout: 10000 });
3732
}

0 commit comments

Comments
 (0)