1. <legend id='IHEHS'><style id='IHEHS'><dir id='IHEHS'><q id='IHEHS'></q></dir></style></legend>
      <i id='IHEHS'><tr id='IHEHS'><dt id='IHEHS'><q id='IHEHS'><span id='IHEHS'><b id='IHEHS'><form id='IHEHS'><ins id='IHEHS'></ins><ul id='IHEHS'></ul><sub id='IHEHS'></sub></form><legend id='IHEHS'></legend><bdo id='IHEHS'><pre id='IHEHS'><center id='IHEHS'></center></pre></bdo></b><th id='IHEHS'></th></span></q></dt></tr></i><div id='IHEHS'><tfoot id='IHEHS'></tfoot><dl id='IHEHS'><fieldset id='IHEHS'></fieldset></dl></div>

      <small id='IHEHS'></small><noframes id='IHEHS'>

        <bdo id='IHEHS'></bdo><ul id='IHEHS'></ul>
      <tfoot id='IHEHS'></tfoot>
    1. 在特定的 webpack 构建(角度 2)的情况下,如何将 CSS 文件中的资产 url() 替换为第 3 方域?

      时间:2023-09-05
      • <small id='cagAw'></small><noframes id='cagAw'>

        <i id='cagAw'><tr id='cagAw'><dt id='cagAw'><q id='cagAw'><span id='cagAw'><b id='cagAw'><form id='cagAw'><ins id='cagAw'></ins><ul id='cagAw'></ul><sub id='cagAw'></sub></form><legend id='cagAw'></legend><bdo id='cagAw'><pre id='cagAw'><center id='cagAw'></center></pre></bdo></b><th id='cagAw'></th></span></q></dt></tr></i><div id='cagAw'><tfoot id='cagAw'></tfoot><dl id='cagAw'><fieldset id='cagAw'></fieldset></dl></div>
          <tbody id='cagAw'></tbody>
      • <tfoot id='cagAw'></tfoot>

          <bdo id='cagAw'></bdo><ul id='cagAw'></ul>
            • <legend id='cagAw'><style id='cagAw'><dir id='cagAw'><q id='cagAw'></q></dir></style></legend>

                本文介绍了在特定的 webpack 构建(角度 2)的情况下,如何将 CSS 文件中的资产 url() 替换为第 3 方域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想通过以下方式在我的 CSS 中使用 url()s:

                I want to use the url()s in my CSS in the following way:

                .someImage {
                    background: url(/assets/img/some-image.png);
                }
                

                如果我使用的是开发版本 (config/webpack.dev.js),我希望它被翻译成 background: url(/assets/img/some-image.png);(因为它默认工作).

                In case I'm using development build (config/webpack.dev.js), I want it to to be translated to background: url(/assets/img/some-image.png); (as it works by default).

                但是,如果我使用的是生产版本 (config/webpack.prod.js),我希望它被翻译成 background: url(http://some-3rd-party-domain.com/assets/img/some-image.png);

                However In case I'm using production build (config/webpack.prod.js), I want it to be translated to background: url(http://some-3rd-party-domain.com/assets/img/some-image.png);

                考虑一下 http://some-3rd-party-domain.com,因为它是一个与为 Angular 应用程序提供服务的域完全不同的域.

                Think about the http://some-3rd-party-domain.com as it is a completely different domain than the one that servers the angular app.

                另一个重要的事情是我不想替换我的 css 文件中的所有路径.这意味着我只想在 url() 匹配特定路径时替换它们(在此示例中:/assets/*).

                Another important thing is that I don't want to replace all the paths in my css files. Which means that I want to replace url()s only if they match a specific path (in this example: /assets/*).

                如果没有深度黑客攻击,这可能吗?如果是,如何?

                您可以在这里找到我当前配置的一些详细信息:

                Here you can find some details from my current config:

                config/webpack.common.js的相关部分:

                // ...
                {
                    test: /.css$/,
                    loader: extractVendorCSS.extract({ 
                        fallbackLoader: 'style-loader', loader: 'css-loader' }),
                    include: [/node_modules/]
                },
                {
                    test: /.css$/,
                    use: ['to-string-loader', 'css-loader'],
                    exclude: [/node_modules/]
                },
                {
                    test: /.(jpg|png|gif)$/,
                    use: 'file-loader'
                },
                {
                    test: /.(png|woff|woff2|eot|ttf|svg)(?v=[0-9].[0-9].[0-9])?$/,
                    loader: 'url'
                }
                // ...
                

                config/webpack.dev.jsconfig/webpack.prod.js 都以下列方式包含环境特定的 URL 信息:

                Both config/webpack.dev.js and config/webpack.prod.js contains the environment specific URL info in the following way:

                config/webpack.dev.js:

                // ...
                const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
                const API_URL = process.env.API_URL = 'http://localhost:3000/assets/mock-data/';
                const ASSETS_URL = process.env.API_URL = 'http://localhost:3000/assets/';
                const HOST = process.env.HOST || 'localhost';
                const PORT = process.env.PORT || 3000;
                const METADATA = webpackMerge(commonConfig({env: ENV}).metadata, {
                    host: HOST,
                    API_URL: API_URL,
                    ASSETS_URL: ASSETS_URL,
                    port: PORT,
                    ENV: ENV,
                });
                // ...
                

                config/webpack.prod.js:

                // ...
                const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
                const API_URL = process.env.API_URL = 'http://some-api-url/rest/';
                const ASSETS_URL = process.env.ASSETS_URL = 'http://some-3rd-party-domain.com/assets/';
                const HOST = process.env.HOST || 'localhost';
                const PORT = process.env.PORT || 8080;
                const METADATA = webpackMerge(commonConfig({env: ENV}).metadata, {
                    host: HOST,
                    API_URL: API_URL,
                    ASSETS_URL: ASSETS_URL,
                    port: PORT,
                    ENV: ENV,
                });
                // ...
                

                谢谢!

                推荐答案

                我想,你应该查看关于 publicPath 的文档:http://webpack.github.io/docs/configuration.html#output-公共路径

                I think, you should check the documentation about publicPath: http://webpack.github.io/docs/configuration.html#output-publicpath

                output: {
                    path: "/home/proj/public/assets",
                    publicPath: ENV === "development"? "/assets/" : "http://some-3rd-party-domain.com/assets/"
                }

                但是有一个重要的时刻:
                您对图像使用绝对路径 (background: url(/assets/img/some-image.png);) 并且 css-loader 不会对您的 url 执行任何操作

                您应该切换到另一种语法:background: url(~img/some-image.png);
                https://github.com/webpack-contrib/css-loader 中的更多信息

                But there is one important moment:
                you use an absolute path for images (background: url(/assets/img/some-image.png);) and css-loader will do nothing with your urls

                You should switch to another syntax: background: url(~img/some-image.png);
                more information in https://github.com/webpack-contrib/css-loader

                这篇关于在特定的 webpack 构建(角度 2)的情况下,如何将 CSS 文件中的资产 url() 替换为第 3 方域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <bdo id='NKj1E'></bdo><ul id='NKj1E'></ul>

                  <small id='NKj1E'></small><noframes id='NKj1E'>

                      <tbody id='NKj1E'></tbody>
                      <legend id='NKj1E'><style id='NKj1E'><dir id='NKj1E'><q id='NKj1E'></q></dir></style></legend>

                      <tfoot id='NKj1E'></tfoot>
                      <i id='NKj1E'><tr id='NKj1E'><dt id='NKj1E'><q id='NKj1E'><span id='NKj1E'><b id='NKj1E'><form id='NKj1E'><ins id='NKj1E'></ins><ul id='NKj1E'></ul><sub id='NKj1E'></sub></form><legend id='NKj1E'></legend><bdo id='NKj1E'><pre id='NKj1E'><center id='NKj1E'></center></pre></bdo></b><th id='NKj1E'></th></span></q></dt></tr></i><div id='NKj1E'><tfoot id='NKj1E'></tfoot><dl id='NKj1E'><fieldset id='NKj1E'></fieldset></dl></div>