I'm trying to write a web application with ExpressJS and Angular2. Zone.js appears to be trying (unsuccessfully) to load some #angular libraries, but I'm not sure how to approach fixing it.
My process so far looks like this:
Copied into my build/node_modules folder:
core-js/client/shim.min.js
zone.js/dist/zone.js
reflect-metadata/Reflect.js
systemjs/dist/system.src.js
Map /scripts URLs to node_modules folder in ExpressJS
On my index page, source the above libraries in script tags and load SystemJS:
System.config({
packages: {
app: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
},
map: {
'rxjs': '/scripts/rxjs',
'#angular': '/scripts/#angular'
}
})
System.import('/app/bootstrap')
.then(null, console.error.bind(console))
And finally, my bootstrap.ts file:
import { bootstrap } from '#angular/platform-browser-dynamic'
import { Component } from '#angular/core'
#Component({
selector: 'testing-ang2',
template: '<span>WIP</span>'
})
export class Ang2TestComponent {}
bootstrap(Ang2TestComponent)
However, when I run this, I get the following errors:
zone.js:101 GET http://localhost:3000/scripts/#angular/platform-browser-dynamic/ 404 (Not Found)
zone.js:323 Error: (SystemJS) Error: XHR error (404 Not Found) loading http://localhost:3000/scripts/#angular/platform-browser-dynamic
zone.js:101 GET http://localhost:3000/scripts/#angular/core/ 404 (Not Found)
I've tried copying the #angular library into my build/node_modules and adding the index.js file from each of the folders listed into <script> tags on my main page, but that doesn't make any difference.
Could anyone suggest a way to fix this?
There was two major issues with the configuration above: most of the #angular libraries weren't copied across and SystemJS didn't know how to find them.
To fix the first, copy the entire #angular and rxjs directories into the build/node_modules directory.
For the second, SystemJS needs to be configured like so:
From an issue opened for Angular2 on github:
var map = {
'#angular': 'node_modules/#angular',
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
'rxjs': 'node_modules/rxjs'
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
};
var ngPackageNames = [ // <-----
'common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'router-deprecated',
'upgrade',
];
// Individual files (~300 requests):
function packIndex(pkgName) {
packages['#angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
}
// Add package entries for angular packages
ngPackageNames.forEach(packIndex);
var config = {
map: map,
packages: packages
}
System.config(config);`
Related
I have just created a new CRA app. In our organization we have a micro frontend framework which has certain requirements when it comes to the the asset file of each micro frontend app. CRA will by default, create a asset-manifest.json file.
https://github.com/facebook/create-react-app/blob/main/packages/react-scripts/config/webpack.config.js#L656
Now I need to change this file to assets.json and make some structural changes as well. To achieve this I use CRACO and add the WebpackManifestPlugin.
const ManifestPlugin = require('webpack-manifest-plugin');
module.exports = {
webpack: {
plugins: {
// tried removing CRA definition for ManifestPlugin.
// It worked, but had no impact on my problem
// remove: ['ManifestPlugin'],
add: [
new ManifestPlugin({
fileName: 'assets.json',
generate: (seed, files, entrypoints) => {
const js = [],
css = [];
files.forEach((file) => {
if (file.path.endsWith('.js') && file.isInitial) {
js.push({ value: file.path, type: 'entry' });
}
if (file.path.endsWith('.css') && file.isInitial) {
css.push({ value: file.path, type: 'entry' });
}
});
return { js, css };
},
})
]
}
}
};
Whenever I build the application, my new assets.json file is generated as expected.
However, I can't get CRA, or webpack-dev-server I assume, to serve this file while I run my CRA app in development mode. It only resolves to the index.html file. I have looked through CRA source code and can't really find any relevant place where asset-manifest.json is mentioned.
So how do I get webpack-dev-server to serve my assets.json file?
You need to add the ManifestPlugin to webpack.plugins.remove array to receive only the configuration from WebpackManifestPlugin:
...
webpack: {
alias: {},
plugins: {
add: [
new WebpackManifestPlugin(webpackManifestConfig)
],
remove: [
"ManifestPlugin"
],
},
configure: (webpackConfig, { env, paths }) => { return webpackConfig; }
},
...
I have been using aurelia Framework and the aurelia-i18n plugin for my translations. Everything works fine in local dev but when I build my app (npm run build) the locales folders are not included.
Here is my main.js plugin setup:
.plugin(PLATFORM.moduleName('aurelia-i18n'), (instance) => {
let aliases = ['t', 'i18n'];
TCustomAttribute.configureAliases(aliases);
instance.i18next.use(Backend);
return instance.setup({
fallbackLng: 'en',
whitelist: ['en', 'es'],
preload: ['en', 'es'],
ns: 'translation',
attributes: aliases,
lng: 'en',
debug: true,
backend: {
loadPath: 'src/locales/{{lng}}/{{ns}}.json',
}
});
})
In webpack module rules I have this:
{
test: /\.json$/,
loader: 'json', // I have also tried json-loader
include: ['/locales/en', '/locales/es'],
},
Now, if I include the lang file in the webpack module dependencies the files do appear in the dist folder but are not accessible by the aurelia-i18n plugin as it is looking in the locales folder to find the files.
new ModuleDependenciesPlugin(
{
"aurelia-i18n": [
{ name: "locales/en/translation.json", chunk: "lang-en" },
{ name: "locales/es/translation.json", chunk: "lang-es" }
]
})
Any ideas?
Thank you in advance!
For production build we just copy the locales folder to dist.
This is how the webpack.config.js looks like
new CopyWebpackPlugin([
{
from: path.join(staticDir, 'locales'),
to: path.join(outDir, 'locales')
}
])
In the root of the project we have a folder called static that contains the locales folder with the translations.
And this is the function we use in main.js to configure i18n.
export function configureI18N(aurelia: Aurelia): void {
aurelia.use.plugin(PLATFORM.moduleName('aurelia-i18n'), (instance?: any) => {
// register backend plugin
instance.i18next.use(Backend);
const aliases = ['localization-key', 't', 'i18n'];
// add aliases for 't' attribute
TCustomAttribute.configureAliases(aliases);
TParamsCustomAttribute.configureAliases(['localization-key-value-params']);
// adapt options to your needs (see http://i18next.com/docs/options/)
// make sure to return the promise of the setup method, in order to guarantee proper loading
return instance.setup({
backend: { // <-- configure backend settings
loadPath: '../locales/{{lng}}.{{ns}}.json' // <-- XHR settings for where to get the files from
},
lng: 'en',
attributes: aliases,
fallbackLng: 'en',
lowerCaseLng: true
});
});
}
I am using ng2-daterangepicker but while importing import { Daterangepicker } from 'ng2-daterangepicker'; getting error while building as No such file or directory found on project root.
Did you update your system.js file.
map: {
// angular bundles
// add ng2-daterangepicker bundles
'ng2-daterangepicker': 'npm:ng2-daterangepicker',
'jquery': 'npm:jquery/dist/jquery.js',
'moment': 'npm:moment',
'bootstrap-daterangepicker': 'npm:bootstrap-daterangepicker/daterangepicker.js'
},
packages: {
// other packages already defined
// ng2-daterangepicker packages
moment: {
main: 'moment',
defaultExtension: 'js'
},
'ng2-daterangepicker': {
main: 'index',
defaultExtension: 'js'
}
}
Check your app.module as well.
import { Daterangepicker } from 'ng2-daterangepicker';
#NgModule({
imports: [Daterangepicker]
})
I have an Angular2 and Express based web app. I am trying to implement a secured API endpoint, and am cheating and using Auth0. I am following the guide, however, it doesn't seem to be getting on with my setup. My app is currently crashing with:
GET /angular2-jwt 404 30.433 ms - 14
Despite me having this system.config.js:
(function (global) {
System.config({
paths: {
'npm:': 'node_modules/'
},
map: {
app: 'app',
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
//all the other angular packages
'angular2-jwt': 'node_modules/angular2-jwt/angular2-jwt.js',
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api'
},
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
main: 'Rx.js',
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'angular2-jwt': { defaultExtension: 'js' }
}
});
})(this);
Why isn't my system.config.js working, or have I missunderstood how to use systemJS?
My index.html contains:
<script src="./config/systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
if I add a console.log message directly above the System.config({ line of my system.config, it isn't triggered. Does this mean my system.config isn't being read?
You have a typo in system.config.js:
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api'
},no extension
packages: {
When I remove no extension, it requests as expected
GET /node_modules/angular2-jwt/angular2-jwt.js
if I add a console.log message directly above the System.config({ line of my system.config, it isn't triggered. Does this mean my system.config isn't being read?
Most likely, yes. You can check it by looking at the network activity tab in developer tools in your browser - there should be a request that fetches systemjs.config.js.
You can find an example with working import at https://github.com/fictitious/test-systemjs-angular (it does not do any authentication however).
i got an error "angular2-polyfills.js:126 GET http://localhost:3000/ng2-uploader/ng2-uploader 404 (Not Found)" in image upload in angularjs 2. but the class is in there in my folder structure. how i solve it?
I think that your SystemJS configuration isn't correct. You could try the following:
<script>
System.config({
map: {
'ng2-uploader': 'node_modules/ng2-uploader'
},
packages: {
(...)
'ng2-uploader': {
format: 'register',
defaultExtension: 'js'
}
}
});
(...)
</script>