Angular + AOT + Webpack + NgTools - Problem Creating ngFactory

I am trying to configure AOT with ngTools running in my Angular application (using Webpack to load a module) and having an absolute time nightmare. I looked at various resources in the documents here and here , and also read readme ngTools, looked at an example laid down inside the Angular CLI (this application was not created using the Angular CLI, but I thought it would be at least a useful link), found through this resource on Angular + Webpack + Sass + ngTools (I also use SASS, but it compiles with my webpack.config.aot.js - it blocks me from switching to ngc, although at least, as far as I found, this is what I saw in some examples), as well as all the github or postoverflow posts on it that I found in the last few years of the days and no matter what I try to create aot, the following error occurs:

 ERROR in window is not defined

 ERROR in /Users/nataliecoley/development/company-app-name/src/main-aot.ts (2,36): Cannot find module '../aot/src/app/app.module.ngfactory'.

 ERROR in ./src/main-aot.ts

Module not found: Error: cannot resolve '../aot/src/app/app.module.ngfactory' to '/ Users / nataliecoley / development / company-app-name / src' @. / Src / main-aot. ts 2: 0-73

Currently, I am ignoring the first problem (partly because I cannot find anyone else on the Internet that has this error, and partly because I searched my entire code base, excluding node modules and windownot appearing anywhere in the code), but I am open to suggestions if anyone knows where this might come from (or if you think this is due to errors 2 and 3).

The second and third error messages, although I think they are the main source of my problems, as itโ€™s pretty clear that it app.module.tsdoesnโ€™t compile before main-aot.ts, so by the time the main-aot.tscompilation is in progress, it cannot find the file. However, I set it up according to the documentation that I specifically found so that it is already compiled and app.module.ngfactoryready to go when the compiler reaches main-aot.ts.

, - , , , , , . , , , , , ( - ), , AOT + ngTools + webpack + Angular, , .

():

.
+-- config/
|   +-- helpers.js
|   +-- webpack.common.js
|   +-- webpack.dev.js
|   +-- webpack.prod.js
+-- src/
|   +-- app/
|   +-- assets/
|   +-- vendor/
|   +-- index.html
|   +-- main.ts
|   +-- main-aot.ts
|   +-- polyfills.ts
|   +-- tsconfig.json
+-- package.json
+-- server.js
+-- tsconfig-aot.json
+-- webpack.config.aot.js
+-- webpack.config.js (only contains: module.exports = require('./config/webpack.dev.js');)

, aot ( , Angular), "build:aot": "webpack --config webpack.config.aot.js", package.json. , 4.1.0 Angular, @ngtools/webpack v1.3.1 webpack v 2.3.3 typescript v2.3.0

. , aot typescript/angular, , Angular ( v4.1.0 ), , strictnullchecks .. , , AOT, , , - Angular AOT, , .

tsconfig-aot.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "removeComments": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
    "skipLibCheck": true,
    "skipCodeGeneration": false,
    "typeRoots": [
      "../node_modules/@types/"
    ]
 },
  "files": [
    "src/app/app.module.ts",
    "src/main-aot.ts"
  ],
  "compileOnSave": true,
  "angularCompilerOptions": {
    "genDir": "aot",
    "entryModule": "src/app/app.module#AppModule",
    "skipMetadataEmit": true
  }
}

webpack.config.aot.js:

const ngtools = require('@ngtools/webpack');
const webpack = require('webpack');
const helpers = require('./config/helpers');


module.exports = {
  devtool: 'source-map',
  entry: {
    main: './src/main-aot.ts'
  },
  resolve: {
     extensions: ['.ts', '.js']
  },
  target: 'node',
  output: {
    path: helpers.root('dist'),
    filename: 'build.js'
  },
  plugins: [
    new ngtools.AotPlugin({
      tsConfigPath: helpers.root('tsconfig-aot.json'),
      entryModule: helpers.root('src', 'app', 'app.module#AppModule')
    }),
    new webpack.optimize.UglifyJsPlugin({ sourceMap: true })
  ],
  module: {
    rules: [
      {
         test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
         loader: 'file-loader?name=assets/[name].[hash].[ext]'
      },
      { test: /\.css$/, loader: 'raw-loader' },
      {
        test: /\.scss$/,
        loaders: ['to-string-loader', 'style-loader', 'css-loader', 'resolve-url-loader', 'sass-loader?sourceMap']
      },
      { test: /\.html$/, loader: 'raw-loader' },
      { test: /\.ts$/, loader: '@ngtools/webpack' }
    ]
  }
}

entry. , entryModule tsconfig-aot.json, webpack.config.aot.js, , , webpack.config.aot.js. tsconfig-aot.json webpack.config.aot.js - , , . (, tsconfig-aot.json), , .

main-aot.ts:

import { platformBrowser }    from '@angular/platform-browser';
import { AppModuleNgFactory } from '../aot/src/app/app.module.ngfactory';

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

, , , , , , , , , , , -, Angular + Webpack, , . !

+6
2

, :

ngfactory ngc: .. /../path/to/app/app.module.ngfactory. . genDir webpack, tsconfig.js.

, ngtools , AOT Blackbox. ngc (.. ngfactory ) Angular CLI AOT (ngtools).

ngfactories, ngc. ngtools , ngc Angular.

, ngtools , - : https://github.com/angular/angular-cli/issues/5137

?

webpack , , ngc ...

package.json:

