Skip to content

Commit ae66676

Browse files
fix: add lazy loading and aot base pipeline (#23)
1 parent 4d0faca commit ae66676

File tree

7 files changed

+46
-18
lines changed

7 files changed

+46
-18
lines changed

src/commands/serve.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import {
1212
import { resolve } from 'path'
1313
import { NgProdPlugin } from '../fusebox/ng.prod.plugin'
1414
import { NgPolyfillPlugin } from '../fusebox/ng.polyfill.plugin'
15-
import { NgCompilerPlugin } from '../fusebox/ng.compiler.plugin'
1615
import readConfig_ from '../utilities/read-config'
1716
import { Ng2TemplatePlugin } from 'ng2-fused'
1817
import { FuseProcess } from 'fuse-box/FuseProcess'
18+
import { NgAotFactoryPlugin } from '../fusebox/ng.aot-factory.plugin'
19+
import clearTerminal from '../utilities/clear'
20+
import { main as ngc } from '@angular/compiler-cli/src/main'
1921

2022
command(
2123
'serve [port][prod][aot][sw]',
@@ -63,6 +65,8 @@ function serve(isProdBuild = false) {
6365
? config.fusebox.browser.aotBrowserModule
6466
: config.fusebox.browser.browserModule
6567

68+
isAotBuild && ngc(['-p', resolve('tsconfig.aot.json')])
69+
6670
const fuseBrowser = FuseBox.init({
6771
log,
6872
modulesFolder,
@@ -72,12 +76,11 @@ function serve(isProdBuild = false) {
7276
target: 'browser@es5',
7377
useTypescriptCompiler: true,
7478
plugins: [
79+
isAotBuild && NgAotFactoryPlugin(),
7580
Ng2TemplatePlugin(),
7681
['*.component.html', RawPlugin()],
7782
NgProdPlugin({ enabled: isProdBuild }),
78-
NgCompilerPlugin({ enabled: isAotBuild }),
7983
NgPolyfillPlugin(),
80-
// NgOptimizerPlugin({ enabled: opts.enableAngularBuildOptimizer }),
8184
[
8285
'*.component.css',
8386
SassPlugin({
@@ -146,6 +149,7 @@ function serve(isProdBuild = false) {
146149
.instructions(` > [${config.fusebox.server.serverModule}]`)
147150
.completed(proc => {
148151
prevServerProcess && prevServerProcess.kill()
152+
clearTerminal()
149153
proc.start()
150154
prevServerProcess = proc
151155
})
@@ -156,6 +160,7 @@ function serve(isProdBuild = false) {
156160
.bundle('app')
157161
.watch(watchDir)
158162
.instructions(` !> [${browserModule}]`)
163+
.splitConfig({ dest: '../js/modules' })
159164

160165
logInfo('Bundling your application, this may take some time...')
161166
fuseBrowser.run()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Plugin, File } from 'fuse-box'
2+
3+
// tslint:disable:no-class
4+
// tslint:disable:no-this
5+
// tslint:disable:no-if-statement
6+
// tslint:disable:no-object-mutation
7+
// tslint:disable:readonly-keyword
8+
const defaults: NgAotFactoryPluginOptions = {}
9+
10+
export interface NgAotFactoryPluginOptions {}
11+
12+
export class NgAotFactoryPluginClass implements Plugin {
13+
constructor(public opts: NgAotFactoryPluginOptions = defaults) {}
14+
15+
public test: RegExp = /-routing.module.js/
16+
17+
onTypescriptTransform?(file: File) {
18+
if (!this.test.test(file.relativePath)) return
19+
const regex1 = new RegExp(/.module'/, 'g')
20+
const regex2 = new RegExp(/Module\);/, 'g')
21+
file.contents = file.contents.replace(regex1, ".module.ngfactory'")
22+
file.contents = file.contents.replace(regex2, 'ModuleNgFactory);')
23+
}
24+
}
25+
26+
export const NgAotFactoryPlugin = (options?: NgAotFactoryPluginOptions) =>
27+
new NgAotFactoryPluginClass(options)

src/fusebox/ng.compiler.plugin.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { main as ngc } from '@angular/compiler-cli/src/main'
2-
import { Plugin, File } from 'fuse-box'
2+
import { Plugin } from 'fuse-box'
33
import { resolve } from 'path'
44

55
// tslint:disable:no-class
@@ -15,17 +15,10 @@ export interface NgcPluginOptions {
1515

1616
export class NgcPluginClass implements Plugin {
1717
constructor(private opts: NgcPluginOptions = defaults) {}
18-
public test: RegExp = /-routing.module.js/
18+
1919
bundleStart() {
2020
this.opts.enabled && ngc(['-p', resolve('tsconfig.aot.json')])
2121
}
22-
23-
transform(file: File) {
24-
const regex1 = new RegExp(/.module'/, 'g')
25-
const regex2 = new RegExp(/Module\);/, 'g')
26-
file.contents = file.contents.replace(regex1, ".module.ngfactory'")
27-
file.contents = file.contents.replace(regex2, 'ModuleNgFactory);')
28-
}
2922
}
3023

3124
export const NgCompilerPlugin = (options?: NgcPluginOptions) =>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
:host {
2-
display: block;
1+
// apply global styles here
2+
3+
app-root {
4+
35
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Component, ChangeDetectionStrategy } from '@angular/core'
1+
import { Component, ChangeDetectionStrategy, ViewEncapsulation } from '@angular/core'
22

33
@Component({
44
selector: 'app-root',
55
templateUrl: './app.component.html',
66
styleUrls: ['./app.component.css'],
7-
changeDetection: ChangeDetectionStrategy.OnPush
7+
changeDetection: ChangeDetectionStrategy.OnPush,
8+
encapsulation: ViewEncapsulation.None
89
})
910
export class AppComponent {
1011
}

src/templates/core/app/app.module.ts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { SharedModule } from './app.shared.module'
1212
AppRoutingModule,
1313
TransferHttpCacheModule,
1414
SharedModule.forRoot(),
15-
BrowserModule.withServerTransition({ appId: 'my-app' }),
15+
BrowserModule.withServerTransition({ appId: 'app-root' }),
1616
]
1717
})
1818
export class AppModule { }

src/templates/core/browser/app.browser.module.ts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AppComponent } from '../app/app.component'
55

66
@NgModule({
77
imports: [
8-
BrowserModule.withServerTransition({ appId: 'pm-app' }),
8+
BrowserModule.withServerTransition({ appId: 'app-root' }),
99
BrowserTransferStateModule,
1010
AppModule
1111
],

0 commit comments

Comments
 (0)