Browserify + shim on jquery and signalR not working - gulp

I'm using gulp + browserify to bundle my source but i got always the same error : jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file. SignalR get $ = undefined...
I split my source into two bundle : vendor and app. Vendor bundle get lib's id from packages.json and the bundle require it. App bundle get main entry and i passe id's lib to this bundle with bundle.external.
Here my packages.json :
"browser": {
"angular-notify": "./node_modules/angular-notify/dist/angular-notify.js",
"angular-i18n": "./node_modules/angular-i18n/angular-locale_fr.js",
"jquery": "./node_modules/jquery/dist/jquery.js",
"signalR": "./node_modules/ms-signalr-client/jquery.signalr-2.2.0.js",
"moment": "./node_modules/moment/moment.js",
"moment-business": "./Scripts/Vendor/js/moment-business.js"
},
"browserify": {
"transform": [
"browserify-shim"
]
},
"browserify-shim": {
"jquery": "$",
"signalR": {
"depends": [
"jquery:jQuery"
]
},
"moment": "moment"
}
Here my gulp taks :
'use strict';
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import browserify from 'browserify';
import browserifyInc from 'browserify-incremental';
import ngHtml2Js from 'browserify-ng-html2js';
import shim from 'browserify-shim';
import xtend from 'xtend';
import tsify from 'tsify';
import babelify from 'babelify';
import minifyify from 'minifyify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import browserSync from 'browser-sync';
import packageJson from './package.json';
const $ = gulpLoadPlugins();
let bs = browserSync.create();
let dependenciesCss = [
'bootstrap',
'font-awesome',
'animate.css'
];
let externalDependenciesjs = [
'signalR',
'moment-business'
];
let dependenciesJs = Object.keys(packageJson.dependencies).filter(
key => (
dependenciesCss.every(
libCssName => (key.trim() !== libCssName)
)
)
);
dependenciesJs = dependenciesJs.concat(externalDependenciesjs);
/*************************************
* SCRIPTS (build) *
*************************************/
let extensions = ['.js', '.json', '.ts'];
let bundler = browserify(xtend(browserifyInc.args, {
entries: 'Scripts/App/app.ts',
debug: true,
extensions,
cache: {},
packageCache: {},
fullPaths: true
}))
.external(dependenciesJs)
.plugin(tsify, {
target: 'es6'
})
.transform(babelify.configure({
extensions,
}))
.plugin(minifyify, {
map: 'app.min.js.map',
output: 'Scripts/Dist/app.min.js.map'
});
function compile() {
bundler.on('log', $.util.log);
browserifyInc(bundler, {
cacheFile: './.tmp/browserify-cache.json'
});
$.util.log('Bundling JS ...');
return bundler.bundle()
.pipe($.plumber({
errorHandler: browserifyError
}))
.on('error', browserifyError)
.pipe(source('app.min.js'))
.pipe(buffer())
.pipe($.size({
title: 'scripts'
}))
.pipe(gulp.dest('Scripts/Dist'))
.pipe($.if(bs.active, bs.stream({
once: true
})));
}
let bundlerVendor = browserify(xtend(browserifyInc.args, {
debug: true,
extensions,
cache: {},
packageCache: {},
fullPaths: true
}))
.require(dependenciesJs)
.plugin(minifyify, {
map: 'vendor.min.js.map',
output: 'Scripts/Dist/vendor.min.js.map'
});
function compileVendor() {
bundlerVendor.on('log', $.util.log);
browserifyInc(bundlerVendor, {
cacheFile: './.tmp/browserify-vendor-cache.json'
});
$.util.log('Bundling vendor JS ...');
return bundlerVendor.bundle()
.pipe($.plumber({
errorHandler: browserifyError
}))
.on('error', browserifyError)
.pipe(source('vendor.min.js'))
.pipe(buffer())
.pipe($.size({
title: 'scripts vendor'
}))
.pipe(gulp.dest('Scripts/Dist'))
.pipe($.if(bs.active, bs.stream({
once: true
})));
}
function browserifyError(err) {
error(err);
this.end();
}
Vendor bundle haven't entry point, it only require lib.
Here my app bundle entry :
/// <reference path="_references.ts" />
import 'signalR';
import 'moment';
import 'moment-business';
import 'moment-range';
import 'angular';
import 'angular-messages';
import 'angular-mocks';
import 'angular-animate';
import 'angular-file-upload';
import 'angular-notify';
import 'angular-i18n';
import 'angular-ui-bootstrap';
import 'angular-ui-router';
import 'angular-vs-repeat';
import 'postal';
import Route from './route';
import * as Configuration from './config';
import register from './registerModule';
import {camelize} from './tools';
let modules: Array<string> = [
appName + '.Controllers',
appName + '.Directives',
appName + '.Filters',
appName + '.Services',
appName + '.Factory',
appName + '.Constant'];
modules.forEach((moduleName: string): ng.IModule => angular.module(moduleName, []));
register();
modules.push('templates');
modules.push('ui.router');
modules.push('ui.bootstrap');
modules.push('angularFileUpload');
modules.push('ngAnimate');
modules.push('ngMessages');
modules.push('cgNotify');
modules.push('vs-repeat');
angular.module(appName, modules);
angular.module(appName)
.config(
['$stateProvider', '$urlRouterProvider', '$locationProvider',
($stateProvider: ng.ui.IStateProvider,
$urlRouterProvider: ng.ui.IUrlRouterProvider,
$locationProvider: ng.ILocationProvider): Route => (
new Route($stateProvider, $urlRouterProvider, $locationProvider)
)
]);
angular.module(appName)
.config(['$logProvider', ($logProvider: ng.ILogProvider): void => {
$logProvider.debugEnabled(Configuration.ENABLED_CONSOLE_DEBUG);
}
]);
angular.module(appName)
.config(
['$provide', ($provide: ng.auto.IProvideService): void => {
/* tslint:disable:no-any */
$provide.decorator('$exceptionHandler', ['$delegate', '$window', ($delegate: Function, $window: ng.IWindowService): any => {
return (exception: any, cause: string): any => {
/* tslint:enable:no-any */
// utilisation du service $delegate pour formatter le message à afficher dans la console
$delegate(exception, cause);
};
}]);
}
]);
angular.module(appName)
.config(
['$provide', '$httpProvider', ($provide: ng.auto.IProvideService, $httpProvider: ng.IHttpProvider): void => {
$provide.factory('customHttpInterceptor', ['$q', ($q: ng.IQService) => {
return {
/* tslint:disable:no-any */
'response': (response: any): any=> (camelize(response))
/* tslint:enable:no-any */
};
}]);
$httpProvider.interceptors.push('customHttpInterceptor');
}]);
angular.module(appName).run(runAngular);
runAngular.$inject = ['$rootScope', '$location', '$log'];
function runAngular($rootScope: ng.IRootScopeService,
$location: ng.ILocationService,
$log: ng.ILogService): void {
'use strict';
$log.debug('Démarrage de l\'application : ', appName);
}
I already try to use browserify-shim transform with option global but this not work too.

