Skip to content

Commit 2a50946

Browse files
test: static output (#2913)
1 parent 385fc71 commit 2a50946

File tree

9 files changed

+84
-26
lines changed

9 files changed

+84
-26
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: 4 additions & 0 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
@@ -29,6 +30,7 @@ exports[`CLI --hot webpack 4 1`] = `
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
@@ -74,6 +77,7 @@ exports[`CLI --no-hot webpack 4 1`] = `
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: 42 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');
@@ -9,11 +9,9 @@ const isWebpack5 = require('../helpers/isWebpack5');
99
// skip if webpack-dev-server is not linked
1010
let runCLITest = describe;
1111
let basePath;
12+
1213
try {
13-
basePath = join(require.resolve('webpack-dev-server'), '..', '..').replace(
14-
/\\/g,
15-
'/'
16-
);
14+
basePath = path.join(require.resolve('webpack-dev-server'), '..', '..');
1715
} catch {
1816
runCLITest = describe.skip;
1917
}
@@ -39,7 +37,9 @@ runCLITest('CLI', () => {
3937
'$1 Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT'
4038
)
4139
.replace(/webpack [^ )]+/g, 'webpack x.x.x')
40+
.replace(new RegExp(quotemeta(basePath.replace(/\\/g, '/')), 'g'), 'Xdir')
4241
.replace(new RegExp(quotemeta(basePath), 'g'), 'Xdir')
42+
.replace(/[\\/]static/, '/static')
4343
.replace(/(Hash:) [a-z0-9]+/g, '$1 X')
4444
.replace(/ dependencies:Xms/g, '')
4545
.replace(/, additional resolving: X ms/g, '');
@@ -178,7 +178,7 @@ runCLITest('CLI', () => {
178178
it('should log public path', (done) => {
179179
testBin(
180180
false,
181-
resolve(__dirname, '../fixtures/dev-public-path/webpack.config.js')
181+
path.resolve(__dirname, '../fixtures/dev-public-path/webpack.config.js')
182182
)
183183
.then((output) => {
184184
expect(output.exitCode).toEqual(0);
@@ -194,10 +194,35 @@ runCLITest('CLI', () => {
194194
});
195195
});
196196

197+
it('should log static', (done) => {
198+
testBin(
199+
false,
200+
path.resolve(__dirname, '../fixtures/static/webpack.config.js')
201+
)
202+
.then((output) => {
203+
console.log(output);
204+
expect(output.exitCode).toEqual(0);
205+
done();
206+
})
207+
.catch((err) => {
208+
const staticDirectory = path.resolve(
209+
__dirname,
210+
'../fixtures/static/static'
211+
);
212+
213+
// for windows
214+
expect(err.stderr).toContain(
215+
`Content not from webpack is served from '${staticDirectory}' directory`
216+
);
217+
expect(err.stderr).toContain('Compiled successfully.');
218+
done();
219+
});
220+
});
221+
197222
it('should accept the promise function of webpack.config.js', (done) => {
198223
testBin(
199224
false,
200-
resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
225+
path.resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
201226
)
202227
.then((output) => {
203228
expect(output.exitCode).toEqual(0);
@@ -211,8 +236,8 @@ runCLITest('CLI', () => {
211236
});
212237

213238
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');
239+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
240+
const examplePath = path.resolve(__dirname, '../../examples/cli/public');
216241
const cp = execa('node', [cliPath], { cwd: examplePath });
217242

218243
cp.stderr.on('data', (data) => {
@@ -231,8 +256,8 @@ runCLITest('CLI', () => {
231256
});
232257

233258
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');
259+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
260+
const cwd = path.resolve(__dirname, '../fixtures/cli');
236261
const cp = execa('node', [cliPath], { cwd });
237262

238263
let killed = false;
@@ -253,8 +278,8 @@ runCLITest('CLI', () => {
253278
});
254279

255280
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');
281+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
282+
const examplePath = path.resolve(__dirname, '../../examples/cli/public');
258283
const cp = execa('node', [cliPath, '--stdin'], { cwd: examplePath });
259284

260285
cp.stderr.on('data', (data) => {
@@ -274,8 +299,8 @@ runCLITest('CLI', () => {
274299
});
275300

276301
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');
302+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
303+
const cwd = path.resolve(__dirname, '../fixtures/cli');
279304
const cp = execa('node', [cliPath, '--stdin'], { cwd });
280305

281306
let killed = false;
@@ -299,8 +324,8 @@ runCLITest('CLI', () => {
299324
// TODO: do not skip after @webpack-cli/serve passes null port by default
300325
// https://github.com/webpack/webpack-cli/pull/2126
301326
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');
327+
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
328+
const cwd = path.resolve(__dirname, '../fixtures/cli');
304329

305330
const cp = execa('node', [cliPath, '--colors=false'], { cwd });
306331
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)