Mus*_*adi 16 webpack webpack-style-loader angular-upgrade angular angular8
我们有一个Angular 4我最近尝试升级到的项目Angular 8。我遵循了Angular网站上的迁移指南,更改了某些语法,并且阻止了项目的构建。我遇到了我的样式无法在本地正确加载并且根本不在Azure上加载的问题。我找到Webpack并尝试配置它,添加了style-loader,sass-loader和css-loader。现在,当我在本地运行时,可以在浏览器的“网络”选项卡中看到所有样式均已正确加载,但它仅加载默认的index.html文件,而没有引导应用程序。
当我在Azure上部署应用程序时,它会加载其他组件,但是没有一种样式会加载。我花了3周的时间进行迁移,但仍然没有希望。
工具/插件版本:
Angular CLI: 8.3.5
Node: 10.15.1
OS: win32 x64
Angular: 8.2.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.803.5
@angular-devkit/build-angular 0.803.5
@angular-devkit/build-optimizer 0.803.5
@angular-devkit/build-webpack 0.803.5
@angular-devkit/core 8.3.5
@angular-devkit/schematics 8.3.5
@angular/cdk 8.2.0
@angular/cli 8.3.5
@angular/http 5.2.11
@angular/material 8.2.0
@ngtools/webpack 8.3.5
@schematics/angular 8.3.5
@schematics/update 0.803.5
rxjs 6.5.3
typescript 3.5.3
webpack 4.40.2Run Code Online (Sandbox Code Playgroud)
这是我的Webpack配置:
const { resolve } = require('path');
const rxPaths = require('rxjs/_esm5/path-mapping');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const { AngularCompilerPlugin } = require('@ngtools/webpack');
const { IndexHtmlWebpackPlugin } = require('@angular-devkit/build-angular/src/angular-cli-files/plugins/index-html-webpack-plugin');
module.exports = {
mode: 'development',
devtool: 'eval',
entry: {
main: './src/main.ts',
polyfills: './src/polyfills.ts',
styles: './src/styles.scss'
},
output: {
path: resolve('./dist/'),
filename: '[name].js',
},
resolve: {
extensions: ['.ts', '.js'],
alias: rxPaths()
},
node: false,
performance: {
hints: false,
},
module: {
rules: [
{
test: /\.ts$/,
use: '@ngtools/webpack'
},
{
test: /\.js$/,
exclude: /(ngfactory|ngstyle).js$/,
enforce: 'pre',
use: 'source-map-loader'
},
{
test: /\.html$/,
use: 'raw-loader'
},
{
test: /\.s[ac]ss$/i,
use: ['sass-loader'],
// use: ['to-string-loader, css-loader, sass-loader'],
exclude: [resolve('./src/styles.scss')]
},
{
test: /\.s[ac]ss$/i,
// use: ['sass-loader'],
include: [resolve('./src/styles.scss')]
},
{
test: /\.(eot|svg|cur)$/,
loader: 'file-loader',
options: {
name: `[name].[ext]`,
limit: 10000
}
},
{
test: /\.(jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
loader: 'url-loader',
options: {
name: `[name].[ext]`,
limit: 10000
}
},
// This hides some deprecation warnings that Webpack throws
{
test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
parser: { system: true },
}
]
},
plugins: [
new IndexHtmlWebpackPlugin({
input: 'index.html',
output: 'index.html',
inject: 'body',
entrypoints: [
'styles',
'polyfills',
'main'
]
}),
new AngularCompilerPlugin({
mainPath: resolve('./src/main.ts'),
sourceMap: true,
nameLazyFiles: true,
tsConfigPath: resolve('./src/tsconfig.app.json')
// ,
// skipCodeGeneration: true
}),
new ProgressPlugin(),
new CircularDependencyPlugin({
exclude: /[\\\/]node_modules[\\\/]/
}),
new CopyWebpackPlugin([
{
from: 'src/assets',
to: 'assets'
},
{
from: 'src/favicon.ico'
}
])
]
};Run Code Online (Sandbox Code Playgroud)
请注意,我在Webpack配置中评论use []的原因是它引发了一些postcss-loader异常,并且在Github上的一个问题上发现,默认的sass-loader和Webpack会发生一些冲突,您应该删除它们。
这是angular.json中的构建配置
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js",
"replaceDuplicatePlugins": true
},
"outputPath": "./dist/",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
"src/assets",
"src/config",
"src/favicon.ico"
],
"styles": [
"./src/styles.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js",
"node_modules/metismenu/dist/metisMenu.js",
"node_modules/vis/dist/vis.min.js",
"vendor/js/jvectormap/jquery-jvectormap-2.0.2.min.js",
"vendor/js/jvectormap/jquery-jvectormap-us-mill.js",
"vendor/js/iCheck/icheck.js",
"node_modules/toastr/toastr.js"
]
},
"configurations": {
"dev": {
"optimization": true,
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.dev.ts"
}
]
},
"local": {
"optimization": true,
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
},
"production": {
"optimization": true,
"sourceMap": false,
"extractCss": false,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"styles": [
"./src/styles.scss"
]
}
}
},Run Code Online (Sandbox Code Playgroud)
这是Angular.json中的serve config。
"serve": {
"builder": "@angular-builders/custom-webpack:dev-server",
"options": {
"browserTarget": "projectName:build",
"customWebpackConfig": {
"path": "./webpack.config.js"
}
},
"configurations": {
"local":{
"browserTarget": "projectName:build:local"
}
,
"dev": {
"browserTarget": "projectName:build:dev"
},
"production": {
"browserTarget": "projectName:build:production"
}
}
},Run Code Online (Sandbox Code Playgroud)
我已经使用@import语句将所有样式导入了styles.scss中。我不确定的一件事就是我正在使用Highcharts并在构建过程中收到一些警告。
WARNING in ./node_modules/angular2-highcharts/dist/index.js
Module Warning (from ./node_modules/source-map-loader/index.js):
(Emitted value instead of an instance of Error) Cannot find source file '../src/index.ts': Error: Can't resolve '../src/index.ts'
Run Code Online (Sandbox Code Playgroud)
除此之外,项目中没有编译/运行时错误。
感谢您抽出宝贵的时间阅读我的问题。我感谢任何提示/技巧。
更新: 这是项目结构的样子:
WARNING in ./node_modules/angular2-highcharts/dist/index.js
Module Warning (from ./node_modules/source-map-loader/index.js):
(Emitted value instead of an instance of Error) Cannot find source file '../src/index.ts': Error: Can't resolve '../src/index.ts'
Run Code Online (Sandbox Code Playgroud)
项目中缺少文件index.ts,似乎文件index.js扩展名已从ts更改为js,并且正在将其作为ts查找。
尝试使用 src 文件夹中的index.ts 更改index.js 扩展名。
| 归档时间: |
|
| 查看次数: |
469 次 |
| 最近记录: |