I want to create a reusable component that I can link to a single js file and share with other angular 2 applications. However, I keep gettingError: (SystemJS) Unexpected value 'undefined' declared by the module 'AppModule'
I use gulp to build my packages. The result is as follows:
build/pkg/basic.js (this is my reusable package)
build/app/main.js (this is one test app trying to use the package)
build/app/app.module.js
build/app/app.component.js
build/systemjs.config.js
build.index.html
I know that pkg / basic.js loads via a web page (I see the file in dev tools).
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
app.module.ts
import { BasicComponent } from 'basic/basic.component';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent, BasicComponent],
bootstrap: [ AppComponent, BasicComponent]
})
export class AppModule { }
app.component.ts
import { BasicComponent } from 'basic/basic.component';
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>My First Angular App</h1><basic></basic>'
})
export class AppComponent { }
basic.component.ts (my package of reusable components)
import { Component } from '@angular/core';
@Component({
selector: 'basic',
template: 'HI BASIC'
})
export class BasicComponent { }
systemjs.config.js
function (global) {
System.config({
paths: {
'npm:': 'lib/',
'pkg:': 'pkg/'
},
map: {
app: 'app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'rxjs': 'npm:rxjs',
'basic/basic.component': 'pkg:basic.js',
},
packages: {
'basic/basic.component': {
defaultExtension: 'js'
},
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
index.html
<html>
<head>
<title>Angular QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="lib/core-js/client/shim.min.js"></script>
<script src="lib/zone.js/dist/zone.js"></script>
<script src="lib/reflect-metadata/Reflect.js"></script>
<script src="lib/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<div style="float:left">
<my-app>Loading...</my-app>
</div>
</body>
</html>
key parts of my gulpfile
gulp.task("app:compile", [], () => {
let tsResult = gulp.src(["src/**/*.ts", "!src/basic/**/*.ts"])
.pipe(sourcemaps.init())
.pipe(tsc(tsProject))
.pipe(sourcemaps.write(".", {sourceRoot: '/src'}))
.pipe(gulp.dest("build"));
});
gulp.task("pkg:basic:compile", [], () => {
let tsResult = gulp.src("src/basic/**/*.ts")
.pipe(sourcemaps.init())
.pipe(inlineNg2Template())
.pipe(tsc({
typescript: require('typescript'),
target: 'ES5',
module: 'System',
experimentalDecorators: true,
emitDecoratorMetadata: true,
outFile: 'basic.js',
"rootDir": "./src",
"outDir": "./build",
moduleResolution: "node",
}))
.pipe(sourcemaps.write(".", {sourceRoot: '/src'}))
.pipe(gulp.dest("build/pkg"));
});
gulp.task("resources", () => {
return gulp.src(["src/**/*", "!**/*.ts"])
.pipe(gulp.dest("build"));
});
gulp.task("libs", () => {
return gulp.src([
'core-js/client/shim.min.js',
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'reflect-metadata/Reflect.js',
'rxjs/**',
'zone.js/dist/**',
'@angular/**',
'q/q.js',
'aws-sdk/dist/aws-sdk.min.js'
], {cwd: "node_modules/**"})
.pipe(gulp.dest("build/lib"));
});
gulp.task("static", () => {
return gulp.src([
'systemjs.config.js'
])
.pipe(gulp.dest("build"));
});
gulp.task("build", ['app:compile', 'pkg:basic:compile','resources', 'libs', 'static'], () => {
console.log("Building the project ...");
});