You still have to import jQuery into your code. The depends section of browserify-shim just tells it that jQuery comes before SignalR in the bundle. It doesn't say that any time you import SignalR that it will automatically import jQuery first.
The exact solution depends on whether SignalR is expecting jQuery to simply be present in the bundle, whether it expects jQuery to be present on the window object, or whether SignalR is a jQuery plugin that could potentially need attaching manually to the $ object.
The first solution I'd try is to simply import jQuery before you import SignalR:
/// <reference path="_references.ts" />
import $ from 'jquery';
import 'signalR';
// Rest of app.js........

Related

pixi.js import issue with gulp & webpack

I have this gulp config file: (I removed some functions that do not interact with js)
const { src, dest, parallel, series, watch } = require('gulp')
const notify = require('gulp-notify')
const sourcemaps = require('gulp-sourcemaps')
const browserSync = require('browser-sync').create()
const del = require('del')
const webpackStream = require('webpack-stream')
const uglify = require('gulp-uglify-es').default
function scripts() {
return src('./src/assets/js/pages/*.js')
.pipe(
webpackStream({
mode: 'development',
output: {
filename: '[name].js',
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [['#babel/preset-env']],
},
},
},
],
},
})
)
.pipe(sourcemaps.init())
.pipe(uglify().on('error', notify.onError('')))
.pipe(sourcemaps.write('.'))
.pipe(dest('./app/assets/js'))
.pipe(browserSync.stream())
}
function watchFiles() {
browserSync.init({
server: {
baseDir: './app',
},
})
watch('./src/*.html', htmlInclude)
watch('./src/partials/**/*.html', htmlInclude)
watch('./src/assets/scss/**/*.scss', styles)
watch('./src/assets/images/**/*', imgToApp)
watch('./src/assets/svg/**.svg', svg)
watch('./src/assets/fonts/**/*', fonts)
watch('./src/assets/js/**/*.js', scripts)
watch('./src/assets/fav.ico', favIcon)
}
function favIcon() {
return src('./src/assets/fav.ico').pipe(dest('./app/assets/'))
}
function clean() {
return del(['app/*'])
}
module.exports = {
styles,
watchFiles,
default: series(
clean,
parallel(favIcon, htmlInclude, imgToApp, svg, fonts, styles, scripts),
watchFiles
),
}
So, when I try to import Pixi.js
import * as PIXI from 'pixi.js';
my js code doesn't work at all and the worst thing is that there are no errors in the console.
Also I need hsl-to-hex module in my project and I just import it and everything works
I tried this variant of importing import PIXI from 'pixi.js/dist/pixi.js';
Also tried to use some gulp plugins, but I not really understand what they do, so it didn't give any result

