- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新
@developer033 提到后,我正在运行 npm run server:prod 并且应用程序正在 prod 模式下运行。但我不确定,如何验证它是否经过 AOT 优化?
如果我检查 main.bundle.js 以检查它是否有 main.browser.aot.ts 的代码:
我看到是 main.browser.ts 而不是 main.browser.aot.ts所以我不确定我是否在使用 AOT 构建。
原帖
好的,我可以使用 Webpack 在 AOT 模式下编译我的 Angular2 项目,它创建了两个文件夹,一个是“dist”,另一个是“compiled”,但我不确定编译后如何运行 AOT 项目。
构建命令:
"build:aot:prod": "npm run clean:dist && npm run clean:aot && webpack --config config/webpack.prod.js --progress --profile --bail"
编译后,我运行命令:
npm run webpack-dev-server
现在应用程序运行了,但我不确定它是否使用 AOT 编译代码。我在浏览器中看到 AOT 和非 AOT 应用程序没有区别。
tsconfig.webpack.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"baseUrl": "./src",
"outDir": "compiled",
"paths": {},
"lib": [
"es2015",
"dom"
],
"typeRoots": [
"node_modules/@types"
]
},
"exclude": [
"node_modules",
"dist",
"compiled",
"src/**/*.spec.ts",
"src/**/*.e2e.ts"
],
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
"angularCompilerOptions": {
"genDir": "compiled",
"skipMetadataEmit": true
},
"compileOnSave": false,
"buildOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
main.browser.aot.ts:
import { platformBrowser } from '@angular/platform-browser';
import { decorateModuleRef } from './app/environment';
import { enableProdMode } from '@angular/core';
import { AppModuleNgFactory } from '../compiled/src/app/app.module.ngfactory';
if (ENV.toLocaleLowerCase() === 'production' || ENV.toLocaleLowerCase() === 'prod') {
enableProdMode();
}
export function main(): Promise<any> {
return platformBrowser()
.bootstrapModuleFactory(AppModuleNgFactory)
.then(decorateModuleRef)
.catch(err => console.error(err));
}
export function bootstrapDomReady() {
document.addEventListener('DOMContentLoaded', main);
}
bootstrapDomReady();
config/webpack.common.js:
const HMR = helpers.hasProcessFlag('hot');
const AOT = helpers.hasNpmFlag('aot');
const METADATA = {
title: 'Angular2 Webpack Starter by @gdi2290 from @AngularClass',
baseUrl: '/',
isDevServer: helpers.isWebpackDevServer()
};
module.exports = function(options) {
isProd = options.env === 'production';
return {
entry: {
'polyfills': './src/polyfills.browser.ts',
'vendor': './src/vendor.browser.ts',
'main': AOT ? './src/main.browser.aot.ts' : './src/main.browser.ts'
},
resolve: { extensions: ['.ts', '.js', '.json', '.css', '.scss'], modules: [helpers.root('src'), 'node_modules'], },
module: {
rules: [
{
test: /\.ts$/,
loaders: [
'@angularclass/hmr-loader?pretty=' + !isProd + '&prod=' + isProd,
'awesome-typescript-loader',
{ loader: 'ng-router-loader', options: { loader: 'async-import', genDir: 'compiled', aot: AOT } },
'angular2-template-loader'
],
exclude: [/\.(spec|e2e)\.ts$/]
},
{
test: /\.scss$/,
exclude: /node_modules/,
loaders: ['raw-loader', 'sass-loader'] // sass-loader not scss-loader
},{ test: /\.json$/, loader: 'json-loader'},
{ test: /\.woff(2)?(\?v=.+)?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
{ test: /\.(ttf|eot|svg)(\?v=.+)?$/, use: 'file-loader' },
{ test: /bootstrap\/dist\/js\/umd\//, use: 'imports-loader?jQuery=jquery' },
{ test: /\.html$/, loader: 'raw-loader', exclude: [helpers.root('src/index.html')] },
{ test: /\.(jpg|png|gif)$/, loader: 'file' },
],
},
plugins: [
new ngcWebpack.NgcWebpackPlugin({
disabled: !AOT,
tsConfig: helpers.root('tsconfig.webpack.json'),
resourceOverride: helpers.root('config/resource-override.js')
}),
new ExtractTextPlugin({ filename: 'initial.css', allChunks: true }),
new AssetsPlugin({ path: helpers.root('dist'), filename: 'webpack-assets.json', prettyPrint: true }),
new CheckerPlugin(),
new CommonsChunkPlugin({ name: 'polyfills', chunks: ['polyfills'] }),
new CommonsChunkPlugin({ name: 'vendor', chunks: ['main'], minChunks: module => /node_modules/.test(module.resource) }),
new CommonsChunkPlugin({ name: ['polyfills', 'vendor'].reverse() }),
new ContextReplacementPlugin( /angular(\\|\/)core(\\|\/)@angular/, helpers.root('src') ),
new CopyWebpackPlugin([{ from: 'src/assets', to: 'assets', }, { from: 'src/meta', }, ]),
new HtmlWebpackPlugin({ template: 'src/index.html', title: METADATA.title, chunksSortMode: 'dependency', metadata: METADATA, inject: 'head',
//csp: "default-src 'self'; child-src 'none'; object-src 'none'"
}),
new ScriptExtHtmlWebpackPlugin({ defaultAttribute: 'defer' }),
new HtmlElementsPlugin({ headTags: require('./head-config.common') }),
new LoaderOptionsPlugin({}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
}),
// Fix Angular 2
new NormalModuleReplacementPlugin( /facade(\\|\/)async/, helpers.root('node_modules/@angular/core/src/facade/async.js') ),
new NormalModuleReplacementPlugin( /facade(\\|\/)collection/, helpers.root('node_modules/@angular/core/src/facade/collection.js') ),
new NormalModuleReplacementPlugin( /facade(\\|\/)errors/, helpers.root('node_modules/@angular/core/src/facade/errors.js') ),
new NormalModuleReplacementPlugin( /facade(\\|\/)lang/, helpers.root('node_modules/@angular/core/src/facade/lang.js') ),
new NormalModuleReplacementPlugin( /facade(\\|\/)math/, helpers.root('node_modules/@angular/core/src/facade/math.js') ),
],
node: { global: true, crypto: 'empty', process: true, module: false, clearImmediate: false, setImmediate: false }
};
}
config/webpack.prod.js:
const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
const API_HOST = process.env.HOST || 'localhost';
const API_PORT = process.env.PORT || 8080;
const METADATA = webpackMerge(commonConfig({ env: ENV }).metadata, { API_HOST: API_HOST, API_PORT: API_PORT,
ENV: ENV, HMR: false
});
module.exports = function(env) {
return webpackMerge(commonConfig({ env: ENV }), {
devtool: 'source-map',
output: {
path: helpers.root('dist'),
filename: '[name].[chunkhash].bundle.js',
sourceMapFilename: '[name].[chunkhash].bundle.map',
chunkFilename: '[id].[chunkhash].chunk.js'
},
plugins: [
new WebpackMd5Hash(),
new DefinePlugin({'API_HOST': JSON.stringify(API_HOST), 'API_PORT': JSON.stringify(API_PORT),
'ENV': JSON.stringify(METADATA.ENV), 'NODE_ENV': JSON.stringify(METADATA.ENV), 'HMR': METADATA.HMR,
'process.env': {
'API_HOST': JSON.stringify(API_HOST), 'API_PORT': JSON.stringify(API_PORT),
'ENV': JSON.stringify(METADATA.ENV), 'NODE_ENV': JSON.stringify(METADATA.ENV), 'HMR': METADATA.HMR,
}
}),
new UglifyJsPlugin({
beautify: false, //prod
mangle: { screw_ie8: true, keep_fnames: true }, //prod
compress: { screw_ie8: true }, //prod
comments: false //prod
}),
new NormalModuleReplacementPlugin( /angular2-hmr/, helpers.root('config/modules/angular2-hmr-prod.js') ),
new LoaderOptionsPlugin({
debug: false,
options: {
tslint: { emitErrors: true, failOnHint: true, resourcePath: 'src' },
htmlLoader: { minimize: true, removeAttributeQuotes: false, caseSensitive: true,
customAttrSurround: [ [/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/] ],
customAttrAssign: [/\)?\]?=/]
},
}
}),
],
node: { global: true, crypto: 'empty', process: false, module: false, clearImmediate: false, setImmediate: false }
});
}
Package.json:
{
"name": "angular2-webpack-starter",
"version": "5.1.1",
"description": "An Angular 2 Webpack Starter kit featuring Angular 2 (Router, Http, Forms, Services, Tests, E2E, Coverage), Karma, Protractor, Jasmine, Istanbul, TypeScript, and Webpack by AngularClass",
"keywords": [
"angular2",
"webpack",
"typescript"
],
"author": "Patrick Stapleton <patrick@angularclass.com>",
"homepage": "https://github.com/angularclass/angular2-webpack-starter",
"license": "MIT",
"scripts": {
"build:dev": "webpack --config config/webpack.dev.js --progress --profile",
"build:docker": "npm run build:prod && docker build -t angular2-webpack-start:latest .",
"build:prod": "webpack --config config/webpack.prod.js --progress --profile --bail",
"build": "npm run build:dev",
"ci": "npm run lint && npm test && npm run e2e",
"clean:dist": "npm run rimraf -- dist",
"clean:install": "npm set progress=false && npm install",
"clean:start": "npm start",
"clean": "npm cache clean && npm run rimraf -- node_modules dist",
"clean:aot": "npm run rimraf -- compiled",
"docker": "docker",
"docs": "npm run typedoc -- --options typedoc.json --exclude '**/*.spec.ts' ./src/",
"e2e:live": "npm run e2e -- --elementExplorer",
"e2e": "npm run protractor",
"github-deploy:dev": "webpack --config config/webpack.github-deploy.js --progress --profile --github-dev",
"github-deploy:prod": "webpack --config config/webpack.github-deploy.js --progress --profile --github-prod",
"github-deploy": "npm run github-deploy:dev",
"lint": "npm run tslint \"src/**/*.ts\"",
"postversion": "git push && git push --tags",
"prebuild:dev": "npm run clean:dist",
"prebuild:prod": "npm run clean:dist",
"preclean:install": "npm run clean",
"preclean:start": "npm run clean",
"pree2e": "npm run webdriver:update -- --standalone",
"preversion": "npm test",
"protractor": "protractor",
"rimraf": "rimraf",
"server:dev:hmr": "npm run server:dev -- --inline --hot",
"server:dev": "webpack-dev-server --config config/webpack.dev.js --progress --profile --watch --content-base src/",
"server:prod": "http-server dist --cors",
"server": "npm run server:dev",
"start:hmr": "npm run server:dev:hmr",
"start": "npm run server:dev",
"test": "karma start",
"tslint": "tslint",
"typedoc": "typedoc",
"version": "npm run build",
"watch:dev:hmr": "npm run watch:dev -- --hot",
"watch:dev": "npm run build:dev -- --watch",
"watch:prod": "npm run build:prod -- --watch",
"watch:test": "npm run test -- --auto-watch --no-single-run",
"watch": "npm run watch:dev",
"build:aot:prod": "npm run clean:dist && npm run clean:aot && webpack --config config/webpack.prod.js --progress --profile --bail",
"webdriver-manager": "webdriver-manager",
"webdriver:start": "npm run webdriver-manager start",
"webdriver:update": "npm run webdriver-manager update",
"webpack-dev-server": "webpack-dev-server",
"webpack": "webpack"
},
"dependencies": {
"@angular/animations": "^4.1.3",
"@angular/common": "4.1.3",
"@angular/compiler": "4.1.3",
"@angular/core": "4.1.3",
"@angular/forms": "^4.1.3",
"@angular/http": "4.1.3",
"@angular/platform-browser": "4.1.3",
"@angular/platform-browser-dynamic": "4.1.3",
"@angular/platform-server": "4.1.3",
"@angular/router": "4.1.3",
"@angularclass/conventions-loader": "^1.0.2",
"@angularclass/hmr": "~1.2.0",
"@angularclass/hmr-loader": "~3.0.2",
"animate.css": "^3.5.2",
"assets-webpack-plugin": "^3.4.0",
"bootstrap": "^4.0.0-alpha.6",
"bootstrap-loader": "^2.1.0",
"bootstrap-sass": "^3.3.7",
"core-js": "^2.4.1",
"error-stack-parser": "^2.0.1",
"extract-text-webpack-plugin": "^2.1.0",
"font-awesome": "^4.7.0",
"font-awesome-sass-loader": "^2.0.1",
"http-server": "^0.10.0",
"ie-shim": "^0.1.0",
"ionicons": "^3.0.0",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"ng-snotify": "^1.1.6",
"ngc-webpack": "^2.0.0",
"normalize.css": "^7.0.0",
"primeng": "^4.0.1",
"resolve-url-loader": "^2.0.2",
"rxjs": "^5.0.0-rc.1",
"sourcemapped-stacktrace": "^1.1.6",
"stacktrace-gps": "^3.0.1",
"stacktrace-js": "^2.0.0",
"zone.js": "~0.8.11"
},
"devDependencies": {
"@angular/cli": "^1.1.0",
"@angular/compiler-cli": "^4.1.3",
"@ngtools/webpack": "^1.4.1",
"@types/hammerjs": "^2.0.33",
"@types/jasmine": "^2.2.34",
"@types/node": "^7.0.22",
"@types/protractor": "^4.0.0",
"@types/selenium-webdriver": "3.0.4",
"@types/source-map": "^0.5.0",
"@types/uglify-js": "^2.0.27",
"@types/webpack": "^2.2.15",
"angular2-template-loader": "^0.6.0",
"awesome-typescript-loader": "^3.1.3",
"codelyzer": "~3.0.1",
"copy-webpack-plugin": "^4.0.0",
"css-loader": "^0.28.4",
"exports-loader": "^0.6.4",
"expose-loader": "^0.7.1",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"gh-pages": "^1.0.0",
"html-webpack-plugin": "^2.21.0",
"imports-loader": "^0.7.1",
"istanbul-instrumenter-loader": "^2.0.0",
"json-loader": "^0.5.4",
"karma": "^1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-mocha-reporter": "^2.0.0",
"karma-remap-coverage": "^0.1.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "2.0.3",
"ng-router-loader": "^2.1.0",
"node-sass": "^4.5.3",
"parse5": "^3.0.2",
"postcss-loader": "^2.0.5",
"protractor": "^5.1.2",
"raw-loader": "0.5.1",
"resolve-url-loader": "^2.0.2",
"rimraf": "^2.5.2",
"sass-loader": "^6.0.5",
"script-ext-html-webpack-plugin": "^1.3.2",
"source-map-loader": "^0.2.1",
"string-replace-loader": "1.2.0",
"style-loader": "^0.18.1",
"to-string-loader": "^1.1.4",
"ts-helpers": "1.1.2",
"ts-node": "^3.0.4",
"tslint": "^5.3.2",
"tslint-loader": "^3.5.3",
"typedoc": "^0.7.1",
"typescript": "2.3.4",
"url-loader": "^0.5.8",
"webpack": "^2.1.0-beta.25",
"webpack-dev-middleware": "^1.6.1",
"webpack-dev-server": "^2.1.0-beta.9",
"webpack-md5-hash": "^0.0.5",
"webpack-merge": "^4.1.0"
},
"repository": {
"type": "git",
"url": "https://github.com/angularclass/angular2-webpack-starter.git"
},
"bugs": {
"url": "https://github.com/angularclass/angular2-webpack-starter/issues"
},
"engines": {
"node": ">= 4.2.1",
"npm": ">= 3"
}
}
最佳答案
更新:
正如@developer033 所指出的,ng2-admin 最近更新为 Angular-cli 而不是 angular2-webpack-starter。
原答案:
发布完整答案
运行以下两个命令来构建和运行服务器(如@developer033 所述):
npm run build:prod (npm run clean:dist && npm run clean:aot && webpack --config config/webpack.prod.js --progress --profile --bail)
npm run server:prod
在我的例子中,我在不同的地址同时运行 JIT 和 AOT 构建:
http://127.0.0.1:8080是JIT,其余是AOT
要验证您的构建是否为 AOT,请按照@Maximus 所述检查您的主 bundle 文件。您也可以暂时禁用 UglifyJsPlugin 优化代码,只是为了检查主包 js。
最后,我在 AOT 构建中遇到了以下错误:
"Uncaught TypeError: Cannot read property 'setGlobalVar' of null"
为了解决这个问题,我简单地评论了 disableDebugTools();在我的 environment.ts 文件中。
注意:
我用 angular2-webpack-starter 创建了项目模板对于 AOT,我提到了 ng2-admin
关于javascript - 如何使用 Webpack 运行 Angular 2 AOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44469206/
我正在尝试从 Unity 发布到我的 iPhone 4s。构建成功,但我继续收到此消息: “在 aot-only 模式下运行时无法加载 AOT 模块‘Assembly-CSharp’,因为依赖项无法找
在听说了这么多有关 Angular AOT 模式的信息后,我想尝试一下。因此,我们将应用程序迁移到 Angular v4.1.2,并进行了所有必要的更改。当我尝试使用我的 webpack 配置文件中的
我有一个 C# 项目,我需要将其编译为 native 二进制文件。我在这里读过: http://www.mono-project.com/AOT Mono 可以将程序集预编译为两个选项之一: --ao
我搜索了很多关于这种伟大语言的信息,并在此页面上找到了自己 https://github.com/dart-lang/sdk/wiki/Snapshots但它没有回答一些问题: 快照和 AOT 有什么
我知道什么是字节码检测。它只是在运行时更改 .class 文件字节码,这似乎从 JDK 1.5 开始可用。但是,据说是在类加载期间而不是运行时。 现在我的问题是,什么是 AOT 或提前检测?相反的程序
我正在为我的应用程序构建一个插件系统。我读到任何人都可以反编译 .class 文件,因此我不得不使用 Ahead-Of-Time 编译器(对吗?)。问题是我需要动态加载一些插件类。现在,我正在将所有
我的问题是 Angular 模块之一无法在 Safari 浏览器中正常工作。 我有以下代码: @NgModule({ bootstrap: [ AppComponent ], impo
我在这里问是因为我无法在网上找到完整的文档。示例 here太简单了。我有一个包含多个组件和一些模块的应用程序。用ngc编译后,我有很多错误。我发现修复它们的方法是使用相对路径。因此,我在所有组件中都使
在使用 webpack 进行 AOT 构建后,bundle 将每个组件模板包含两次,使得 bundle 比需要的大得多,因为模板作为字符串包含在内,UglifyJS 无法缩小。 我希望组件模板被排除为
我正在尝试使用 ngc 编译 angular 2.4.4: Error: Error encountered resolving symbol values statically. Expressio
当我这样做时,有什么原因(我可以解决): ngc -p tsconfig.aot.json 作为回应,它要求: 姓名: ngc 似乎忽略了我的 tsconfig.aot.json。 这可能是什么原因?
我正在开发一个 Angular 7 应用程序,除了加载问题外它工作正常,在研究中我发现我应该使用 AOT 构建而不是 JIT 或正常构建,由于应用程序规模巨大,我无法进行 aot 构建,它正在返回堆我
我在 Dynamics AX 2012 中有一个 AOT 查询,它由 HcmWorker 和 HcmEmployment 表(等等)组成。我的问题是,当我创建报告(不是 SSRS)或以其他方式使用此查
有谁知道在使用带有 AOT 编译的 Angular CLI 时如何获得更详细的错误堆栈跟踪?使用 JIT 编译运行时,我的应用程序没有问题,但是当我运行 ng build --aot 时,我得到的只是
我想查询 AOT 以查看是否存在使用 X++ 的表名。谁能指出我正确的方向或提供一些示例代码来做到这一点? 如果表存在并提供名称 (str tableName),则返回 true;否则,返回false
我有一个 Angular 5 应用程序,当我在测试模式下运行时没有问题,也就是说,当我使用 ng serve 命令在本地运行应用程序时。但是,当我在生产模式下部署应用程序时,即通过命令ng build
语境 我正在创建一个 Angular 应用程序的桌面版本,为此我正在使用 Electron 。 打开主窗口非常简单: win = new BrowserWindow({ width: 1280
我有一个看起来像这样的错误处理程序: @Injectable() export class GlobalErrorHandler implements ErrorHandler { construct
当我尝试构建启用了 AOT 选项的 Android 应用程序时,收到以下错误消息。 Invalid command line switch for "aapt.exe". VersionCode is
我在我的 Angular 2 应用程序中使用 Highcharts 和 Kendo Charts,当我尝试运行 AOT 编译时,它会抛出类似的错误 Cannot Import Module 或 Hom
我是一名优秀的程序员,十分优秀!