ค้นหาเว็บไซต์

ไม่สามารถดีบั๊กไฟล์ TypeScript ใน Chrome ได้หลังจากอัปเดต Angular


ฉันได้เขียนแอปง่าย ๆ โดยใช้ Angular 4.2.5 เพื่อเรียนรู้บางสิ่งบางอย่าง ฉันสามารถดีบักไฟล์ TypeScript ใน Chrome ได้ตลอดเวลาเมื่อตอนนั้น แต่เมื่อเร็ว ๆ นี้ฉันอัปเดตเป็น Angular 5.2.7 และไม่สามารถเห็นไฟล์ TypeScript ใน Chrome ได้อีกต่อไป ไฟล์เหล่านี้ไม่ปรากฏเลยเมื่อกด control-P ในแท็บแหล่งที่มา (รวมถึงไฟล์ JavaScript ด้วย) ไฟล์ tsconfig.json ของฉันยังไม่ได้เปลี่ยนแปลง และอย่างที่คุณเห็น คุณสมบัติ "sourcemap" ถูกตั้งค่าเป็น true ดังนั้นฉันจึงไม่แน่ใจว่าเหตุใด Chrome ถึงหาคู่ไฟล์เหล่านี้ไม่เจอในตอนนี้

{   "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" ]   },   "exclude": [ "bin", "node_modules" ],   "atom": { "rewriteTsconfig": false } }

เพิ่ม webpack.config ตามที่ร้องขอ:

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$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-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' },
                { test: /\.scss$/, exclude: /node_modules/, loaders: ['raw-loader', 'sass-loader'] }
            ]
        },
        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.browser.module#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.server.module#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

การเพิ่มไฟล์ webpack.vendor ก็ด้วย:

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/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',
];
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)(\?|$)/, 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];
}

คำตอบ #1

เทมเพลต Angular จาก Visual Studio โดยค่าเริ่มต้น v4.2.5 จาก .NET Core 2.x.x

ถ้าคุณต้องการอัปเดตเป็นเวอร์ชัน 5 มันจะติดขัดและจะแสดง UNMET PEER DEPENDENCY สำหรับการอัปเดต npm เพื่อหลีกเลี่ยงปัญหานี้ พวกเขาจะสร้าง shrink-wrap เพื่อล็อกเวอร์ชัน npm (npm-shrinkwrap.json) มีรายงานปัญหาบางอย่าง ดูข้อมูลเพิ่มเติมได้

ปัญหาที่คุณมีตอนนี้ เพราะยังไม่ได้ทำการแปลงโค้ดของคุณให้เสร็จ จำไว้ว่า dotnet จะเก็บแคชแอปของคุณครั้งสุดท้ายที่มันถูกคอมไพล์เสมอ

ตรวจสอบให้แน่ใจว่าคุณได้อัปเดตการพึ่งพา npm และเปลี่ยนชื่อหรือเอาออก npm-shrinkwrap.json.

ติดตามบล็อกนี้:

ตามที่คุณทราบ Webpack ถูกใช้เป็นตัวรวมโมดูลโดย Visual Studio Webpack ใช้ AotPlugin ในการคอมไพล์แอป Angular 4 แต่ตอนนี้ Webpack ไม่ได้ใช้ AotPlugin สำหรับ Angular 5 อีกต่อไป ตอนนี้มันใช้ AngularCompilerPlugin คุณสามารถอ่านรายละเอียดเพิ่มเติมเกี่ยวกับเรื่องนี้ได้ที่นี่

ดังนั้น webpack.config ของคุณยังไม่ได้อัปเดต Aotplugin เป็น AngularCompilerPlugin นั่นเป็นปัญหาเดียวที่ต้องแก้ไข อ่านสิ่งนี้เพื่อข้อมูลเพิ่มเติม

แทนที่จะอัปเกรด Angular ด้วยตนเอง คุณสามารถใช้เวอร์ชันปัจจุบัน 4.x.x และจากนั้นคุณสามารถมุ่งเน้นไปที่การเขียนโค้ด และรอจนกว่า dotnet จะมีเทมเพลต Angular เวอร์ชัน 5 อัปเดตอย่างเป็นทางการ