Skip to content

Commit b0ad9d8

Browse files
fix: default value of the static option is the static directory path.join(process.cwd() + 'static') (#2897)
BREAKING CHANGE: default value of the `static` option is `path.resolve(process.cwd(), 'static')`, previously `process.cwd()`
1 parent c4c5823 commit b0ad9d8

File tree

7 files changed

+42
-19
lines changed

7 files changed

+42
-19
lines changed

lib/utils/normalizeOptions.js

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

3+
const path = require('path');
34
const isAbsoluteUrl = require('is-absolute-url');
45
const getCompilerConfigArray = require('./getCompilerConfigArray');
56

@@ -14,7 +15,7 @@ function normalizeOptions(compiler, options) {
1415
: {};
1516

1617
const defaultOptionsForStatic = {
17-
directory: process.cwd(),
18+
directory: path.join(process.cwd(), 'static'),
1819
staticOptions: {},
1920
publicPath: ['/'],
2021
serveIndex: { icons: true },
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Heyo.

test/server/host-option.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
'use strict';
22

3+
const path = require('path');
34
const request = require('supertest');
45
const config = require('../fixtures/simple-config/webpack.config');
56
const testServer = require('../helpers/test-server');
67
const port = require('../ports-map')['host-option'];
78

9+
const staticDirectory = path.resolve(
10+
__dirname,
11+
'../fixtures/contentbase-config'
12+
);
13+
814
describe('host option', () => {
915
let server = null;
1016
let req = null;
@@ -15,6 +21,7 @@ describe('host option', () => {
1521
config,
1622
{
1723
static: {
24+
directory: staticDirectory,
1825
watch: false,
1926
},
2027
port,
@@ -44,6 +51,7 @@ describe('host option', () => {
4451
config,
4552
{
4653
static: {
54+
directory: staticDirectory,
4755
watch: false,
4856
},
4957
// eslint-disable-next-line no-undefined
@@ -75,6 +83,7 @@ describe('host option', () => {
7583
config,
7684
{
7785
static: {
86+
directory: staticDirectory,
7887
watch: false,
7988
},
8089
host: null,
@@ -105,6 +114,7 @@ describe('host option', () => {
105114
config,
106115
{
107116
static: {
117+
directory: staticDirectory,
108118
watch: false,
109119
},
110120
host: '127.0.0.1',
@@ -135,6 +145,7 @@ describe('host option', () => {
135145
config,
136146
{
137147
static: {
148+
directory: staticDirectory,
138149
watch: false,
139150
},
140151
host: 'localhost',
@@ -165,6 +176,7 @@ describe('host option', () => {
165176
config,
166177
{
167178
static: {
179+
directory: staticDirectory,
168180
watch: false,
169181
},
170182
host: '0.0.0.0',

test/server/port-option.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
'use strict';
22

3+
const path = require('path');
34
const request = require('supertest');
45
const testServer = require('../helpers/test-server');
56
const config = require('../fixtures/simple-config/webpack.config');
67
const port = require('../ports-map')['port-option'];
78

9+
const staticDirectory = path.resolve(
10+
__dirname,
11+
'../fixtures/contentbase-config'
12+
);
13+
814
describe('port', () => {
915
let server = null;
1016
let req = null;
@@ -15,6 +21,7 @@ describe('port', () => {
1521
config,
1622
{
1723
static: {
24+
directory: staticDirectory,
1825
watch: false,
1926
},
2027
port,
@@ -45,6 +52,7 @@ describe('port', () => {
4552
config,
4653
{
4754
static: {
55+
directory: staticDirectory,
4856
watch: false,
4957
},
5058
// eslint-disable-next-line no-undefined
@@ -76,6 +84,7 @@ describe('port', () => {
7684
config,
7785
{
7886
static: {
87+
directory: staticDirectory,
7988
watch: false,
8089
},
8190
port: null,
@@ -106,6 +115,7 @@ describe('port', () => {
106115
config,
107116
{
108117
static: {
118+
directory: staticDirectory,
109119
watch: false,
110120
},
111121
port: '33333',
@@ -135,6 +145,7 @@ describe('port', () => {
135145
config,
136146
{
137147
static: {
148+
directory: staticDirectory,
138149
watch: false,
139150
},
140151
port: '33333',

test/server/static-directory-option.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ const testServer = require('../helpers/test-server');
77
const config = require('../fixtures/contentbase-config/webpack.config');
88
const port = require('../ports-map')['static-directory-option'];
99

10-
const publicDirectory = path.resolve(
10+
const staticDirectory = path.resolve(
1111
__dirname,
12-
'../fixtures/contentbase-config/public'
13-
);
14-
const otherPublicDirectory = path.resolve(
15-
__dirname,
16-
'../fixtures/contentbase-config/other'
12+
'../fixtures/contentbase-config'
1713
);
14+
const publicDirectory = path.resolve(staticDirectory, 'public');
15+
const otherPublicDirectory = path.resolve(staticDirectory, 'other');
1816

1917
describe('static.directory option', () => {
2018
let server;
@@ -290,7 +288,9 @@ describe('static.directory option', () => {
290288

291289
describe('default to PWD', () => {
292290
beforeAll((done) => {
293-
jest.spyOn(process, 'cwd').mockImplementation(() => publicDirectory);
291+
jest
292+
.spyOn(process, 'cwd')
293+
.mockImplementation(() => path.resolve(staticDirectory));
294294

295295
server = testServer.start(
296296
config,
@@ -310,7 +310,7 @@ describe('static.directory option', () => {
310310
});
311311

312312
it('Request to page', (done) => {
313-
req.get('/other.html').expect(200, done);
313+
req.get('/index.html').expect(200, done);
314314
});
315315
});
316316

test/server/static-publicPath-option.test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ const testServer = require('../helpers/test-server');
66
const config = require('../fixtures/contentbase-config/webpack.config');
77
const port = require('../ports-map')['static-publicPath-option'];
88

9-
const publicDirectory = path.resolve(
9+
const staticDirectory = path.resolve(
1010
__dirname,
11-
'../fixtures/contentbase-config/public'
11+
'../fixtures/contentbase-config'
1212
);
13-
const otherPublicDirectory = path.resolve(
14-
__dirname,
15-
'../fixtures/contentbase-config/other'
16-
);
17-
13+
const publicDirectory = path.resolve(staticDirectory, 'public');
14+
const otherPublicDirectory = path.resolve(staticDirectory, 'other');
1815
const staticPublicPath = '/serve-content-base-at-this-url';
1916
const otherStaticPublicPath = '/serve-other-content-at-this-url';
2017

@@ -191,7 +188,7 @@ describe('static.publicPath option', () => {
191188

192189
describe('default to PWD', () => {
193190
beforeAll((done) => {
194-
jest.spyOn(process, 'cwd').mockImplementation(() => publicDirectory);
191+
jest.spyOn(process, 'cwd').mockImplementation(() => staticDirectory);
195192

196193
server = testServer.start(
197194
config,
@@ -213,7 +210,7 @@ describe('static.publicPath option', () => {
213210
});
214211

215212
it('Request to page', (done) => {
216-
req.get(`${staticPublicPath}/other.html`).expect(200, done);
213+
req.get(`${staticPublicPath}/index.html`).expect(200, done);
217214
});
218215
});
219216

test/server/utils/normalizeOptions.test.js

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

3+
const path = require('path');
34
const webpack = require('webpack');
45
const normalizeOptions = require('../../../lib/utils/normalizeOptions');
56

@@ -448,7 +449,7 @@ describe('normalizeOptions', () => {
448449

449450
if (data.options.static) {
450451
data.options.static.forEach((staticOpts) => {
451-
if (staticOpts.directory === process.cwd()) {
452+
if (staticOpts.directory === path.join(process.cwd(), 'static')) {
452453
// give an indication in the snapshot that this is the
453454
// current working directory
454455
staticOpts.directory = 'CWD';

0 commit comments

Comments
 (0)