Skip to content

Commit

Permalink
Fix typings: webpack stats can be undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanelian committed Nov 20, 2020
1 parent fc82661 commit d10f410
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 20 deletions.
11 changes: 5 additions & 6 deletions bin/ProcessInvoke.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/TypeScriptBuildEngine.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions bin/VueLoaderVersions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bin/loaders/CoreTypeScriptLoader.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bin/loaders/HtmLoader.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/ProcessInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UserSettingsPath } from './user-settings/UserSettingsPath';
import { VuePackageVersions } from './variables-factory/BuildVariables';
import chalk = require('chalk');
import { Shout } from './Shout';
import { VueLoaderVersions } from './VueLoaderVersions';

/**
* Runs a child process that displays outputs to current command line output.
Expand Down Expand Up @@ -68,20 +69,18 @@ export async function selectPackageManager(preference: string | undefined, root:

function getVueCompilerServicePackageVersions(versions: VuePackageVersions): string | undefined {
if (versions.vue?.startsWith('2')) {
const loaderVersion = '15.9.5';
if (versions.loader === loaderVersion && versions.compilerService === versions.vue) {
if (versions.loader === VueLoaderVersions.Vue2Loader && versions.compilerService === versions.vue) {
return undefined;
} else {
return `vue-loader@${loaderVersion} vue-template-compiler@${versions.vue}`;
return `vue-loader@${VueLoaderVersions.Vue2Loader} vue-template-compiler@${versions.vue}`;
}
}

if (versions.vue?.startsWith('3')) {
const loaderVersion = '16.0.0-rc.1';
if (versions.loader === loaderVersion && versions.compilerService === versions.vue) {
if (versions.loader === VueLoaderVersions.Vue3Loader && versions.compilerService === versions.vue) {
return undefined;
} else {
return `vue-loader@${loaderVersion} @vue/compiler-sfc@${versions.vue}`;
return `vue-loader@${VueLoaderVersions.Vue3Loader} @vue/compiler-sfc@${versions.vue}`;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/TypeScriptBuildEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ export class TypeScriptBuildEngine {
return config;
}

buildOnce(webpackConfiguration: webpack.Configuration): Promise<webpack.Stats> {
buildOnce(webpackConfiguration: webpack.Configuration): Promise<webpack.Stats | undefined> {
// https://github.com/webpack/changelog-v5/blob/master/README.md#compiler-idle-and-close
// The webpack() facade automatically calls close when being passed a callback.
return new Promise<webpack.Stats>((ok, reject) => {
return new Promise<webpack.Stats | undefined>((ok, reject) => {
webpack(webpackConfiguration, (err, stats) => {
if (err) {
reject(err);
Expand Down Expand Up @@ -500,7 +500,7 @@ export class TypeScriptBuildEngine {
await this.watch(webpackConfiguration);
} else {
const stats = await this.buildOnce(webpackConfiguration);
if (this.variables.stats) {
if (this.variables.stats && stats) {
await fse.outputJson(this.finder.statsJsonFilePath, stats.toJson());
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/VueLoaderVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const VueLoaderVersions = {
Vue2Loader: "15.9.5",
Vue3Loader: "16.0.0-rc.2"
};

export { VueLoaderVersions };
9 changes: 7 additions & 2 deletions src/loaders/CoreTypeScriptLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const tsconfigJson = {

const tsconfig = parseTypescriptConfig(fixtures, tsconfigJson);

async function compileAsync(entry: string): Promise<webpack.Stats> {
async function compileAsync(entry: string): Promise<webpack.Stats | undefined> {
const compiler = webpack({
context: fixtures,
entry: [entry],
Expand All @@ -55,7 +55,7 @@ async function compileAsync(entry: string): Promise<webpack.Stats> {

compiler.outputFileSystem = new memoryFS();

return await new Promise<webpack.Stats>((ok, reject) => {
return await new Promise<webpack.Stats | undefined>((ok, reject) => {
compiler.run((err, stats) => {
if (err) {
reject(err);
Expand All @@ -70,6 +70,11 @@ test('Core TypeScript Loader: ES5', async t => {
const entry = path.join(fixtures, 'index.ts');
const stats = await compileAsync(entry);

if (!stats){
t.fail('webpack stats is undefined!');
return;
}

const o = stats.toJson({
source: true,
modules: true
Expand Down
9 changes: 7 additions & 2 deletions src/loaders/HtmLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LoaderPaths } from './LoaderPaths';
const root = process.cwd();
const fixtures = path.join(root, 'fixtures', 'HtmLoader');

async function compileAsync(entry: string): Promise<webpack.Stats> {
async function compileAsync(entry: string): Promise<webpack.Stats | undefined> {
const compiler = webpack({
context: fixtures,
entry: [entry],
Expand All @@ -31,7 +31,7 @@ async function compileAsync(entry: string): Promise<webpack.Stats> {

compiler.outputFileSystem = new memoryFS();

return await new Promise<webpack.Stats>((ok, reject) => {
return await new Promise<webpack.Stats | undefined>((ok, reject) => {
compiler.run((err, stats) => {
if (err) {
reject(err);
Expand All @@ -46,6 +46,11 @@ test('HTML Loader', async t => {
const entry = path.join(fixtures, 'index.ts');
const stats = await compileAsync(entry);

if (!stats){
t.fail('webpack stats is undefined!');
return;
}

const o = stats.toJson({
source: true,
modules: true
Expand Down

0 comments on commit d10f410

Please sign in to comment.