Exclude JSON files from the main bundle with webpack for react-lottie

In our web app we have a few JSON files that are ~10-80k lines each. These are getting included in our main bundle. These are used by an animation plugin called react-lottie.
An example of our webpack.config.js
module.exports = {
entry: ["./src/index.js"],
module: {
rules: [
{ test: /\.(js|jsx)$/, exclude: /node_modules/, use: ["babel-loader"] },
{
test: /\.(jpg|png|gif|ico)$/,
use: {
loader: "file-loader",
options: { name: "[path][name].[hash].[ext]" }
}
}
]
},
resolve: { extensions: ["*", ".js", ".jsx"] },
output: {
path: __dirname + "/dist",
publicPath: "/",
filename: "[name].[hash].js"
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({ hash: false, template: "src/index.html" }),
new DashboardPlugin(),
new CopyWebpackPlugin([
{
from: "src/components/Assets/BookingBar.js",
to: "assets/BookingBar.js"
}
]),
new BundleAnalyzerPlugin()
],
devServer: {
contentBase: "./dist",
hot: true,
historyApiFallback: true,
port: 4000
}
};
What is the expected behavior?
There should be a way to exclude .json files from the main bundle. I've tried File-Loader, json-loader, and const someJson = require(./someJson)
Other relevant information:
webpack version: 4.16.1
Node.js version: 10.12.0
Operating System: Mac OS 10.14 Mojave
ANSWER BELOW (AT LEAST FOR HOW I SOLVED IT). I couldn't initialize the lottie without any data.
The expected behavior is that the JSON will get bundled because it's, presumably, needed synchronously at runtime. JSON data differs from something like image files which are loaded asynchronously by the browser as they are rendered on the page via src attributes etc.
As the comments mentioned, you should be using code splitting. The latest version of Webpack supports dynamic imports if you install and use the #babel/plugin-syntax-dynamic-import plugin.
npm install --save-dev #babel/plugin-syntax-dynamic-import
Then in babel.config.js:
module.exports = {
...
plugins: [
"#babel/plugin-syntax-dynamic-import"
]
...
};
Example
Say you have a React component that might need some JSON data, but doesn't need to load it synchronously as part of the bundle. Your non-code splitted version might look something like this:
import React from 'react';
import myJSON from './myJSON.json';
export default class MyComponent extends React.Component {
render() {
return <div>{JSON.stringify(myJSON, null, 2)}</div>
}
}
Instead you can use a dynamic import - basically a runtime import that returns a Promise you can use to asynchronously load some data chunked separately from your bundle:
import React from 'react';
import myJSON from './myJSON.json';
export default class MyComponent extends React.Component {
state = {data: {}};
componentDidMount() {
import(/* webpackChunkName: 'myJSON' */ './myJSON.json')
.then((data) => {
this.setState({data});
});
}
render() {
return <div>{JSON.stringify(this.state.data, null, 2)}</div>
}
}
Alternately, you can use React's new lazy and Suspense API (v16.6.0 and higher) to dynamically import React components that get chunked separately from the bundle. This might be preferable if you want to chunk a component and its corresponding JSON data together, but separately from the main bundle:
// MyComponent.jsx
import React from 'react';
import myJSON from './myJSON.json';
export default class MyComponent extends React.Component {
render() {
return <div>{JSON.stringify(myJSON, null, 2)}</div>
}
}
// SomeParent.jsx
import React, {lazy, Suspense} from 'react';
const MyComponent = lazy(() => import(/* webpackChunkName: 'MyComponent' */ './MyComponent'));
export default class SomeParent extends React.Component {
render() {
return <div>
<Suspense fallback={<div>Loading...<div>} >
<MyComponent />
</Suspense>
</div>;
}
}
In the above example, <MyComponent /> and its corresponding code -- including the JSON data -- will only be loaded when the component is actually rendered at runtime.
Ultimately I took the answer above below me but wasn't able to initialize the lottie without any JSON data. I ended up doing this:
import React, { PureComponent } from "react"
import Lottie from 'react-lottie'
export default class AnimationAutomatedCommunication extends PureComponent {
constructor(props) {
super(props)
this.state = {
animation: <div />
}
}
async componentDidMount() {
const animation = await import(/* webpackChunkName: "AnimationAutomatedCommunication" */ './JsonData/AnimationAutomatedCommunication.json')
const defaultOptions = {
loop: true,
autoplay: true,
animationData: animation.default
}
this.setState({
animation: <div className={this.props.className}>
<Lottie key="lottie-win-jobs" options={defaultOptions}
isStopped={this.props.isStopped} />
</div>
})
}
render() {
return (
<React.Fragment>
{this.state.animation}
</React.Fragment>
)
}
}

