Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit bc0fc63

Browse files
committed
refactor(webpack): check for compiler before preprending ionic core code
check for compiler before preprending ionic core code
1 parent fb0584c commit bc0fc63

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

src/core/bundle-components.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,22 @@ function getCoreCompiler(context: BuildContext): CoreCompiler {
4949
}
5050
return null;
5151
}
52+
53+
// In serve mode, we only want to do the look-up for the compiler once
54+
let cachedCompilerModuleResult: CompilerModuleResult = null;
55+
export function doesCompilerExist(context: BuildContext): boolean {
56+
if (!cachedCompilerModuleResult) {
57+
const result = getCoreCompiler(context);
58+
cachedCompilerModuleResult = {
59+
found: result ? true : false,
60+
module: result ? result : null
61+
};
62+
}
63+
64+
return cachedCompilerModuleResult.found;
65+
}
66+
67+
interface CompilerModuleResult {
68+
found: boolean;
69+
module: any;
70+
};

src/webpack.spec.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { join } from 'path';
33
import * as webpack from './webpack';
44
import { FileCache } from './util/file-cache';
55
import * as helpers from './util/helpers';
6+
import * as ionicGlobal from './core/ionic-global';
7+
import * as bundleComponents from './core/bundle-components';
68

79
describe('Webpack Task', () => {
810
describe('writeBundleFilesToDisk', () => {
@@ -43,6 +45,8 @@ describe('Webpack Task', () => {
4345
context.fileCache.set(fileTwelvePath, { path: fileTwelvePath, content: fileTwelvePath + 'content'});
4446

4547
const writeFileSpy = spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
48+
spyOn(ionicGlobal, ionicGlobal.prependIonicGlobal.name);
49+
spyOn(bundleComponents, bundleComponents.doesCompilerExist.name).and.returnValue(false);
4650

4751
const promise = webpack.writeBundleFilesToDisk(context);
4852

@@ -52,8 +56,7 @@ describe('Webpack Task', () => {
5256
expect(writeFileSpy.calls.all()[0].args[0]).toEqual(fileOnePath);
5357

5458
// igore the appended ionic global
55-
let mainBundleContent = fileOnePath + 'content';
56-
expect(mainBundleContent.indexOf(writeFileSpy.calls.all()[0].args[1])).toEqual(-1);
59+
expect(writeFileSpy.calls.all()[0].args[1]).toEqual(fileOnePath + 'content');
5760

5861
expect(writeFileSpy.calls.all()[1].args[0]).toEqual(fileTwoPath);
5962

@@ -68,6 +71,49 @@ describe('Webpack Task', () => {
6871

6972
expect(writeFileSpy.calls.all()[5].args[0]).toEqual(fileSixPath);
7073
expect(writeFileSpy.calls.all()[5].args[1]).toEqual(fileSixPath + 'content');
74+
75+
expect(ionicGlobal.prependIonicGlobal).not.toHaveBeenCalled();
76+
});
77+
});
78+
79+
it('should preprend ionic core info', () => {
80+
const appDir = join('some', 'fake', 'dir', 'myApp');
81+
const buildDir = join(appDir, 'www', 'build');
82+
83+
const context = {
84+
fileCache: new FileCache(),
85+
buildDir: buildDir,
86+
outputJsFileName: 'main.js'
87+
};
88+
89+
const fileOnePath = join(buildDir, 'main.js');
90+
const fileTwoPath = join(buildDir, 'main.js.map');
91+
92+
context.fileCache.set(fileOnePath, { path: fileOnePath, content: fileOnePath + 'content'});
93+
context.fileCache.set(fileTwoPath, { path: fileTwoPath, content: fileTwoPath + 'content'});
94+
95+
const prependIonicGlobalData = {
96+
code: 'someCode',
97+
map: 'someString'
98+
};
99+
100+
const writeFileSpy = spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
101+
spyOn(ionicGlobal, ionicGlobal.prependIonicGlobal.name).and.returnValue(prependIonicGlobalData);
102+
spyOn(bundleComponents, bundleComponents.doesCompilerExist.name).and.returnValue(true);
103+
104+
const promise = webpack.writeBundleFilesToDisk(context);
105+
106+
return promise.then(() => {
107+
expect(writeFileSpy).toHaveBeenCalledTimes(2);
108+
109+
expect(writeFileSpy.calls.all()[0].args[0]).toEqual(fileOnePath);
110+
111+
// igore the appended ionic global
112+
expect(writeFileSpy.calls.all()[0].args[1]).toEqual(prependIonicGlobalData.code);
113+
114+
expect(writeFileSpy.calls.all()[1].args[0]).toEqual(fileTwoPath);
115+
116+
expect(ionicGlobal.prependIonicGlobal).toHaveBeenCalled();
71117
});
72118
});
73119
});

src/webpack.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { prependIonicGlobal } from './core/ionic-global';
21
import { EventEmitter } from 'events';
32
import { dirname, join } from 'path';
43

54
import * as webpackApi from 'webpack';
65

6+
import { prependIonicGlobal } from './core/ionic-global';
7+
import { doesCompilerExist } from './core/bundle-components';
78
import { Logger } from './logger/logger';
89
import { fillConfigDefaults, getUserConfigFile, replacePathVars } from './util/config';
910
import * as Constants from './util/constants';
@@ -118,7 +119,7 @@ export function writeBundleFilesToDisk(context: BuildContext) {
118119

119120
const mainJsFile = context.fileCache.get(mainJsPath);
120121
const mainJsMapFile = context.fileCache.get(mainJsMapPath);
121-
if (mainJsFile) {
122+
if (mainJsFile && doesCompilerExist(context)) {
122123
const ionicBundle = prependIonicGlobal(context, context.outputJsFileName, mainJsFile.content);
123124

124125
mainJsFile.content = ionicBundle.code;
@@ -140,18 +141,15 @@ export function runWebpackFullBuild(config: WebpackConfig) {
140141
const callback = (err: Error, stats: any) => {
141142
if (err) {
142143
reject(new BuildError(err));
143-
}
144-
else {
144+
} else {
145145
const info = stats.toJson();
146146

147147
if (stats.hasErrors()) {
148148
reject(new BuildError(info.errors));
149-
}
150-
else if (stats.hasWarnings()) {
149+
} else if (stats.hasWarnings()) {
151150
Logger.debug(info.warnings)
152151
resolve(stats);
153-
}
154-
else {
152+
} else {
155153
resolve(stats);
156154
}
157155
}

0 commit comments

Comments
 (0)