Skip to content

Commit

Permalink
feat: add WEBPACK_SERVE environment variable (#2027)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rishabh3112 committed Nov 4, 2020
1 parent 7c5a2ba commit ea369a9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap
Expand Up @@ -11,6 +11,9 @@ Object {
},
"webpackArgs": Object {
"color": true,
"env": Object {
"WEBPACK_SERVE": true,
},
"hot": true,
},
}
Expand Down Expand Up @@ -39,6 +42,9 @@ Object {
},
"webpackArgs": Object {
"color": true,
"env": Object {
"WEBPACK_SERVE": true,
},
"mode": "development",
},
}
Expand Down
7 changes: 7 additions & 0 deletions packages/serve/src/parseArgs.ts
Expand Up @@ -39,6 +39,13 @@ export default function parseArgs(cli: WebpackCLIType, args: string[]): ArgsType
const parsedWebpackArgs = cli.argParser(core, parsedDevServerArgs.unknownArgs, true, process.title);
const webpackArgs = parsedWebpackArgs.opts;

// Add WEBPACK_SERVE environment variable
if (webpackArgs.env) {
webpackArgs.env.WEBPACK_SERVE = true;
} else {
webpackArgs.env = { WEBPACK_SERVE: true };
}

// pass along the 'hot' argument to the dev server if it exists
if (webpackArgs && webpackArgs.hot !== undefined) {
devServerArgs['hot'] = webpackArgs.hot;
Expand Down
33 changes: 33 additions & 0 deletions test/serve/serve-variable/serve-basic.test.js
@@ -0,0 +1,33 @@
'use strict';

const path = require('path');
const getPort = require('get-port');
const { runServe } = require('../../utils/test-utils');

const testPath = path.resolve(__dirname);

describe('serve variable', () => {
let port;

beforeEach(async () => {
port = await getPort();
});

const isWindows = process.platform === 'win32';

// TODO fix me on windows
if (isWindows) {
it('TODO: Fix on windows', () => {
expect(true).toBe(true);
});
return;
}

it('compiles without flags and export variable', async () => {
const { stdout, stderr } = await runServe(['--port', port], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('HotModuleReplacementPlugin');
expect(stderr).toHaveLength(0);
expect(stdout).toContain('PASS');
});
});
1 change: 1 addition & 0 deletions test/serve/serve-variable/src/index.js
@@ -0,0 +1 @@
console.log('hello world');
24 changes: 24 additions & 0 deletions test/serve/serve-variable/webpack.config.js
@@ -0,0 +1,24 @@
const isInProcess = process.env.WEBPACK_SERVE;

class CustomTestPlugin {
constructor(isInEnvironment) {
this.isInEnvironment = isInEnvironment;
}
apply(compiler) {
compiler.hooks.done.tap('testPlugin', () => {
if (!isInProcess && this.isInEnvironment) {
console.log('PASS');
} else {
console.log('FAIL');
}
});
}
}

module.exports = (env) => {
return {
mode: 'development',
devtool: false,
plugins: [new CustomTestPlugin(env.WEBPACK_SERVE)],
};
};

0 comments on commit ea369a9

Please sign in to comment.