使用React + Typescript + WebPack的图像的URL错误

时间:2018-02-21 06:09:31

标签: javascript reactjs typescript webpack asp.net-core

我在.JSX文件中引用了一个图像,但生成的URL错误。 它看起来像这样:http://localhost:43124/dist/dist/9ee7eb54c0eb428bb30b599ef121fe25.jpg

文件夹“dist”与图片一起存在但不是“dist / dist”。我认为问题来自我的Webpack.config.js。以下是文件:

module.d.ts

我指示Typescript如何处理图像文件,如提到here

declare module '*.jpg'
declare module '*.svg'

Layout.tsx

我在React中引用了我的徽标,因此它可以由Webpack打包。

/// <reference path="./module.d.ts"/>
import * as React from 'react';
import logo from '../img/logo.svg';

export class Layout extends React.Component<{}, {}> {
    public render() {
        return <img src="{logo}" width="220" alt="logo" />
    }
}

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const merge = require('webpack-merge');

module.exports = (env) => {
    const isDevBuild = !(env && env.prod);

    // Configuration in common to both client-side and server-side bundles
    const sharedConfig = () => ({
        stats: { modules: false },
        resolve: { extensions: ['.js', '.jsx', '.ts', '.tsx'] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' },
                { 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-client.tsx' },
        module: {
            rules: [
                { test: /\.css$/, use: ExtractTextPlugin.extract({ use: isDevBuild ? 'css-loader' : 'css-loader?minimize' }) }
            ]
        },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new ExtractTextPlugin('style.css'),
            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()
        ])
    });       

    return [clientBundleConfig];
};

我使用默认的Visual Studio ASP.NET Core React + Redux模板。

0 个答案:

没有答案