@anvilco/reorient-css

中文文档
0.0.1发布于 2022/06/22MITCommonJS近 30 天下载 7579

Updates relative asset URLs within a string of CSS to reflect a relocation of the CSS file.

原始使用文档
文档目录5 节
  1. Usage
  2. Notes
  3. Options
  4. PostCSS
  5. License

reorient-css Build Status

NPM

Updates relative asset URLs (e.g. url(../images/something.png)) within a string of CSS to reflect a relocation of the CSS file.

Example use case: if you were inlining CSS from styles/main.css into a <style> element in index.html (as part of a build task aiming to reduce HTTP round trips), you could use this function to rewrite all the image/font URLs within the CSS so they still work from the new relative base – in this example, it would prepend all file-relative URLs with style/ (and resolve any .. etc).

Usage

npm install reorient-css

var reorientCSS = require('reorient-css');

var result = reorientCSS(
  'body { background: url(something.png); }', // css string
  'some/old/stylesheet/file.css',             // old css location
  'some/new/one.css'                          // new css location (this could even be an .html file,
);                                            // if you're inlining the CSS into a <style> element
                                              // - the basename is ignored anyway)

console.log(result.css);
// > body { background: url('../old/stylesheet/something.png'); }

Notes

  • It doesn't care whether files exist at the old/new paths – it just uses these path strings to determine the relative path from the new location back to the old location, so it can know how to rewrite the asset URLs.
  • Only file-relative URLs are rewritten. Root-relative, absolute and data: URLs will be left untouched, as these are expected to work the same regardless of the CSS file's location.
  • behavior properties (a proprietary IE thing) will be left untouched, because any URLs in these properties are relative to the HTML document, not the CSS file, so in theory they should never change.
    • The only exception is if you are moving CSS from one HTML document to another (ie, in <style> elements). If both the new and old CSS locations you provide are .html files, behavior properties will be reoriented.
  • Whitespace and formatting are left untouched.
  • The output is an object with a property named css, which contains the reoriented CSS string. This object might also contain a map property, if you have specified that option (see below).

Options

The function accepts an optional fourth argument: an object of extra options to pass to PostCSS. For example, {map: true} will tell it to generate a source map (which will be available in the map key of the result object). See the PostCSS docs for more details.

PostCSS

reorient-css can also be used as a PostCSS processor.

var postcss = require('postcss'),
    reorientCSS = require('reorient-css');

var result = postcss()
    .use( reorientCSS.processor( 'some/old/stylesheet/file.css', 'some/new/one.css' ) )
    .process( 'body { background: url(something.png); }' );

console.log(result.css);
// > body { background: url('../old/stylesheet/something.png'); }

License

Copyright (c) 2014 Callum Locke. Licensed under the MIT license.

版本与发布记录

@anvilco/reorient-css 共发布 1 个版本,首次发布于 2022/06/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2022/06/224 年前

关于 @anvilco/reorient-css 的常见问题

@anvilco/reorient-css 是什么?

Updates relative asset URLs within a string of CSS to reflect a relocation of the CSS file.

如何安装 @anvilco/reorient-css?

在项目目录运行 npm install @anvilco/reorient-css,或使用 pnpm add / yarn add / bun add。

@anvilco/reorient-css 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/anvilco__reorient-css 是否存在。

@anvilco/reorient-css 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anvilco/reorient-css 可以商用吗?

@anvilco/reorient-css 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anvilco/reorient-css 的下载量有多少?

根据 npm 官方下载统计,@anvilco/reorient-css 在 2026/10/06 – 2026/11/04 这 30 天内被下载 7,579 次,日均约 253 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anvilco/reorient-css 还在维护吗?

最新版本 0.0.1 发布于 2022/06/22(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。