问题描述
我想通过以下方式在我的 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.js
和 config/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 方域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!