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 4 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
3 changes: 3 additions & 0 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import parseArgs from './parseArgs';
export default function serve(...args: string[]): void {
const cli = new WebpackCLI();

// add WEBPACK_SERVE to compilation environment
args = [...args, '--env', 'WEBPACK_SERVE=true'];
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved

const { webpackArgs, devServerArgs } = parseArgs(cli, args);

cli.getCompiler(webpackArgs).then((compiler): void => {
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)],
};
};