Skip to content

Commit ea369a9

Browse files
authored
feat: add WEBPACK_SERVE environment variable (#2027)
* tests: add test case for requirement * feat: add WEBPACK_SERVE variable * refactor: declare into variable * feat: enforce use of env variable * chore: update variable defination * chore: update snapshots
1 parent 7c5a2ba commit ea369a9

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Object {
1111
},
1212
"webpackArgs": Object {
1313
"color": true,
14+
"env": Object {
15+
"WEBPACK_SERVE": true,
16+
},
1417
"hot": true,
1518
},
1619
}
@@ -39,6 +42,9 @@ Object {
3942
},
4043
"webpackArgs": Object {
4144
"color": true,
45+
"env": Object {
46+
"WEBPACK_SERVE": true,
47+
},
4248
"mode": "development",
4349
},
4450
}

packages/serve/src/parseArgs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export default function parseArgs(cli: WebpackCLIType, args: string[]): ArgsType
3939
const parsedWebpackArgs = cli.argParser(core, parsedDevServerArgs.unknownArgs, true, process.title);
4040
const webpackArgs = parsedWebpackArgs.opts;
4141

42+
// Add WEBPACK_SERVE environment variable
43+
if (webpackArgs.env) {
44+
webpackArgs.env.WEBPACK_SERVE = true;
45+
} else {
46+
webpackArgs.env = { WEBPACK_SERVE: true };
47+
}
48+
4249
// pass along the 'hot' argument to the dev server if it exists
4350
if (webpackArgs && webpackArgs.hot !== undefined) {
4451
devServerArgs['hot'] = webpackArgs.hot;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const getPort = require('get-port');
5+
const { runServe } = require('../../utils/test-utils');
6+
7+
const testPath = path.resolve(__dirname);
8+
9+
describe('serve variable', () => {
10+
let port;
11+
12+
beforeEach(async () => {
13+
port = await getPort();
14+
});
15+
16+
const isWindows = process.platform === 'win32';
17+
18+
// TODO fix me on windows
19+
if (isWindows) {
20+
it('TODO: Fix on windows', () => {
21+
expect(true).toBe(true);
22+
});
23+
return;
24+
}
25+
26+
it('compiles without flags and export variable', async () => {
27+
const { stdout, stderr } = await runServe(['--port', port], testPath);
28+
expect(stdout).toContain('main.js');
29+
expect(stdout).not.toContain('HotModuleReplacementPlugin');
30+
expect(stderr).toHaveLength(0);
31+
expect(stdout).toContain('PASS');
32+
});
33+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello world');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const isInProcess = process.env.WEBPACK_SERVE;
2+
3+
class CustomTestPlugin {
4+
constructor(isInEnvironment) {
5+
this.isInEnvironment = isInEnvironment;
6+
}
7+
apply(compiler) {
8+
compiler.hooks.done.tap('testPlugin', () => {
9+
if (!isInProcess && this.isInEnvironment) {
10+
console.log('PASS');
11+
} else {
12+
console.log('FAIL');
13+
}
14+
});
15+
}
16+
}
17+
18+
module.exports = (env) => {
19+
return {
20+
mode: 'development',
21+
devtool: false,
22+
plugins: [new CustomTestPlugin(env.WEBPACK_SERVE)],
23+
};
24+
};

0 commit comments

Comments
 (0)