Skip to content

Instantly share code, notes, and snippets.

@colltoaction
Created October 17, 2016 17:30
Show Gist options
  • Save colltoaction/08d1be519b525899969180bdf7edb67f to your computer and use it in GitHub Desktop.
Save colltoaction/08d1be519b525899969180bdf7edb67f to your computer and use it in GitHub Desktop.
Example configuration for Angular 2 AoT and Webpack 2
import "./polyfills";
import "./rxjs-operators";
import { enableProdMode } from "@angular/core";
import { platformBrowser } from "@angular/platform-browser";
import { MainModuleNgFactory } from "./components/main.aot.ngfactory"; // Ignore error
if ((<any>module)["hot"]) {
(<any>module)["hot"].accept();
(<any>module)["hot"].dispose(() => { platform.destroy(); });
}
else {
enableProdMode();
}
// Boot the application, either now or when the DOM content is loaded
const platform = platformBrowser();
const bootApplication = () => { platform.bootstrapModuleFactory(MainModuleNgFactory); };
if (document.readyState === "complete") {
bootApplication();
}
else {
document.addEventListener("DOMContentLoaded", bootApplication);
}
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"lib": [ "es2015", "dom" ],
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipDefaultLibCheck": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"removeComments": true,
// Waiting for comments on https://github.com/angular/angular/pull/8902
"strictNullChecks": false
},
"compileOnSave": false,
"buildOnSave": false,
"files": [
"ClientApp/components/main.aot.ts"
]
}
var isDevBuild = process.argv.indexOf("--env.prod") < 0;
var path = require("path");
const webpack = require("webpack");
const ForkCheckerPlugin = require("awesome-typescript-loader").ForkCheckerPlugin;
var merge = require("webpack-merge");
var ngToolsWebpack = require("@ngtools/webpack");
// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
resolve: {
extensions: [".ts", ".js"]
},
output: {
path: path.join(__dirname, "wwwroot", "dist"),
filename: "[name].js",
publicPath: "/dist/" // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.html$/, include: /ClientApp/, loader: "raw" },
{
test: /\.scss$/,
include: /ClientApp/,
use: [
"to-string-loader",
"css-loader",
"sass-loader"
],
},
{ test: /\.(png|jpg|jpeg|gif|svg)$/, include: /ClientApp/, loader: "url?name=assets/[name].[hash:6].[ext]&limit=25000" }
]
},
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: path.join(__dirname, "wwwroot", "dist", "vendor-manifest.json"),
}),
],
};
var devBundleConfig = {
entry: { "main-client": "./ClientApp/boot-client.ts" },
devtool: "cheap-module-eval-source-map",
module: {
rules: [
{ test: /\.ts$/, include: /ClientApp/, loader: "awesome-typescript!angular2-template" },
]
},
plugins: [
new ForkCheckerPlugin(),
],
};
var prodBundleConfig = {
entry: { "main-client": "./ClientApp/boot-client-aot.ts" },
devtool: false,
module: {
rules: [
{ test: /\.ts$/, loader: "@ngtools/webpack" },
]
},
plugins: [
new ngToolsWebpack.AotPlugin({
tsConfigPath: "./tsconfig.aot.json",
entryModule: path.resolve(__dirname, "ClientApp", "components", "main.aot") + "#MainModule",
typeChecking: true,
}),
new webpack.optimize.UglifyJsPlugin({
mangle: { screw_ie8: true },
compress: {
screw_ie8: true,
warnings: false,
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
],
};
module.exports = [merge(sharedConfig, isDevBuild ? devBundleConfig : prodBundleConfig)];
@mscottreed
Copy link

What is main.aot.ts referenced at the bottom of the tsconfig? Is it the app.component?

@colltoaction
Copy link
Author

@mscottreed it's a file where I keep my main module (an NgModule annotated class). It seems like there's nothing specific to AoT there, so you can use app.module or whatever you have.

And sorry for the delay, I didn't get any notification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment