Skip to content

test: static output #2913

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

Merged
merged 9 commits into from
Dec 7, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client-src/default/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const webpack = require('webpack');

module.exports = {
mode: 'production',
Expand All @@ -9,7 +10,8 @@ module.exports = {
path: path.resolve(__dirname, '../../client/default'),
filename: 'index.bundle.js',
},
target: ['web', 'es5'],
// Workaround for webpack@4 installation
target: webpack.webpack ? ['web', 'es5'] : 'web',
module: {
rules: [
{
Expand Down
4 changes: 3 additions & 1 deletion client-src/sockjs/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const webpack = require('webpack');

module.exports = {
mode: 'production',
Expand All @@ -11,5 +12,6 @@ module.exports = {
library: 'SockJS',
libraryTarget: 'umd',
},
target: ['web', 'es5'],
// Workaround for webpack@4 installation
target: webpack.webpack ? ['web', 'es5'] : 'web',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, failed locally installation for webpack@4

};
3 changes: 2 additions & 1 deletion client-src/transpiled-modules/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const webpack = require('webpack');
const { merge } = require('webpack-merge');

const base = {
Expand All @@ -9,7 +10,7 @@ const base = {
path: path.resolve(__dirname, '../../client/transpiled-modules'),
libraryTarget: 'commonjs2',
},
target: ['web', 'es5'],
target: webpack.webpack ? ['web', 'es5'] : 'web',
module: {
rules: [
{
Expand Down
4 changes: 4 additions & 0 deletions test/cli/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`CLI --hot webpack 4 1`] = `
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
<i> [webpack-dev-middleware] Hash: X
<i> Version: webpack x.x.x Time: Xms
<i> Built at: Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT
Expand Down Expand Up @@ -29,6 +30,7 @@ exports[`CLI --hot webpack 4 1`] = `

exports[`CLI --hot webpack 5 1`] = `
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
<i> runtime modules X KiB 10 modules
<i> cacheable modules X KiB
Expand All @@ -47,6 +49,7 @@ exports[`CLI --hot webpack 5 1`] = `

exports[`CLI --no-hot webpack 4 1`] = `
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
<i> [webpack-dev-middleware] Hash: X
<i> Version: webpack x.x.x Time: Xms
<i> Built at: Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT
Expand Down Expand Up @@ -74,6 +77,7 @@ exports[`CLI --no-hot webpack 4 1`] = `

exports[`CLI --no-hot webpack 5 1`] = `
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
<i> runtime modules X bytes 3 modules
<i> cacheable modules X KiB
Expand Down
59 changes: 42 additions & 17 deletions test/cli/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { join, resolve } = require('path');
const path = require('path');
const execa = require('execa');
const internalIp = require('internal-ip');
const testBin = require('../helpers/test-bin');
Expand All @@ -9,11 +9,9 @@ const isWebpack5 = require('../helpers/isWebpack5');
// skip if webpack-dev-server is not linked
let runCLITest = describe;
let basePath;

try {
basePath = join(require.resolve('webpack-dev-server'), '..', '..').replace(
/\\/g,
'/'
);
basePath = path.join(require.resolve('webpack-dev-server'), '..', '..');
} catch {
runCLITest = describe.skip;
}
Expand All @@ -39,7 +37,9 @@ runCLITest('CLI', () => {
'$1 Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT'
)
.replace(/webpack [^ )]+/g, 'webpack x.x.x')
.replace(new RegExp(quotemeta(basePath.replace(/\\/g, '/')), 'g'), 'Xdir')
.replace(new RegExp(quotemeta(basePath), 'g'), 'Xdir')
.replace(/[\\/]static/, '/static')
.replace(/(Hash:) [a-z0-9]+/g, '$1 X')
.replace(/ dependencies:Xms/g, '')
.replace(/, additional resolving: X ms/g, '');
Expand Down Expand Up @@ -178,7 +178,7 @@ runCLITest('CLI', () => {
it('should log public path', (done) => {
testBin(
false,
resolve(__dirname, '../fixtures/dev-public-path/webpack.config.js')
path.resolve(__dirname, '../fixtures/dev-public-path/webpack.config.js')
)
.then((output) => {
expect(output.exitCode).toEqual(0);
Expand All @@ -194,10 +194,35 @@ runCLITest('CLI', () => {
});
});

it('should log static', (done) => {
testBin(
false,
path.resolve(__dirname, '../fixtures/static/webpack.config.js')
)
.then((output) => {
console.log(output);
expect(output.exitCode).toEqual(0);
done();
})
.catch((err) => {
const staticDirectory = path.resolve(
__dirname,
'../fixtures/static/static'
);

// for windows
expect(err.stderr).toContain(
`Content not from webpack is served from '${staticDirectory}' directory`
);
expect(err.stderr).toContain('Compiled successfully.');
done();
});
});

it('should accept the promise function of webpack.config.js', (done) => {
testBin(
false,
resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
path.resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
)
.then((output) => {
expect(output.exitCode).toEqual(0);
Expand All @@ -211,8 +236,8 @@ runCLITest('CLI', () => {
});

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

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

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

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

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

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

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

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

const cp = execa('node', [cliPath, '--colors=false'], { cwd });
const cp2 = execa('node', [cliPath, '--colors=false'], { cwd });
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/static/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log('i am foo!');
15 changes: 15 additions & 0 deletions test/fixtures/static/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
Test
</body>
</html>
11 changes: 11 additions & 0 deletions test/fixtures/static/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const path = require('path');

module.exports = {
mode: 'development',
entry: path.resolve(__dirname, 'foo.js'),
devServer: {
static: path.resolve(__dirname, 'static'),
},
};
7 changes: 1 addition & 6 deletions test/helpers/test-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ function testBin(testArgs, configPath) {
testArgs = testArgs.split(' ');
}

const args = [
webpackDevServerPath,
'--config',
configPath,
'--no-static',
].concat(testArgs);
const args = [webpackDevServerPath, '--config', configPath].concat(testArgs);

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