Angular 9
Hello,
I have a question: Is there already something to test out NativeScript with Angular 9? I am building an app for web, which will go out in December and thinking about to have also mobile app.
Thanks for answer!
Angular 9.1 is out now: https://blog.angular.io/version-9-1-of-angular-now-available-typescript-3-8-faster-builds-and-more-eb292f989428?gi=684bd4297007
@NathanWalker any update to support angular v9?
Any Update to this?
All details to use Angular 9 with NativeScript today can be found here: https://github.com/NativeScript/nativescript-angular/pull/2124#issue-376409005
If anyone encounters issue with instructions provided, please post a public repo here and what you are running into and would be happy to help.
@NathanWalker Thanks for the quick comment!!! I'll give it a shot.
An alternative is switching to a framework that already supports v9 https://twitter.com/LiamDeBeasi/status/1261298258506375169
Hi Guys, I am wondering if the Angular+NativeScript is still in development. I am new to Angular and I liked it a lot, so would be greate if we could use Angular with NativeScript.
Hi @handelcamilo thanks for the comment - the Angular 9 Ivy support in https://github.com/NativeScript/nativescript-angular/pull/2124 will be merged and published by no later than Wednesday next week along with some long awaited goodies like true async APP_INITIALIZER support and some nice support for easy animated launch screens given async launch conditions 👍
Good to know @NathanWalker!! I was afraid because the last change on this project was in February.. This is a very and important project to simply die hheeheh and you are doing a great job! I hope I can help with any time soon. Thanks.
@NathanWalker is the next release going to support the angular 9 i18n system?
@hrueger I haven't tried their i18n commands yet and the configuration specified in that doc but definitely seems like we could make that work (if it doesn't already). I have often used ngx-translate for all i18n on {N} apps as it's a very solid solution but I definitely think the i18n solution the angular team specifies should be able to work.
$localize seems not to be working. I also cannot find anything regarding nativescript and $localize (or Angular 9 in general). When can we expect a solution as this is really blocking us from migrating our app to NativeScript?
Why is the @nativescript/angular package on npm released with version 9.0.0, but not the nativescript-angular package? This one is still 8.21.0...
Why is the
@nativescript/angularpackage on npm released with version9.0.0, but not thenativescript-angularpackage? This one is still8.21.0...
They are the same package. You can switch to @nativescript/angular
@hrueger - @dimitri-bret is correct - nativescript-angular had become just an alias to @nativescript/angular back in the 6.0 release (or maybe earlier?) so you can just use @nativescript/angular now - and in fact all {N} packages will be going fully scoped in 7.0 and you are safe to move to @nativescript/core instead of tns-core-modules right now. If you encounter any issues you can use this webpack.config that adds the aliases to help with any plugins you may use which may still use old style imports (this is a slightly improved webpack.config and will likely soon add some of these adjustments to latest nativescript-dev-webpack plugin - just npm i terser-webpack-plugin --save-dev if you don't have it installed as it's used here 👍 ):
const { join, relative, resolve, sep, dirname } = require('path');
const webpack = require('webpack');
const nsWebpack = require('nativescript-dev-webpack');
const nativescriptTarget = require('nativescript-dev-webpack/nativescript-target');
const {
nsReplaceBootstrap
} = require('nativescript-dev-webpack/transformers/ns-replace-bootstrap');
const {
nsReplaceLazyLoader
} = require('nativescript-dev-webpack/transformers/ns-replace-lazy-loader');
const {
nsSupportHmrNg
} = require('nativescript-dev-webpack/transformers/ns-support-hmr-ng');
const {
getMainModulePath
} = require('nativescript-dev-webpack/utils/ast-utils');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const {
NativeScriptWorkerPlugin
} = require('nativescript-worker-loader/NativeScriptWorkerPlugin');
const TerserPlugin = require('terser-webpack-plugin');
const {
getAngularCompilerPlugin
} = require('nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin');
const hashSalt = Date.now().toString();
module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
"@nativescript/core/ui/frame", "@nativescript/core/ui/frame/activity"
];
const platform = env && ((env.android && 'android') || (env.ios && 'ios'));
if (!platform) {
throw new Error('You need to provide a target platform!');
}
const AngularCompilerPlugin = getAngularCompilerPlugin(platform);
const projectRoot = __dirname;
// Default destination inside platforms/<platform>/...
const dist = resolve(
projectRoot,
nsWebpack.getAppPath(platform, projectRoot)
);
const {
// The 'appPath' and 'appResourcesPath' values are fetched from
// the nsconfig.json configuration file
// when bundling with `tns run android|ios --bundle`.
appPath = 'src',
appResourcesPath = 'App_Resources',
// You can provide the following flags when running 'tns run android|ios'
aot, // --env.aot
snapshot, // --env.snapshot,
production, // --env.production
uglify, // --env.uglify
report, // --env.report
sourceMap, // --env.sourceMap
hiddenSourceMap, // --env.hiddenSourceMap
hmr, // --env.hmr,
unitTesting, // --env.unitTesting
verbose, // --env.verbose
ci, // --env.ci
snapshotInDocker, // --env.snapshotInDocker
skipSnapshotTools, // --env.skipSnapshotTools
compileSnapshot // --env.compileSnapshot
} = env;
const useLibs = compileSnapshot;
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
const externals = nsWebpack.getConvertedExternals(env.externals);
const appFullPath = resolve(projectRoot, appPath);
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
const tsConfigName = 'tsconfig.tns.json';
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
const entryPath = `.${sep}${entryModule}`;
const entries = { bundle: entryPath };
const areCoreModulesExternal =
Array.isArray(env.externals) &&
env.externals.some(e => e.indexOf('@nativescript') > -1);
if (platform === 'ios' && !areCoreModulesExternal) {
entries['tns_modules/@nativescript/core/inspector_modules'] =
'inspector_modules';
}
const ngCompilerTransformers = [];
const additionalLazyModuleResources = [];
if (aot) {
ngCompilerTransformers.push(nsReplaceBootstrap);
}
const copyTargets = [
{ from: { glob: 'assets/**' } },
{ from: { glob: 'fonts/**' } }
];
if (!production) {
// TIP: you can push in additional copyTargets for development purposes only
if (hmr) {
ngCompilerTransformers.push(nsSupportHmrNg);
}
}
// when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
// directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
// fixes https://github.com/NativeScript/nativescript-cli/issues/4024
if (env.externals && env.externals.indexOf('@angular/core') > -1) {
const appModuleRelativePath = getMainModulePath(
resolve(appFullPath, entryModule),
tsConfigName
);
if (appModuleRelativePath) {
const appModuleFolderPath = dirname(
resolve(appFullPath, appModuleRelativePath)
);
// include the lazy loader inside app module
ngCompilerTransformers.push(nsReplaceLazyLoader);
// include the new lazy loader path in the allowed ones
additionalLazyModuleResources.push(appModuleFolderPath);
}
}
const ngCompilerPlugin = new AngularCompilerPlugin({
hostReplacementPaths: nsWebpack.getResolver([platform, 'tns']),
platformTransformers: ngCompilerTransformers.map(t =>
t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)
),
mainPath: join(appFullPath, entryModule),
tsConfigPath: join(__dirname, tsConfigName),
skipCodeGeneration: !aot,
sourceMap: !!isAnySourceMapEnabled,
additionalLazyModuleResources: additionalLazyModuleResources
});
let sourceMapFilename = nsWebpack.getSourceMapFilename(
hiddenSourceMap,
__dirname,
dist
);
const itemsToClean = [`${dist}/**/*`];
if (platform === 'android') {
itemsToClean.push(
`${join(
projectRoot,
'platforms',
'android',
'app',
'src',
'main',
'assets',
'snapshots'
)}`
);
itemsToClean.push(
`${join(
projectRoot,
'platforms',
'android',
'app',
'build',
'configurations',
'nativescript-android-snapshot'
)}`
);
}
nsWebpack.processAppComponents(appComponents, platform);
const config = {
mode: production ? 'production' : 'development',
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
// Don't watch hidden files
'**/.*'
]
},
target: nativescriptTarget,
entry: entries,
output: {
pathinfo: false,
path: dist,
sourceMapFilename,
libraryTarget: 'commonjs2',
filename: '[name].js',
globalObject: 'global',
hashSalt
},
resolve: {
extensions: ['.ts', '.js', '.scss', '.css'],
modules: [
resolve(__dirname, 'node_modules/@nativescript/core'),
resolve(__dirname, 'node_modules'),
'node_modules/@nativescript/core',
'node_modules'
],
alias: {
'~': appFullPath,
"tns-core-modules": "@nativescript/core",
"nativescript-angular": "@nativescript/angular"
},
symlinks: true
},
resolveLoader: {
symlinks: false
},
node: {
// Disable node shims that conflict with NativeScript
http: false,
timers: false,
setImmediate: false,
fs: 'empty',
__dirname: false
},
devtool: hiddenSourceMap
? 'hidden-source-map'
: sourceMap
? 'inline-source-map'
: 'none',
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
name: 'vendor',
chunks: 'all',
test: (module, chunks) => {
const moduleName = module.nameForCondition
? module.nameForCondition()
: '';
return (
/[\\/]node_modules[\\/]/.test(moduleName) ||
appComponents.some(comp => comp === moduleName)
);
},
enforce: true
}
}
},
minimize: !!uglify,
minimizer: [
new TerserPlugin({
parallel: true,
cache: !ci,
sourceMap: isAnySourceMapEnabled,
terserOptions: {
output: {
comments: false,
semicolons: !isAnySourceMapEnabled
},
compress: {
// The Android SBG has problems parsing the output
// when these options are enabled
collapse_vars: platform !== 'android',
sequences: platform !== 'android'
}
}
})
]
},
module: {
rules: [
{
include: join(appFullPath, entryPath),
use: [
// Require all Android app components
platform === 'android' && {
loader: 'nativescript-dev-webpack/android-app-components-loader',
options: { modules: appComponents }
},
{
loader: 'nativescript-dev-webpack/bundle-config-loader',
options: {
angular: true,
loadCss: !snapshot, // load the application css if in debug mode
unitTesting,
appFullPath,
projectRoot,
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
}
}
].filter(loader => !!loader)
},
{ test: /\.html$|\.xml$/, use: 'raw-loader' },
// tns-core-modules reads the app.css and its imports using css-loader
{
test: /[\/|\\]app\.css$/,
use: [
'nativescript-dev-webpack/style-hot-loader',
{ loader: 'css-loader', options: { url: false } }
]
},
{
test: /[\/|\\]app\.scss$/,
use: [
'nativescript-dev-webpack/style-hot-loader',
{ loader: 'css-loader', options: { url: false } },
'sass-loader'
]
},
// Angular components reference css files and their imports using raw-loader
{ test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: 'raw-loader' },
{
test: /\.scss$/,
exclude: /[\/|\\]app\.scss$/,
use: ['raw-loader', 'resolve-url-loader', 'sass-loader']
},
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
use: [
'nativescript-dev-webpack/moduleid-compat-loader',
'nativescript-dev-webpack/lazy-ngmodule-hot-loader',
'@ngtools/webpack'
]
},
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
// Removing this will cause deprecation warnings to appear.
{
test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
parser: { system: true }
}
]
},
plugins: [
// Define useful constants like TNS_WEBPACK
new webpack.DefinePlugin({
'global.TNS_WEBPACK': 'true',
process: 'global.process'
}),
// Remove all files from the out dir.
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
// Copy assets to out dir. Add your own globs as needed.
new CopyWebpackPlugin(copyTargets, {
ignore: [`${relative(appPath, appResourcesFullPath)}/**`]
}),
new nsWebpack.GenerateNativeScriptEntryPointsPlugin('bundle'),
// For instructions on how to set up workers with webpack
// check out https://github.com/nativescript/worker-loader
new NativeScriptWorkerPlugin(),
ngCompilerPlugin,
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
new nsWebpack.WatchStateLoggerPlugin()
]
};
if (report) {
// Generate report files for bundles content
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
generateStatsFile: true,
reportFilename: resolve(projectRoot, 'report', `report.html`),
statsFilename: resolve(projectRoot, 'report', `stats.json`)
})
);
}
if (!production && hmr) {
config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
return config;
};
@NathanWalker Thanks, unfortunately I still have issue #2171 so I can't confirm that it's working.
@hrueger that issue is covered in the wiki here - just got you details in that issue 👍