Skip to content
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

feat: add WEBPACK_SERVE environment variable #2027

Merged
merged 6 commits into from
Nov 4, 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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;
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
} 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world');
24 changes: 24 additions & 0 deletions test/serve/serve-variable/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const isInProcess = process.env.WEBPACK_SERVE;
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved

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)],
};
};