"aot": "./node_modules/.bin/ngc -p tsconfig-aot.json",

...

server.ts:

import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { renderModuleFactory } from '@angular/platform-server'
import { enableProdMode } from '@angular/core'
import { AppServerModuleNgFactory } from './dist/ngfactory/src/app/app.server.module.ngfactory'
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';

const PORT = process.env.PORT || 8081;

enableProdMode();

const app = express();

let template = readFileSync(join(__dirname, 'dist', 'index.html')).toString();

app.engine('html', (_, options, callback) => {
  const opts = { document: template, url: options.req.url };

  renderModuleFactory(AppServerModuleNgFactory, opts)
    .then(html => callback(null, html));
});

app.set('view engine', 'html');
app.set('views', 'src')

app.get('*.*', express.static(join(__dirname, 'dist')));

app.get('*', (req, res) => {
  res.render('index', { req });
});

app.listen(PORT, () => {
  console.log(`listening on http://localhost:${PORT}!`);
});
+4

Webpack. , , Angular 5 , Angular 5 AOT Builds, , Production Builds.

Angular, ...

Typescript ( .. tsconfig.json) Angular Builds...

: Angular 4

Angular -CLI video, , Webpack...

, Webpack:

  • Typescript/Angular , Angular , . Angular v4.4 Typscript v2.5.3
  • @ngtools/webpack NPM
  • tsconfig.json AOT Builds...
  • index.ts ( , :()

AOT- Webpack:

  • , Angular -CLI Command Tool ngc -p tsconfig.json
  • -, Angulars Official @ngtools/webpack v ^ 1.7.0. NgTools , Old Builds Angular (.. Angular 4) ngc-webpack, ( , , Angular AOT)...

(I):

Youtube ...

TL;DR; ( )

  • bootstrap.aot.ts : bootstrap.aot.ts
  • tsconfig-aot.json : tsconfig-aot.json
  • ur webpack.config.js AOT, , ... - webpack.prod.js webpack.common.js
  • ur package.json , , , ... package.json

    npm run start:prod:test, , AOT Build... Explaination:

    • ...
    • ur src/ /
    • CSS SASS, ngc, , SASS/LESS, , , ...
    • TEXT '.scss' '.css', ./config/replace-scss.js :
  • *.ngfactory.ts . goto dist/folder, tsconfig.json. ngfactory bootstrap.aot.ts. , , ....

:

/
|---src/                             // Main Source Folder
    |---bootstrap.aot.ts
|---build/                           // Copied files and Sass->Css in here
|---dist/
  1. Webpack typescript , , ngc AOT...

. . .

(II):

, ... *.ngfactory.ts , 5. point

bootstrap.aot.ts, tsconfig-aot.json ( tsconfig )

webpack.prod.ts

package.json

, Angular, ngtools typescript...

"@angular/animations": "~4.4.4",
"@angular/common": "~4.4.4",
"@angular/compiler": "~4.4.4",
"@angular/core": "~4.4.4",
"@angular/forms": "~4.4.4",
"@angular/http": "~4.4.4",
"@angular/platform-browser": "~4.4.4",
"@angular/platform-browser-dynamic": "~4.4.4",
"@angular/router": "~4.4.4",
"core-js": "^2.5.1",
"immutable": "^3.8.2",
"rxjs": "^5.4.3",
"zone.js": "~0.8.18"
"@angular-devkit/build-optimizer": "^0.0.32",
"@angular/cli": "^1.5.5",
"@angular/compiler-cli": "^4.4.4",
"@ngtools/webpack": "^1.7.0",
"@types/jasmine": "2.5.45",
"@types/jquery": "^3.2.15",
"@types/node": "~6.0.60",
"angular2-template-loader": "^0.6.2",
"autoprefixer": "^7.1.4",
"awesome-typescript-loader": "^3.2.3",
"chalk": "^2.3.0",
"clean-webpack-plugin": "^0.1.16",
"codelyzer": "~4.0.1",
"compodoc": "0.0.41",
"concurrently": "^3.5.1",
"copy-webpack-plugin": "^4.0.0",
"cpx": "^1.5.0",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.1",
"file-loader": "^0.11.2",
"html-webpack-plugin": "^2.30.1",
"node-sass": "^4.5.3",
"npm-run-all": "^4.1.1",
"postcss": "^5.2.11",
"postcss-loader": "^1.2.2",
"progress-bar-webpack-plugin": "^1.10.0",
"raw-loader": "^0.5.1",
"replace": "^0.3.0",
"rimraf": "^2.6.2",
"sass-loader": "^6.0.6",
"source-map-loader": "^0.2.1",
"style-loader": "^0.18.2",
"to-string-loader": "^1.1.5",
"ts-node": "~3.0.4",
"tslint": "~5.3.2",
"tslint-loader": "^3.5.3",
"typescript": "~2.5.3",
"uglify-js": "^3.0.28",
"webpack": "^3.8.1",
"webpack-bundle-analyzer": "^2.9.1",
"webpack-combine-loaders": "^2.0.3",
"webpack-dev-server": "^2.9.3",
"webpack-merge": "^4.1.0",
"webpack-notifier": "^1.5.0"

, Production Builds, , webpack.test.js

, , - , , .

: ( , , - , , - , , )

Angular 5

, issue, - . Hack , Production.

, .

u : P

0

Source: https://habr.com/ru/post/1016968/


All Articles