Skip to content

Commit

Permalink
Merge branch 'next' into version.
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 13, 2020
2 parents 6cfe9df + e4cf0dc commit e24a267
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 30 deletions.
14 changes: 0 additions & 14 deletions packages/generators/__tests__/plugin-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { join } from 'path';
import { run } from 'yeoman-test';
import * as assert from 'yeoman-assert';

import { generatePluginName } from '../src/utils';

describe('plugin generator', () => {
it('generates a default plugin', async () => {
const pluginName = 'my-test-plugin';
Expand Down Expand Up @@ -35,15 +33,3 @@ describe('plugin generator', () => {
// higher timeout so travis has enough time to execute
}, 10000);
});

describe('generate plugin name', () => {
it('should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin', () => {
const pluginName = generatePluginName('extract-text-webpack-plugin');
expect(pluginName).toEqual('ExtractTextWebpackPlugin');
});

it('should return webpack Standard Plugin Name for Name : webpack.DefinePlugin', () => {
const pluginName = generatePluginName('webpack.DefinePlugin');
expect(pluginName).toEqual('webpack.DefinePlugin');
});
});
30 changes: 30 additions & 0 deletions packages/generators/__tests__/utils/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { replaceAt, generatePluginName } from '../../lib/utils/plugins';

describe('generate plugin name', () => {
it('should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin', () => {
const pluginName = generatePluginName('extract-text-webpack-plugin');
expect(pluginName).toEqual('ExtractTextWebpackPlugin');
});

it('should return webpack Standard Plugin Name for Name : webpack.DefinePlugin', () => {
const pluginName = generatePluginName('webpack.DefinePlugin');
expect(pluginName).toEqual('webpack.DefinePlugin');
});

it('should return webpack Standard Plugin Name for Name : my-plugin-name-1', () => {
const pluginName = generatePluginName('my-plugin-name-1');
expect(pluginName).toEqual('MyPluginName1');
});

it('replaceAt capitalizes first letter', () => {
expect(replaceAt('mystring', 0, 'M')).toEqual('Mystring');
});

it('replaceAt replaces within string', () => {
expect(replaceAt('mystring', 2, '-inserted-')).toEqual('my-inserted-tring');
});

it('replaceAt replaces at end of string', () => {
expect(replaceAt('mystring', 7, '-inserted-')).toEqual('mystrin-inserted-');
});
});
3 changes: 1 addition & 2 deletions packages/generators/src/init-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import chalk from 'chalk';
import logSymbols from 'log-symbols';
import Generator from 'yeoman-generator';
import path from 'path';
import { getPackageManager } from '@webpack-cli/package-utils';
import { Confirm, Input, List } from '@webpack-cli/webpack-scaffold';
Expand All @@ -22,7 +21,7 @@ import { CustomGenerator } from './types';
* Generator for initializing a webpack config
*
* @class InitGenerator
* @extends Generator
* @extends CustomGenerator
* @returns {Void} After execution, transforms are triggered
*
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/generators/src/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const replaceAt = (str: string, index: number, replace: string): string =
export const generatePluginName = (rawPluginName: string): string => {
const myPluginNameArray = rawPluginName.split('-');
const pluginArrLength: number = myPluginNameArray.length;
// ignore plugin names without hyphens to allow for cases
// such as webpack.DefinePlugin, which should not be capitalized
for (let i = 0; i < pluginArrLength && pluginArrLength > 1; i++) {
myPluginNameArray[i] = replaceAt(myPluginNameArray[i], 0, myPluginNameArray[i].charAt(0).toUpperCase());
}
Expand Down
30 changes: 30 additions & 0 deletions test/entry/flag-entry/entry-with-flag.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const { run } = require('../../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');

describe('entry flag', () => {
it('should load ./src/a.js as entry', (done) => {
const { stderr, stdout } = run(__dirname, ['--entry', './src/a.js']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('Hello from a.js');
done();
});
});

it('should throw error for invalid entry file', () => {
const { stderr, stdout } = run(__dirname, ['--entry', './src/test.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('not found');
});
});
1 change: 1 addition & 0 deletions test/entry/flag-entry/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from a.js');
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

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

const runServe = (args) => {
return runWatch({
testCase: path.resolve(__dirname),
args: ['serve'].concat(args),
setOutput: false,
outputKillStr: 'main',
});
};
const { runServe } = require('../../utils/test-utils');

const testPath = path.resolve(__dirname);

describe('basic serve usage', () => {
let port;
Expand All @@ -31,29 +24,29 @@ describe('basic serve usage', () => {
});
} else {
it('compiles without flags', async () => {
const { stdout, stderr } = await runServe(['--port', port]);
const { stdout, stderr } = await runServe(['--port', port], testPath);
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('uses hot flag to alter bundle', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot']);
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
expect(stderr).toHaveLength(0);
});

it('uses hot flag and progress flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress']);
const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress'], testPath);
expect(stdout).toContain('main.js');
expect(stdout).toContain('hot/dev-server.js');
// progress flag makes use of stderr
expect(stderr).not.toHaveLength(0);
});

it('throws error on unknown flag', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--unknown-flag']);
const { stdout, stderr } = await runServe(['--port', port, '--unknown-flag'], testPath);
expect(stdout).toHaveLength(0);
expect(stderr).toContain('Unknown option: --unknown-flag');
});
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/serve/with-custom-port/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Kageyama Tobio');
57 changes: 57 additions & 0 deletions test/serve/with-custom-port/serve-custom-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

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

const testPath = path.resolve(__dirname);

describe('serve with devServer in config', () => {
let port;

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

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

if (isWindows) {
// TODO fix me on windows
it('compiles without flags', () => {
expect(true).toBe(true);

console.warn('TODO: fix `serve` test on windows');
});
} else {
it('Should pick up the host and port from config', async () => {
const { stdout, stderr } = await runServe([], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain('http://0.0.0.0:1234');
expect(stderr).toBeFalsy();
});

it('Port flag should override the config port', async () => {
const { stdout, stderr } = await runServe(['--port', port], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});

it('Passing hot flag works alongside other server config', async () => {
const { stdout, stderr } = await runServe(['--port', port, '--hot'], testPath);
// Should output the correct bundle file
expect(stdout).toContain('main.js');
// HMR is being used
expect(stdout).toContain('hot/dev-server.js');
// Runs at correct host and port
expect(stdout).toContain(`http://0.0.0.0:${port}`);
expect(stderr).toBeFalsy();
});
}
});
8 changes: 8 additions & 0 deletions test/serve/with-custom-port/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
mode: 'development',
devtool: false,
devServer: {
port: 1234,
host: '0.0.0.0',
},
};
10 changes: 10 additions & 0 deletions test/utils/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,19 @@ async function runInstall(cwd) {
});
}

const runServe = (args, testPath) => {
return runWatch({
testCase: testPath,
args: ['serve'].concat(args),
setOutput: false,
outputKillStr: 'main',
});
};

module.exports = {
run,
runWatch,
runServe,
runAndGetWatchProc,
extractSummary,
appendDataIfFileExists,
Expand Down

0 comments on commit e24a267

Please sign in to comment.