Why can't I import * these Javascript Files?

I'm trying to import these files inside the perimeters folder
basePerimeter.js
byePerimeter.js
secretPerimeter.js
my import code:
import * as perimeters from '../perimeters'
basePerimeter.js
import { Perimeter } from 'vue-kindergarten';
export default class BasePerimeter extends Perimeter {
isAdmin() {
return this.child && this.child.role === 'admin';
}
}
byePerimeter.js
import basePerimeter from './basePerimeter';
export default new basePerimeter({
purpose: 'bye',
govern: {
'can route': () => true,
'can viewParagraph': function () {
return this.isAdmin();
},
},
});
secretPerimeter.js
import basePerimeter from './basePerimeter';
export default new basePerimeter({
purpose: 'secret',
govern: {
'can route': function () {
return this.isAdmin();
},
},
});
but if I import it individually, it works.
Like this:
import basePerimeter from '../perimeters/basePerimeter'
I need to import via * because of this code:
router.beforeEach((to, from, next) => {
const perimeter = perimeters[`${to.name}Perimeter`];
if (perimeter) {
const sandbox = createSandbox(child(store), {
perimeters: [
perimeter,
],
});
if (!sandbox.isAllowed('route')) {
return next('/');
}
}
return next();
});
Why is it throwing this error:
ERROR in ./src/router/index.js
Module not found: Error: Can't resolve '../perimeters' in 'E:\my\vue\instance\src\router'
# ./src/router/index.js 13:0-44
# ./src/main.js
# multi ./build/dev-client ./src/main.js
I Don't know what's happening here but adding an index.js file and importing the files I needed solved my problem.
index.js
import byePerimeter from './byePerimeter'
import secretPerimeter from './secretPerimeter'
export {
byePerimeter,
secretPerimeter
}

Using Jest to test a Link from react-router v4

