簡體   English   中英

Visual Studio 2017 .Net Core 2.0 Angular發布失敗

[英]Visual Studio 2017 .Net Core 2.0 Angular Publish Fails

我正在運行Windows 7 Professional PC,使用Visual Studio 2017版本15.4.5。 當我創建一個.Net Core 2.0項目並選擇Angular類型時,它開箱即用。

然后我以我認為是標准的方式添加了真棒字體和primeng( https://www.primefaces.org/primeng/#/ ),即,添加對package.json文件的引用,然后向該文件的nonTreeShakableModules部分添加webpack.config.vendor.js文件,然后將模塊添加到app.module.shared.ts。 這是Primeng版本5.0.2和超棒字體版本4.7.0。

在開發中,一切都很好-一切運行良好。 但是,當我嘗試發布時,在“輸出”窗口中看到以下幾行:

 npm install removed 3 packages in 3.389s node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod 

什么? 為什么要刪除3個軟件包

然后,發布嘗試失敗。 失敗后,我發現我的npm軟件包中有3個未安裝:1)@ types / jquery 2)真棒字體3)primeng

而且我看到一堆“找不到模塊”錯誤-當然沒有找到它們,因為它們只是被刪除了!

我查看了.csproj文件,然后看到以下內容:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

因此,據我所知,“ npm install”命令或該命令觸發的操作正在刪除這些模塊。 每次發布嘗試后,我都必須重新安裝它們。

所以,我的問題是:為什么要刪除這些軟件包,我該怎么辦?

我的package.json文件:

{
    "name": "CaseManagementReporting",
    "private": true,
    "version": "0.0.0",
    "scripts": {
        "test": "karma start ClientApp/test/karma.conf.js"
    },
    "dependencies": {
        "@angular/animations": "4.2.5",
        "@angular/common": "4.2.5",
        "@angular/compiler": "4.2.5",
        "@angular/compiler-cli": "4.2.5",
        "@angular/core": "4.2.5",
        "@angular/forms": "4.2.5",
        "@angular/http": "4.2.5",
        "@angular/platform-browser": "4.2.5",
        "@angular/platform-browser-dynamic": "4.2.5",
        "@angular/platform-server": "4.2.5",
        "@angular/router": "4.2.5",
        "@ngtools/webpack": "1.5.0",
        "@types/webpack-env": "1.13.0",
        "angular2-template-loader": "0.6.2",
        "aspnet-prerendering": "^3.0.1",
        "aspnet-webpack": "^2.0.1",
        "awesome-typescript-loader": "3.2.1",
        "bootstrap": "3.3.7",
        "css": "2.2.1",
        "css-loader": "0.28.4",
        "es6-shim": "0.35.3",
        "event-source-polyfill": "0.0.9",
        "expose-loader": "0.7.3",
        "extract-text-webpack-plugin": "2.1.2",
        "file-loader": "0.11.2",
        "font-awesome": "4.7.0",
        "html-loader": "0.4.5",
        "isomorphic-fetch": "2.2.1",
        "jquery": "3.2.1",
        "json-loader": "0.5.4",
        "preboot": "4.5.2",
        "primeng": "5.0.2",
        "raw-loader": "0.5.1",
        "reflect-metadata": "0.1.10",
        "rxjs": "5.4.2",
        "style-loader": "0.18.2",
        "to-string-loader": "1.1.5",
        "typescript": "2.4.1",
        "url-loader": "0.5.9",
        "webpack": "2.5.1",
        "webpack-hot-middleware": "2.18.2",
        "webpack-merge": "4.1.0",
        "zone.js": "0.8.12"
    },
    "devDependencies": {
        "@types/chai": "4.0.1",
        "@types/jasmine": "2.5.53",
        "@types/jquery": "3.2.1",
        "chai": "4.0.2",
        "jasmine-core": "2.6.4",
        "karma": "1.7.0",
        "karma-chai": "0.1.0",
        "karma-chrome-launcher": "2.2.0",
        "karma-cli": "1.0.1",
        "karma-jasmine": "1.1.0",
        "karma-webpack": "2.0.3"
    }
}

我的tsconfig.json文件:

{
  "compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "target": "es5",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipDefaultLibCheck": true,
    "skipLibCheck": true, // Workaround for https://github.com/angular/angular/issues/17863. Remove this if you upgrade to a fixed version of Angular.
    "strict": true,
    "lib": [ "es6", "dom" ],
    "types": [ "webpack-env", "jquery" ]
  },
  "exclude": [ "bin", "node_modules" ],
  "atom": { "rewriteTsconfig": false }
}

我的webpack.config.vendor.js文件:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');
const treeShakableModules = [
    '@angular/animations',
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/forms',
    '@angular/http',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@angular/router',
    'zone.js'
];

const nonTreeShakableModules = [
    'bootstrap',
    'bootstrap/dist/css/bootstrap.css',
    'es6-promise',
    'es6-shim',
    'event-source-polyfill',
    'jquery',
    'font-awesome/css/font-awesome.css',
    'primeng/resources/themes/bootstrap/theme.css',
    'primeng/resources/primeng.min.css',
    'primeng/primeng'
];
const allModules = treeShakableModules.concat(nonTreeShakableModules);

