Skip to content

Commit 972cf6f

Browse files
committed
perf(minify): remove h() function when not used
1 parent 15936f1 commit 972cf6f

9 files changed

Lines changed: 33 additions & 27 deletions

File tree

src/compiler/app/app-core-browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function wrapCoreJs(config: d.Config, jsContent: string, cmpRegistry: d.C
6161
const cmpLoaderRegistryStr = JSON.stringify(cmpLoaderRegistry);
6262

6363
const output = [
64-
generatePreamble(config) + '\n',
64+
generatePreamble(config, {defaultBanner: true}) + '\n',
6565
`(${buildConditionals.es5 ? 'function' : ''}(w,d,x,n,h,c,r)${buildConditionals.es5 ? '' : '=>'}{`,
6666
buildConditionals.es5 ? `"use strict";\n` : ``,
6767
`(${buildConditionals.es5 ? 'function' : ''}(s)${buildConditionals.es5 ? '' : '=>'}{s&&(r=s.getAttribute('data-resources-url'))})(d.querySelector("script[data-namespace='${config.fsNamespace}']"));\n`,

src/compiler/app/app-core-esm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function writeEsmCore(config: d.Config, compilerCtx: d.CompilerCtx, buildC
5454

5555
jsContent = jsContent.replace('__APP__HYDRATED__CSS__PLACEHOLDER__', config.hydratedCssClass);
5656

57-
jsContent = generatePreamble(config, { prefix: `${config.namespace}: Core, ${sourceTarget}` }) + '\n' + jsContent;
57+
jsContent = generatePreamble(config, { prefix: `${config.namespace}: Core, ${sourceTarget}`, defaultBanner: true }) + '\n' + jsContent;
5858

5959
await compilerCtx.fs.writeFile(coreEsm, jsContent);
6060

src/compiler/app/test/app-core.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('app-core', () => {
2828
});
2929

3030
it('starts with the preamble', () => {
31-
const preamble = generatePreamble(config).trim();
31+
const preamble = generatePreamble(config, {defaultBanner: true}).trim();
3232
const lines = core.wrapCoreJs(config, '', cmpRegistry, {} as any).split('\n');
3333
expect(lines[0]).toEqual(preamble);
3434
});

src/compiler/build/test/build.spec.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,14 @@ describe('build', () => {
5454
expect(r.diagnostics).toEqual([]);
5555

5656
const cmpA = await c.fs.readFile(path.join(root, 'www', 'build', 'app', 'cmp-a.es5.entry.js'));
57-
expect(cmpA).toContain('Built with http://stenciljs.com');
58-
expect(cmpA).toContain('App.loadBundle("cmp-a"');
59-
expect(cmpA).toContain('someFn(!0)');
60-
expect(cmpA).not.toContain('/** minify me plz **/');
57+
expect(cmpA).toEqual(`App.loadBundle("cmp-a",["exports","./chunk-97e00951.js"],function(e,n){window;var u=function(){function e(){n.someFn(!0)}return Object.defineProperty(e,"is",{get:function(){return"cmp-a"},enumerable:!0,configurable:!0}),e}();e.CmpA=u,Object.defineProperty(e,"__esModule",{value:!0})});`);
6158

6259
const cmpB = await c.fs.readFile(path.join(root, 'www', 'build', 'app', 'cmp-b.es5.entry.js'));
63-
expect(cmpB).toContain('Built with http://stenciljs.com');
64-
expect(cmpB).toContain('App.loadBundle("cmp-b"');
65-
expect(cmpB).toContain('someFn(!0)');
66-
expect(cmpB).not.toContain('/** minify me plz **/');
60+
expect(cmpB).toEqual(`App.loadBundle("cmp-b",["exports","./chunk-97e00951.js"],function(e,n){window;var u=function(){function e(){n.someFn(!0)}return Object.defineProperty(e,"is",{get:function(){return"cmp-b"},enumerable:!0,configurable:!0}),e}();e.CmpB=u,Object.defineProperty(e,"__esModule",{value:!0})});`);
6761

6862
const chunkFileName = r.filesWritten.find(f => f.includes('chunk') && f.includes('es5'));
6963
const chunk = await c.fs.readFile(chunkFileName);
70-
expect(chunk).toContain('Built with http://stenciljs.com');
71-
expect(chunk).toContain('.someFn=function(');
72-
expect(chunk).toContain('console.log(!0)');
64+
expect(chunk).toEqual(`App.loadBundle("chunk-97e00951.js",["exports"],function(o){window,o.someFn=function(o){o?console.log(!0):console.log(!1)}});`);
7365
});
7466

7567
it('should minify es2017 build', async () => {
@@ -85,7 +77,7 @@ describe('build', () => {
8577
expect(r.hasSvg).toBe(false);
8678

8779
const output = await c.fs.readFile(path.join(root, 'www', 'build', 'app', 'cmp-a.entry.js'));
88-
expect(output).toContain('/*! Built with http://stenciljs.com */\nconst{h:t}=window.App;class s{static get is(){return"cmp-a"}}export{s as CmpA};');
80+
expect(output).toEqual('class t{static get is(){return"cmp-a"}}export{t as CmpA};');
8981
});
9082

9183
it('should build app files, app global and component', async () => {

src/compiler/bundle/derive-modules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ async function transpileEs5Bundle(config: d.Config, compilerCtx: d.CompilerCtx,
100100

101101
function generateIntro(config: d.Config, isBrowser: boolean) {
102102
return isBrowser
103-
? `const { h } = window.${config.namespace};`
103+
? `const h = window.${config.namespace}.h;`
104104
: `import { h } from '../${getCoreEsmFileName(config)}';`;
105105
}

src/compiler/minifier.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@ import { generatePreamble } from './util';
66
* Interal minifier, not exposed publicly.
77
*/
88
export async function minifyJs(config: d.Config, compilerCtx: d.CompilerCtx, diagnostics: d.Diagnostic[], jsText: string, sourceTarget: d.SourceTarget, preamble: boolean, buildTimestamp?: string): Promise<string> {
9-
const opts: any = { output: {}, compress: {}, mangle: true };
9+
const opts: any = {
10+
output: {beautify: false},
11+
compress: {},
12+
mangle: true
13+
};
1014

1115
if (sourceTarget === 'es5') {
1216
opts.ecma = 5;
1317
opts.output.ecma = 5;
1418
opts.compress.ecma = 5;
1519
opts.compress.arrows = false;
1620
opts.compress.pure_getters = true;
17-
opts.output.beautify = false;
1821

1922
} else {
2023
opts.ecma = 7;
2124
opts.toplevel = true;
25+
opts.module = true;
2226
opts.output.ecma = 7;
2327
opts.compress.ecma = 7;
2428
opts.compress.arrows = true;
2529
opts.compress.module = true;
26-
opts.output.beautify = false;
30+
opts.compress.pure_getters = true;
2731
}
2832

2933
if (config.logLevel === 'debug') {
@@ -55,9 +59,9 @@ export async function minifyJs(config: d.Config, compilerCtx: d.CompilerCtx, dia
5559
}
5660

5761
const r = await config.sys.minifyJs(jsText, opts);
58-
59-
if (compilerCtx) {
60-
if (r && r.diagnostics.length === 0 && typeof r.output === 'string') {
62+
if (r && r.diagnostics.length === 0 && typeof r.output === 'string') {
63+
r.output = auxMinify(r.output);
64+
if (compilerCtx) {
6165
await compilerCtx.cache.put(cacheKey, r.output);
6266
}
6367
}
@@ -69,3 +73,7 @@ export async function minifyJs(config: d.Config, compilerCtx: d.CompilerCtx, dia
6973
return r.output;
7074
}
7175
}
76+
77+
function auxMinify(jsText: string) {
78+
return jsText.replace(/^window;/, '');
79+
}

src/compiler/style/test/style.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('component-styles', () => {
100100
expect(iosContent).toContain(`body{font-family:Helvetica}`);
101101
expect(iosContent).toContain(`static get styleMode(){return"ios"}`);
102102

103-
const mdContent = await c.fs.readFile(path.join(root, 'www', 'build', 'app', 'o.entry.js'));
103+
const mdContent = await c.fs.readFile(path.join(root, 'www', 'build', 'app', 'h.entry.js'));
104104
expect(mdContent).toContain(`body{font-family:Roboto}`);
105105
expect(mdContent).toContain(`static get styleMode(){return"md"}`);
106106
});

src/compiler/util.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function isWebDevFile(filePath: string) {
8080
const WEB_DEV_EXT = ['js', 'jsx', 'html', 'htm', 'css', 'scss', 'sass', 'less', 'styl', 'pcss'];
8181

8282

83-
export function generatePreamble(config: d.Config, opts: { prefix?: string; suffix?: string } = {}) {
83+
export function generatePreamble(config: d.Config, opts: { prefix?: string; suffix?: string, defaultBanner?: boolean } = {}) {
8484
let preamble: string[] = [];
8585

8686
if (config.preamble) {
@@ -93,7 +93,9 @@ export function generatePreamble(config: d.Config, opts: { prefix?: string; suff
9393
});
9494
}
9595

96-
preamble.push(BANNER);
96+
if (opts.defaultBanner === true) {
97+
preamble.push(BANNER);
98+
}
9799

98100
if (typeof opts.suffix === 'string') {
99101
opts.suffix.split('\n').forEach(c => {
@@ -110,7 +112,11 @@ export function generatePreamble(config: d.Config, opts: { prefix?: string; suff
110112
return preamble.join('\n');
111113
}
112114

113-
return `/*! ${BANNER} */`;
115+
116+
if (opts.defaultBanner === true) {
117+
return `/*! ${BANNER} */`;
118+
}
119+
return '';
114120
}
115121

116122

test/apps/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
| File | Brotli | Gzipped | Minified |
55
|----------------------------|----------|----------|----------|
6-
| hello-world.entry.js | 110B | 146B | 159B |
6+
| hello-world.entry.js | 106B | 135B | 144B |
77
| app.core.js | 3.52KB | 3.90KB | 8.33KB |
88

99

0 commit comments

Comments
 (0)