I'm using jest to test a component with a <Link> from react-router v4.
I get a warning that <Link /> requires the context from a react-router <Router /> component.
How can I mock or provide a router context in my test? (Basically how do I resolve this warning?)
Link.test.js
import React from 'react';
import renderer from 'react-test-renderer';
import { Link } from 'react-router-dom';
test('Link matches snapshot', () => {
const component = renderer.create(
<Link to="#" />
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
The warning when the test is run:
Warning: Failed context type: The context `router` is marked
as required in `Link`, but its value is `undefined`.
You can wrap your component in the test with the StaticRouter to get the router context into your component:
import React from 'react';
import renderer from 'react-test-renderer';
import { Link } from 'react-router-dom';
import { StaticRouter } from 'react-router'
test('Link matches snapshot', () => {
const component = renderer.create(
<StaticRouter location="someLocation" context={context}>
<Link to="#" />
</StaticRouter>
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
Have a look at the react router docs about testing
I had the same issue and using StaticRouter would still require the context which needed more configuration to have it available in my test, so I ended up using the MemoryRouter which worked very well and without any issues.
import React from 'react';
import renderer from 'react-test-renderer';
import { MemoryRouter } from 'react-router-dom';
// SampleComponent imports Link internally
import SampleComponent from '../SampleComponent';
describe('SampleComponent', () => {
test('should render', () => {
const component = renderer
.create(
<MemoryRouter>
<SampleComponent />
</MemoryRouter>
)
.toJSON();
expect(component).toMatchSnapshot();
});
});
The answer of #Mahdi worked for me! In 2023 if you want to test a component that includes <Link> or <NavLink>, we just need to wrap it with the <MemoryRouter> in the test file:
// App.test.js
import { render, screen } from "#testing-library/react";
import MyComponent from "./components/MyComponent";
import { MemoryRouter } from "react-router-dom"; // <-- Import MemoryRouter
test("My test description", () => {
render(
<MemoryRouter> // <-- Wrap!
<MyComponent />
</MemoryRouter>
);
});
my test like this:
import * as React from 'react'
import DataBaseAccout from '../database-account/database-account.component'
import { mount } from 'enzyme'
import { expect } from 'chai'
import { createStore } from 'redux'
import reducers from '../../../reducer/reducer'
import { MemoryRouter } from 'react-router'
let store = createStore(reducers)
describe('mount database-account', () => {
let wrapper
beforeEach(() => {
wrapper = mount(
< MemoryRouter >
<DataBaseAccout store={store} />
</MemoryRouter >
)
})
afterEach(() => {
wrapper.unmount()
wrapper = null
})
})
but I don't konw why MemoryRouter can solve this。
Above solutions have a common default defact:
Can't access your component's instance! Because the MemoryRouter or StaticRouter component wrapped your component.
So the best to solve this problem is mock a router context, code as follows:
import { configure, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
describe('YourComponent', () => {
test('test component with react router', () => {
// mock react-router context to avoid violation error
const context = {
childContextTypes: {
router: () => void 0,
},
context: {
router: {
history: createMemoryHistory(),
route: {
location: {
hash: '',
pathname: '',
search: '',
state: '',
},
match: { params: {}, isExact: false, path: '', url: '' },
}
}
}
};
// mount component with router context and get component's instance
const wrapper = mount(<YourComponent/>, context);
// access your component as you wish
console.log(wrapper.props(), wrapper.state())
});
beforeAll(() => {
configure({ adapter: new Adapter() });
});
});

Ensure json configuration is loaded in Angular2 [duplicate]

Is there a way to pass arguments rendered on the backend to angular2 bootstrap method? I want to set http header for all requests using BaseRequestOptions with value provided from the backend. My main.ts file looks like this:
import { bootstrap } from '#angular/platform-browser-dynamic';
import { AppComponent } from "./app.component.ts";
bootstrap(AppComponent);
I found how to pass this arguments to root component (https://stackoverflow.com/a/35553650/3455681), but i need it when I'm fireing bootstrap method... Any ideas?
edit:
webpack.config.js content:
module.exports = {
entry: {
app: "./Scripts/app/main.ts"
},
output: {
filename: "./Scripts/build/[name].js"
},
resolve: {
extensions: ["", ".ts", ".js"]
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts-loader'
}
]
}
};
update2
Plunker example
update AoT
To work with AoT the factory closure needs to be moved out
function loadContext(context: ContextService) {
return () => context.load();
}
#NgModule({
...
providers: [ ..., ContextService, { provide: APP_INITIALIZER, useFactory: loadContext, deps: [ContextService], multi: true } ],
See also https://github.com/angular/angular/issues/11262
update an RC.6 and 2.0.0 final example
function configServiceFactory (config: ConfigService) {
return () => config.load();
}
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule,
routes,
FormsModule,
HttpModule],
providers: [AuthService,
Title,
appRoutingProviders,
ConfigService,
{ provide: APP_INITIALIZER,
useFactory: configServiceFactory
deps: [ConfigService],
multi: true }
],
bootstrap: [AppComponent]
})
export class AppModule { }
If there is no need to wait for the initialization to complete, the constructor of `class AppModule {} can also be used:
class AppModule {
constructor(/*inject required dependencies */) {...}
}
hint (cyclic dependency)
For example injecting the router can cause cyclic dependencies.
To work around, inject the Injector and get the dependency by
this.myDep = injector.get(MyDependency);
instead of injecting MyDependency directly like:
#Injectable()
export class ConfigService {
private router:Router;
constructor(/*private router:Router*/ injector:Injector) {
setTimeout(() => this.router = injector.get(Router));
}
}
update
This should work the same in RC.5 but instead add the provider to providers: [...] of the root module instead of bootstrap(...)
(not tested myself yet).
update
An interesting approach to do it entirely inside Angular is explained here https://github.com/angular/angular/issues/9047#issuecomment-224075188
You can use APP_INITIALIZER which will execute a function when the
app is initialized and delay what it provides if the function returns
a promise. This means the app can be initializing without quite so
much latency and you can also use the existing services and framework
features.
As an example, suppose you have a multi-tenanted solution where the
site info relies on the domain name it's being served from. This can
be [name].letterpress.com or a custom domain which is matched on the
full hostname. We can hide the fact that this is behind a promise by
using APP_INITIALIZER.
In bootstrap:
{provide: APP_INITIALIZER, useFactory: (sites:SitesService) => () => sites.load(), deps:[SitesService, HTTP_PROVIDERS], multi: true}),
sites.service.ts:
#Injectable()
export class SitesService {
public current:Site;
constructor(private http:Http, private config:Config) { }
load():Promise<Site> {
var url:string;
var pos = location.hostname.lastIndexOf(this.config.rootDomain);
var url = (pos === -1)
? this.config.apiEndpoint + '/sites?host=' + location.hostname
: this.config.apiEndpoint + '/sites/' + location.hostname.substr(0, pos);
var promise = this.http.get(url).map(res => res.json()).toPromise();
promise.then(site => this.current = site);
return promise;
}
NOTE: config is just a custom config class. rootDomain would be
'.letterpress.com' for this example and would allow things like
aptaincodeman.letterpress.com.
Any components and other services can now have Site injected into
them and use the .current property which will be a concrete
populated object with no need to wait on any promise within the app.
This approach seemed to cut the startup latency which was otherwise
quite noticeable if you were waiting for the large Angular bundle to
load and then another http request before the bootstrap even began.
original
You can pass it using Angulars dependency injection:
var headers = ... // get the headers from the server
bootstrap(AppComponent, [{provide: 'headers', useValue: headers})]);
class SomeComponentOrService {
constructor(#Inject('headers') private headers) {}
}
or provide prepared BaseRequestOptions directly like
class MyRequestOptions extends BaseRequestOptions {
constructor (private headers) {
super();
}
}
var values = ... // get the headers from the server
var headers = new MyRequestOptions(values);
bootstrap(AppComponent, [{provide: BaseRequestOptions, useValue: headers})]);
In Angular2 final release, the APP_INITIALIZER provider can be used to achieve what you want.
I wrote a Gist with a complete example: https://gist.github.com/fernandohu/122e88c3bcd210bbe41c608c36306db9
The gist example is reading from JSON files but can be easily changed to read from a REST endpoint.
What you need, is basically:
a) Set up APP_INITIALIZER in your existent module file:
import { APP_INITIALIZER } from '#angular/core';
import { BackendRequestClass } from './backend.request';
import { HttpModule } from '#angular/http';
...
#NgModule({
imports: [
...
HttpModule
],
...
providers: [
...
...
BackendRequestClass,
{ provide: APP_INITIALIZER, useFactory: (config: BackendRequestClass) => () => config.load(), deps: [BackendRequestClass], multi: true }
],
...
});
These lines will call the load() method from BackendRequestClass class before your application is started.
Make sure you set "HttpModule" in "imports" section if you want to make http calls to the backend using angular2 built in library.
b) Create a class and name the file "backend.request.ts":
import { Inject, Injectable } from '#angular/core';
import { Http } from '#angular/http';
import { Observable } from 'rxjs/Rx';
#Injectable()
export class BackendRequestClass {
private result: Object = null;
constructor(private http: Http) {
}
public getResult() {
return this.result;
}
public load() {
return new Promise((resolve, reject) => {
this.http.get('http://address/of/your/backend/endpoint').map( res => res.json() ).catch((error: any):any => {
reject(false);
return Observable.throw(error.json().error || 'Server error');
}).subscribe( (callResult) => {
this.result = callResult;
resolve(true);
});
});
}
}
c) To read the contents of the backend call, you just need to inject the BackendRequestClass into any class of you choice and call getResult(). Example:
import { BackendRequestClass } from './backend.request';
export class AnyClass {
constructor(private backendRequest: BackendRequestClass) {
// note that BackendRequestClass is injected into a private property of AnyClass
}
anyMethod() {
this.backendRequest.getResult(); // This should return the data you want
}
}
Let me know if this solves your problem.
Instead of having your entry point calling bootstrap itself, you could create and export a function that does the work:
export function doBootstrap(data: any) {
platformBrowserDynamic([{provide: Params, useValue: new Params(data)}])
.bootstrapModule(AppModule)
.catch(err => console.error(err));
}
You could also place this function on the global object, depending on your setup (webpack/SystemJS). It also is AOT-compatible.
This has the added benefit to delay the bootstrap, whenit makes sense. For instance, when you retrieve this user data as an AJAX call after the user fills out a form. Just call the exported bootstrap function with this data.
The only way to do that is to provide these values when defining your providers:
bootstrap(AppComponent, [
provide(RequestOptions, { useFactory: () => {
return new CustomRequestOptions(/* parameters here */);
});
]);
Then you can use these parameters in your CustomRequestOptions class:
export class AppRequestOptions extends BaseRequestOptions {
constructor(parameters) {
this.parameters = parameters;
}
}
If you get these parameters from an AJAX request, you need to bootstrap asynchronously this way:
var appProviders = [ HTTP_PROVIDERS ]
var app = platform(BROWSER_PROVIDERS)
.application([BROWSER_APP_PROVIDERS, appProviders]);
var http = app.injector.get(Http);
http.get('http://.../some path').flatMap((parameters) => {
return app.bootstrap(appComponentType, [
provide(RequestOptions, { useFactory: () => {
return new CustomRequestOptions(/* parameters here */);
}})
]);
}).toPromise();
See this question:
angular2 bootstrap with data from ajax call(s)
Edit
Since you have your data in the HTML you could use the following.
You can import a function and call it with parameters.
Here is a sample of the main module that bootstraps your application:
import {bootstrap} from '...';
import {provide} from '...';
import {AppComponent} from '...';
export function main(params) {
bootstrap(AppComponent, [
provide(RequestOptions, { useFactory: () => {
return new CustomRequestOptions(params);
});
]);
}
Then you can import it from your HTML main page like this:
<script>
var params = {"token": "#User.Token", "xxx": "#User.Yyy"};
System.import('app/main').then((module) => {
module.main(params);
});
</script>
See this question: Pass Constant Values to Angular from _layout.cshtml.