first commit

This commit is contained in:
2026-09-08 20:28:54 +08:00
commit 2ada8d3d5a
37380 changed files with 4886169 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
; EditorConfig is awesome: http://EditorConfig.org
; Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
charset = 'utf-8'
trim_trailing_whitespace = true
+28
View File
@@ -0,0 +1,28 @@
{
"node": true,
"maxparams": 5,
"maxdepth": 5,
"maxstatements": 40,
"maxcomplexity": 10,
"bitwise": false,
"boss" : false,
"curly": true,
"eqeqeq": true,
"forin": true,
"globalstrict": true,
"immed": true,
"indent": 4,
"latedef": "nofunc",
"laxcomma": true,
"loopfunc": true,
"nonew": true,
"multistr": true,
"newcap": true,
"quotmark": true,
"strict": true,
"trailing": true,
"undef": true,
"unused": true
}
+3
View File
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '8.4.0'
+8
View File
@@ -0,0 +1,8 @@
{
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
+26
View File
@@ -0,0 +1,26 @@
0.1.0:
date: 2014-08-12
changes:
- Initial release.
0.1.1:
date: 2014-08-13
changes:
- Bumped post-helpers version to 0.2.0.
0.2.0:
date: 2015-12-20
changes:
- Bumped post-helpers version to 0.3.1.
- Updated dependencies.
- Updated deprecated postcss syntax.
0.2.1:
date: 2017-01-05
changes:
- Fixed issue #3 (properties not accepted 'true').
0.2.2
date: 2017-08-31
changes:
- Fixed issue #4 (blank url() throws error).
0.3.0
date: 2024-08-12
changes:
- Replace deprecated node.js methods and update dependencies.
+22
View File
@@ -0,0 +1,22 @@
Copyright (c) 2014 Alexey Ivanov
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
+66
View File
@@ -0,0 +1,66 @@
## postcss-urlrewrite [![Build Status](https://secure.travis-ci.org/iAdramelk/postcss-urlrewrite.png)](https://travis-ci.org/iAdramelk/postcss-urlrewrite)
> PostCSS plugin for easy url() rewriting.
### Getting Started
```shell
npm install postcss-urlrewrite
```
### Example
Usage example:
```javascript
// dependencies
var fs = require( 'fs' );
var postcss = require( 'postcss' );
var urlrewrite = require( 'postcss-urlrewrite' );
// css to be processed
var css = fs.readFileSync( 'build/build.css', 'utf8' );
// config for urlrewrite
var config = {
imports: true,
properties: [ 'background', 'content' ],
rules: [
{ from: \local\, to: 'global' },
{ from: \local2\, to: 'global2' }
]
};
// process css using postcss-urlrewrite
var out = postcss()
.use( urlrewrite( config ) )
.process( css )
.css;
```
### Configuration
#### imports
**Type**: boolean
**Default**: false
If set to true will replace urls in **@import** at-rules.
#### properties
**Type**: array or boolean
**Default**: true
List of css-properties to replace. If set to true, will work with all
properties. If set to array will work only with the properties in the list.
#### rules
**Type**: array of objects or function
There is to way to set rules:
1. Create array of objects with "from" and "to" keys. "from" can be **String** or **RegExp**, "to" can be **String** or **Function**. See [String.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) syntax for details. ONLY first matching rule will be triggered on each URI.
2. Create function that will work with [URIjs](http://medialize.github.io/URI.js/) objects and mutate them one way or another.
+3
View File
@@ -0,0 +1,3 @@
"use strict";
module.exports = require("./lib/urlrewrite.js");
+142
View File
@@ -0,0 +1,142 @@
"use strict";
var helpers = require("postcss-helpers");
var util = require("util");
/**
* Checks validity of config object.
* @private
* @param {Object} config Configuration object
*/
var validateConfig = function (config) {
if (!config || !config.rules) {
throw new Error("You must configure at least one replace rule.");
}
if (!Array.isArray(config.rules) && typeof config.rules !== "function") {
throw new Error("Rules must be an Array or Function.");
}
if (Array.isArray(config.rules)) {
config.rules.forEach(function (rule) {
if (!rule.from && !rule.to) {
throw new Error(
'Rules must be in { from: "from", to: "to" } format.'
);
} else if (
typeof rule.from !== "string" &&
!util.types.isRegExp(rule.from)
) {
throw new Error("Rule.from must be a String or RegExp.");
} else if (["string", "function"].indexOf(typeof rule.to) === -1) {
throw new Error("Rule.to must be a String or Function.");
}
});
}
if (
config.properties &&
!Array.isArray(config.properties) &&
!(typeof config.properties === "boolean")
) {
throw new Error("Properties must be an Array of Strings or Boolean.");
}
if (Array.isArray(config.properties)) {
config.properties.forEach(function (prop) {
if (typeof prop !== "string") {
throw new Error('Items in "properties" array must be Strings.');
}
});
}
};
/**
* Returns callback function for URI replacement based on config params.
* @private
* @param {Array} rules Array of objects with "from" and "to" keys to use
* as arguments to String.replace.
* @returns {Function} Callback function.
*/
var useRulesCallback = function (rules) {
rules = rules;
return function (uri) {
var modified = false;
var original = uri.href();
rules.forEach(function (rule) {
if (modified) {
return;
}
var tmp = original.replace(rule.from, rule.to);
if (tmp !== original) {
modified = true;
uri.href(tmp);
}
});
};
};
/**
* Plugin body.
* @param {Boolean} config.imports If set to true, will also replace @import values.
* @param {Array|Boolean} config.properties List of css properties to update or false.
* @param {Array|Function} config.rules Array of replace params or
* callback function.
* @returns {Function} PostCSS plugin.
*/
var urlrewrite = function (config) {
validateConfig(config);
// Choosing which callback to use: One received from user or autogenerated
// from params.
var callback = Array.isArray(config.rules)
? useRulesCallback(config.rules)
: config.rules;
// Function to update @import URIs
var updateImport = function (atRule) {
if (atRule.name !== "import") {
return;
}
var helper = helpers.createImportHelper(atRule.params);
callback(helper.URI);
atRule.params = helper.getModifiedRule();
};
// Function to update declarations URIs
var updateDecl = function (decl) {
if (config.properties === false) {
return;
}
if (
Array.isArray(config.properties) &&
config.properties.indexOf(decl.prop) === -1
) {
return;
}
if (!decl.value.match(helpers.regexp.URLS)) {
return;
}
var helper = helpers.createUrlsHelper(decl.value);
helper.URIS.forEach(callback);
decl.value = helper.getModifiedRule();
};
return function (style) {
if (config.imports) {
style.walkAtRules(updateImport);
}
if (config.properties !== false) {
style.walkDecls(updateDecl);
}
};
};
module.exports = urlrewrite;
+31
View File
@@ -0,0 +1,31 @@
{
"name": "postcss-urlrewrite",
"description": "PostCSS plugin for easy url() rewriting.",
"version": "0.3.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/iAdramelk/postcss-urlrewrite.git"
},
"authors": [
"Alexey Ivanov <mail@alexeyivanov.info>"
],
"license": "MIT",
"engines": {
"node": ">=16"
},
"scripts": {
"test": "mocha --reporter list"
},
"keywords": [
"postcss",
"css"
],
"dependencies": {
"postcss-helpers": "^0.3.3"
},
"devDependencies": {
"mocha": "^10.7.3",
"postcss": "^8.4.41"
}
}
+11
View File
@@ -0,0 +1,11 @@
.web-one { background: url("http://www.google.com/images/srpr/logo3w.png"); }
.web-two { background: url(http://www.google.com/images/srpr/logo3w.png); }
.web-three { background: url('http://www.google.com/images/srpr/logo3w.png'); }
.absolute-one { background: url("/resources.png"); }
.absolute-two { background: url(/resources.png); }
.absolute-three { background: url('/resources.png'); }
+11
View File
@@ -0,0 +1,11 @@
.web-one { background: url("http://yandex.ru/images/srpr/logo3w.png"); }
.web-two { background: url(http://yandex.ru/images/srpr/logo3w.png); }
.web-three { background: url('http://yandex.ru/images/srpr/logo3w.png'); }
.absolute-one { background: url("http://mysite.com/resources.png"); }
.absolute-two { background: url(http://mysite.com/resources.png); }
.absolute-three { background: url('http://mysite.com/resources.png'); }
+9
View File
@@ -0,0 +1,9 @@
.png { background: url(local/resources.png); }
.gif { background: url("local/resources.gif"); }
.svg { background: url('local/resources.svg'); }
.jpg { background: url('local/resources.jpg'); }
.jpeg { background: url('local/resources.jpeg'); }
+9
View File
@@ -0,0 +1,9 @@
.png { background: url(global/resources.png); }
.gif { background: url("global/resources.gif"); }
.svg { background: url('global/resources.svg'); }
.jpg { background: url('global/resources.jpg'); }
.jpeg { background: url('global/resources.jpeg'); }
+3
View File
@@ -0,0 +1,3 @@
.blank {
background: url();
}
+3
View File
@@ -0,0 +1,3 @@
.blank {
background: url();
}
+3
View File
@@ -0,0 +1,3 @@
.datauri {
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzFlNTc5OSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUwJSIgc3RvcC1jb2xvcj0iIzI5ODlkOCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUxJSIgc3RvcC1jb2xvcj0iIzIwN2NjYSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3ZGI5ZTgiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
}
+3
View File
@@ -0,0 +1,3 @@
.datauri {
background: url(image.png);
}
+9
View File
@@ -0,0 +1,9 @@
.filter {
background-image: url('local/file');
content: url('local/file');
cue-after: url('local/file');
cue-before: url('local/file');
cursor: url('local/file'), url('local/file2');
list-style-image: url('local/file');
play-during: url('local/file');
}
+9
View File
@@ -0,0 +1,9 @@
.filter {
background-image: url('local/file');
content: url('global/file');
cue-after: url('local/file');
cue-before: url('local/file');
cursor: url('global/file'), url('global/file2');
list-style-image: url('local/file');
play-during: url('local/file');
}
+13
View File
@@ -0,0 +1,13 @@
@font-face {
font-family: 'PT Sans';
font-style: normal;
font-weight: normal;
src: local('PT Sans'), local('PTSans'), url('local/font.eot?#iefix&v=3.2.1') format('eot');
}
@font-face {
font-family: 'PT Sans';
font-style: normal;
font-weight: normal;
src: local('PT Sans'), local('PTSans'), url(local/font.woff) format('woff');
}
+13
View File
@@ -0,0 +1,13 @@
@font-face {
font-family: 'PT Sans';
font-style: normal;
font-weight: normal;
src: local('PT Sans'), local('PTSans'), url('global/font.eot?#iefix&v=3.2.1') format('eot');
}
@font-face {
font-family: 'PT Sans';
font-style: normal;
font-weight: normal;
src: local('PT Sans'), local('PTSans'), url(global/font.woff) format('woff');
}
+5
View File
@@ -0,0 +1,5 @@
@import "local/style.css";
.rule {
background: url( "local/image.png" );
}
+5
View File
@@ -0,0 +1,5 @@
@import "global/style.css";
.rule {
background: url( "local/image.png" );
}
+3
View File
@@ -0,0 +1,3 @@
.multiple {
background: url('local/one.jpg'), url('local/two.jpg'), url('local/three.jpg');
}
+3
View File
@@ -0,0 +1,3 @@
.multiple {
background: url('global/one.jpg'), url('global/two.jpg'), url('global/three.jpg');
}
+3
View File
@@ -0,0 +1,3 @@
.rule {
background: url( "local/image.png" );
}
@@ -0,0 +1,3 @@
.rule {
background: url( "global/image.png" );
}
+9
View File
@@ -0,0 +1,9 @@
.properties {
background-image: url('local/file');
content: url('local/file');
cue-after: url('local/file');
cue-before: url('local/file');
cursor: url('local/file'), url('local/file2');
list-style-image: url('local/file');
play-during: url('local/file');
}
+9
View File
@@ -0,0 +1,9 @@
.properties {
background-image: url('global/file');
content: url('global/file');
cue-after: url('global/file');
cue-before: url('global/file');
cursor: url('global/file'), url('global/file2');
list-style-image: url('global/file');
play-during: url('global/file');
}
+11
View File
@@ -0,0 +1,11 @@
.rule1 {
background: url( 'local/test' );
}
.rule2 {
background: url( 'global/test' );
}
.rule3 {
background: url( 'test' );
}
+11
View File
@@ -0,0 +1,11 @@
.rule1 {
background: url( 'local/test1' );
}
.rule2 {
background: url( 'global/test2' );
}
.rule3 {
background: url( 'test3' );
}
+142
View File
@@ -0,0 +1,142 @@
/*global describe, it */
"use strict";
var fs = require("fs");
var assert = require("assert");
var postcss = require("postcss");
var urlrewrite = require("../index.js");
var fixture = function (name) {
return fs.readFileSync("test/fixtures/" + name + ".css", "utf8").trim();
};
var compareFixtures = function (name, options) {
var actual = postcss(urlrewrite(options)).process(fixture(name)).css.trim();
var expected = fixture(name + ".out");
return assert.equal(actual, expected);
};
describe("postcss-urlrewrite", function () {
describe("paths in absolute rules", function () {
it("should be replaced", function () {
var config = {
rules: [
{ from: "http://www.google.com/", to: "http://yandex.ru/" },
{ from: /^\//, to: "http://mysite.com/" },
],
};
compareFixtures("absolute", config);
});
});
describe("different file types", function () {
it("should return must not affect replacement", function () {
var config = {
rules: [{ from: "local", to: "global" }],
};
compareFixtures("backgrounds", config);
});
});
describe("data-uris", function () {
it("should be replaceable", function () {
var config = {
rules: function (uri) {
uri.href("image.png");
},
};
compareFixtures("datauri", config);
});
});
describe('only "content" and "cursor" properties', function () {
it("should be updated", function () {
var config = {
properties: ["content", "cursor"],
rules: [{ from: "local", to: "global" }],
};
compareFixtures("filter", config);
});
});
describe("fonts src with ie hacks", function () {
it("should be replaced without errors", function () {
var config = {
rules: [{ from: "local", to: "global" }],
};
compareFixtures("fonts", config);
});
});
describe('only "content" and "cursor" properties', function () {
it("should be updated", function () {
var config = {
imports: true,
properties: false,
rules: [{ from: "local", to: "global" }],
};
compareFixtures("imports", config);
});
});
describe("multiple url() in property value", function () {
it("should be replaced without errors", function () {
var config = {
rules: [{ from: "local", to: "global" }],
};
compareFixtures("multiple", config);
});
});
describe("all properties from css 2.1 spec", function () {
it("should be replaceable", function () {
var config = {
rules: [{ from: "local", to: "global" }],
};
compareFixtures("properties", config);
});
});
describe("if multiple rules match value", function () {
it("only first should trigger replace", function () {
var config = {
rules: [
{ from: /local\/test/, to: "$&1" },
{ from: /global\/test/, to: "$&2" },
{ from: /test/, to: "$&3" },
],
};
compareFixtures("rules", config);
});
});
describe('if properties value is "true"', function () {
it("it should not return error", function () {
var config = {
properties: true,
rules: [{ from: "local", to: "global" }],
};
compareFixtures("properties-default", config);
});
});
describe("if blank url()", function () {
it("it should not return error", function () {
var config = {
rules: [{ from: "local", to: "global" }],
};
compareFixtures("blank-url", config);
});
});
});