module.exports = (env) => {
    const extractCSS = new ExtractTextPlugin('vendor.css');
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        resolve: { extensions: [ '.js' ] },
        module: {
            rules: [
                { test: /\.(png|woff|woff2|eot|ttf|svg|jpg|jpeg|gif)(\?|$)/, use: 'url-loader?limit=100000' }
            ]
        },
        output: {
            publicPath: 'dist/',
            filename: '[name].js',
            library: '[name]_[hash]'
        },
        plugins: [
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
            new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
            new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/14898
            new webpack.IgnorePlugin(/^vertx$/) // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
        ]
    };

    const clientBundleConfig = merge(sharedConfig, {
        entry: {
            // To keep development builds fast, include all vendor dependencies in the vendor bundle.
            // But for production builds, leave the tree-shakable ones out so the AOT compiler can produce a smaller bundle.
            vendor: isDevBuild ? allModules : nonTreeShakableModules
        },
        output: { path: path.join(__dirname, 'wwwroot', 'dist') },
        module: {
            rules: [
                { test: /\.css(\?|$)/, use: extractCSS.extract({ use: isDevBuild ? 'css-loader' : 'css-loader?minimize' }) }
            ]
        },
        plugins: [
            extractCSS,
            new webpack.DllPlugin({
                path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    const serverBundleConfig = merge(sharedConfig, {
        target: 'node',
        resolve: { mainFields: ['main'] },
        entry: { vendor: allModules.concat(['aspnet-prerendering']) },
        output: {
            path: path.join(__dirname, 'ClientApp', 'dist'),
            libraryTarget: 'commonjs2',
        },
        module: {
            rules: [
                { test: /\.css(\?|$)/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] }
            ]
        },
        plugins: [
            new webpack.DllPlugin({
                path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ]
    });

    return [clientBundleConfig, serverBundleConfig];
}

我的webpack.config.js文件:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: [ '.js', '.ts' ] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot.browser.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin(),
            new AotPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
                exclude: ['./**/*.server.ts']
            })
        ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot.server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AotPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

我找到了解決此問題的方法(可悲的是,它導致了另一個問題,但是...)。

我相信這是我正在使用的NPM版本中的錯誤。 我安裝了npm版本5.0.3。 經過數小時的搜索,我遇到了這個GitHub討論: 在這里 ,許多人抱怨npm install命令實際上是在刪除模塊。 我將我的npm實例從5.0.3降級到3.10.3(這是我的家庭環境正在使用的實例),我發現不再刪除軟件包。 (為什么有人用“ npm install”之類的命令來寫一些會刪除軟件包的東西,但是我卻看了鏈接的文章,您會發現它造成的挫敗感!)

當然,作為.Net開發人員,我什至都不知道如何降級我的npm實例,因此我必須進行查找。 對於可能會受到幫助的用戶,命令是:npm install -g npm@3.10.3。 我發現在這里

因此,現在我的發布過程運行時沒有刪除程序包,這就是我在問題中所問的。 該問題已解決,因此我將其視為答案。

但這並不是說降級解決了我所有的問題。 當我嘗試發布時,出現了新錯誤,我認為這些錯誤與npm版本無關:

在'C:\\ Projects \\ ForthrightProjects \\ CaseManagementReporting \\ CaseManagementReporting \\ ClientApp'中找不到'./../$$_gendir/ClientApp/app/app.app.module.browser.ngfactory'找不到CaseManagementReporting模塊0

在'C:\\ Projects \\ ForthrightProjects \\ CaseManagementReporting \\ CaseManagementReporting \\ ClientApp'中找不到'./../$$_gendir/ClientApp/app/app.app.module.server.ngfactory'找不到CaseManagementReporting模塊0

還有更多Googling找到了這篇文章: 在這里建議注釋掉webpack.config.js文件中的這一行:

// {測試:/。ts $ /,包括:/ ClientApp /,使用:isDevBuild? ['awesome-typescript-loader?silent = true','angular2-template-loader']:'@ ngtools / webpack'},

或者,也可以將其更改為:{test:/.ts$/,包括:/ ClientApp /,使用:['awesome-typescript-loader?silent = true','angular2-template-loader']},

我嘗試了兩種選擇,並且它們都可以克服上述錯誤。 但是,我現在嘗試發布時遇到另一個錯誤:模塊的元數據版本不匹配:C:/Projects/ForthrightProjects/CaseManagementReporting/CaseManagementReporting/node_modules/primeng/components/dom/domhandler.d.ts,找到的版本4,預期3

因此,經過一些額外的搜索,反復試驗和錯誤,我遇到了這個鏈接: 這里的建議之一是將project.json文件中的primeng條目更改為:“ primeng”:“ ^ 4.2.4”

足夠令人驚訝的是,這確實有效。 我現在可以發布了。 希望成功地部署已發布的文件不會造成如此巨大的痛苦。

對.Net開發人員:還記得剛剛使用NuGet添加軟件包時,一切都正常嗎? 嘆...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM