Skip to content

Commit a655cd4

Browse files
author
dongxu.shelton
committed
feat: publish v0.0.1
Change-Id: Ic7e03778dbf8b4c0a0f89f8e27c8f420eb1c81d2
0 parents  commit a655cd4

14 files changed

+907
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.{json,yml}]
12+
indent_size = 2

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Change Log
2+
# v0.0.1
3+
### Publish v0.0.1

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2017 shelton dong <dongxu2048@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# PostCSS File
2+
3+
[PostCSS]: https://github.com/postcss/postcss
4+
5+
[PostCSS] File is a assets handler for css. It can copies assets and overwrites your url, it also can inserts images as a inline data.
6+
7+
# Installation
8+
9+
```shell
10+
npm install --save-dev postcss-file
11+
```
12+
13+
# Usage
14+
15+
## Webpack
16+
```javascript
17+
// webpack.config.js
18+
module.exports = {
19+
// ...
20+
module: {
21+
rules: [
22+
// ...
23+
{
24+
test: /\.css$/,
25+
use: [{
26+
loader: 'postcss-loader',
27+
options: {
28+
plugins: [
29+
require('postcss-file')({
30+
url: 'copy',
31+
assetsPath: 'dist/assets',
32+
publicPath: './assets/',
33+
hash: true
34+
})
35+
],
36+
},
37+
}]
38+
}
39+
]
40+
}
41+
}
42+
```
43+
44+
## Rollup
45+
```javascript
46+
// rollup.config.js
47+
import postcss from 'postcss';
48+
49+
const config = {
50+
// ...
51+
plugins: [
52+
// ...
53+
postcss({
54+
// ...
55+
plugins: [
56+
require('postcss-file')({
57+
url: 'copy',
58+
assetsPath: 'dist/assets',
59+
publicPath: './assets/',
60+
hash: true
61+
})
62+
]
63+
})
64+
]
65+
};
66+
67+
export default config;
68+
```
69+
#### Note: You must use options to specify how would you like to handle your assets in your css, if you don't pass any options, PostCSS File will do nothing about your assets.
70+
71+
# Url Resolve
72+
73+
You can copy your assets to anywhere or insert them as inline data through "url" option.
74+
75+
## Copy
76+
77+
```javascript
78+
postcss({
79+
plugins: [
80+
require({
81+
url: 'copy',
82+
assetsPath: './dist/assets',
83+
publicPath: 'assets/'
84+
})
85+
]
86+
})
87+
```
88+
89+
Input:
90+
91+
```css
92+
.app-body {
93+
background-image: url('./imgs/background.png');
94+
}
95+
```
96+
97+
Output:
98+
99+
```css
100+
.app-body {
101+
background-image: url('./assets/background.png');
102+
}
103+
```
104+
105+
#### Note: Remember that you should specify the publicPath option since we know nothing about your structure of your project directories, and also we don't know where you want to output your css file. If you don't specify the publicPath option, we would use the assetsPath option to be your url prefix, and obviously, it is normally not what you want.
106+
107+
## Inline
108+
```javascript
109+
postcss({
110+
plugins: [
111+
require({
112+
url: 'inline',
113+
})
114+
]
115+
})
116+
```
117+
118+
Input:
119+
120+
```css
121+
.app-body {
122+
background-image: url('./imgs/background.png');
123+
}
124+
```
125+
126+
Output:
127+
128+
```css
129+
.app-body {
130+
background-image: url('data:image/png;base64,<base64>');
131+
}
132+
```
133+
134+
# Options
135+
option | description | default
136+
---- | ---- | ----
137+
url | determines how to handle the url, "copy" or "inline". require assetsPath | "inline"
138+
assetsPath | where the assets should be copy to. | undefined
139+
publicPath | the prefix of output url | undefined
140+
extensions | determines which type of files should be handle | all
141+
include | determines where to search for assets | all
142+
exclude | determines which folder should be exclided | none
143+
hash | use hash to be the asset's name | false
144+
145+
See [PostCSS] docs for examples for your environment.

package.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "postcss-file",
3+
"version": "0.0.1",
4+
"description": "PostCSS plugin for handle files and assets",
5+
"main": "dist/index.js",
6+
"keywords": [
7+
"postcss",
8+
"css",
9+
"postcss-plugin",
10+
"file",
11+
"url",
12+
"asset"
13+
],
14+
"author": "shelton dong <dongxu2048@gmail.com>",
15+
"license": "MIT",
16+
"repository": "git@github.com:DongShelton/postcss-file.git",
17+
"scripts": {
18+
"build": "rollup -c",
19+
"test": "jest",
20+
"test:coverage": "jest --coverage",
21+
"lint": "tslint -p tsconfig.json"
22+
},
23+
"bugs": {
24+
"url": "https://github.com/DongShelton/postcss-file/issues"
25+
},
26+
"homepage": "https://github.com/DongShelton/postcss-file",
27+
"dependencies": {
28+
"chalk": "^2.3.1",
29+
"mkdirp": "^0.5.1",
30+
"postcss": "^6.0.16"
31+
},
32+
"devDependencies": {
33+
"@types/chalk": "^2.2.0",
34+
"@types/jest": "^22.1.3",
35+
"@types/mkdirp": "^0.5.2",
36+
"@types/node": "^9.4.6",
37+
"jest": "^22.4.0",
38+
"pre-commit": "^1.2.2",
39+
"ts-jest": "^22.0.4",
40+
"tslint": "^5.9.1",
41+
"typescript": "^2.7.2",
42+
"rollup": "^0.54.1",
43+
"rollup-plugin-clear": "^2.0.7",
44+
"rollup-plugin-commonjs": "^8.2.6",
45+
"rollup-plugin-filesize": "^1.5.0",
46+
"rollup-plugin-node-resolve": "^3.0.2",
47+
"rollup-plugin-progress": "^0.4.0",
48+
"rollup-plugin-typescript2": "^0.10.0"
49+
},
50+
"pre-commit": [
51+
"lint",
52+
"test"
53+
],
54+
"jest": {
55+
"verbose": true,
56+
"transform": {
57+
"^.+\\.ts$": "ts-jest"
58+
},
59+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(ts)$",
60+
"moduleFileExtensions": [
61+
"ts",
62+
"js",
63+
"json",
64+
"node"
65+
]
66+
}
67+
}

rollup.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import resolve from 'rollup-plugin-node-resolve';
2+
import typescript from 'rollup-plugin-typescript2';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import clear from 'rollup-plugin-clear';
5+
6+
const config = {
7+
input: './src/index.ts',
8+
output: {
9+
file: './dist/index.js',
10+
format: 'cjs',
11+
},
12+
plugins: [
13+
clear({
14+
targets: ['./dist'],
15+
}),
16+
commonjs(),
17+
resolve(),
18+
typescript(),
19+
],
20+
external: Object.keys(require('./package.json').dependencies),
21+
};
22+
23+
export default config;

src/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as postcss from 'postcss';
2+
import { Transformer } from 'postcss';
3+
import { shouldHandle } from './utils/filters';
4+
import { handleAsset } from './utils/urlHandler';
5+
import { validateOptions } from './utils/validators';
6+
7+
export type URL = 'copy' | 'inline';
8+
9+
export interface PostcssFileOptions {
10+
extensions?: string[];
11+
include?: string[];
12+
exclude?: string[];
13+
url?: URL;
14+
assetsPath?: string;
15+
publicPath?: string;
16+
hash?: boolean;
17+
}
18+
19+
export default postcss.plugin<PostcssFileOptions>('postcss-file', (options): Transformer => {
20+
// it would throw error if options is invalid.
21+
validateOptions(options);
22+
// initialize options
23+
const opts: PostcssFileOptions =
24+
options && Object.prototype.isPrototypeOf(options) ? options : {
25+
url: 'inline'
26+
};
27+
28+
return function(root) {
29+
root.walkDecls(/(background|src)/, function(decl) {
30+
// test whether there is url value
31+
if (!/url/.test(decl.value)) {
32+
return;
33+
}
34+
// test whether the value of url is valid
35+
const urlReg = /(\"|\')(.+?)(\1)/;
36+
const matchs: RegExpMatchArray | null = decl.value.match(urlReg);
37+
if (matchs === null) {
38+
return;
39+
}
40+
// the file of url
41+
const file = matchs[2];
42+
// test whether the asset is included
43+
const handleOptions = {
44+
include: opts.include,
45+
exclude: opts.exclude,
46+
extensions: opts.extensions,
47+
file
48+
};
49+
if (!shouldHandle(handleOptions)) {
50+
return;
51+
}
52+
// handle asset
53+
const urlValue = handleAsset({
54+
...opts,
55+
importer: decl.source.input.file,
56+
file
57+
});
58+
// overwrite the url value
59+
decl.value = `url('${urlValue}')`;
60+
});
61+
};
62+
}
63+
);

src/utils/filters.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as path from 'path';
2+
3+
/**
4+
* Whether the file is included in extensions.
5+
*
6+
* @param exts ".jpg", ".png", etc.
7+
* @param file
8+
*/
9+
const extFilter = (exts: string[], file: string): boolean => {
10+
const ext = path.extname(file);
11+
return exts.some((value: string): boolean => {
12+
return value === ext;
13+
});
14+
};
15+
16+
/**
17+
* Whether the file is included in files
18+
*
19+
* @param files paths
20+
* @param file
21+
*/
22+
const fileFilter = (files: string[], file: string): boolean => {
23+
const rootDir = process.cwd();
24+
const regs = files.map<RegExp>((value: string): RegExp => {
25+
const regStr = path.resolve(rootDir, value).replace(/\*\*/g, '.+?').replace(/\*/g, '[^\/]+?');
26+
return new RegExp(regStr);
27+
});
28+
return regs.some((value: RegExp) => {
29+
return value.test(path.resolve(rootDir, file));
30+
});
31+
};
32+
33+
export interface ShouldHandleOptions {
34+
include?: string[];
35+
exclude?: string[];
36+
extensions?: string[];
37+
file: string;
38+
}
39+
40+
/**
41+
* Whether the file should be handle
42+
*
43+
* @param options
44+
*/
45+
export const shouldHandle = (options: ShouldHandleOptions): boolean => {
46+
switch (true) {
47+
case options.include && !fileFilter(options.include, options.file):
48+
return false;
49+
case options.extensions && !extFilter(options.extensions, options.file):
50+
return false;
51+
case options.exclude && fileFilter(options.exclude, options.file):
52+
return false;
53+
default:
54+
return true;
55+
}
56+
};

0 commit comments

Comments
 (0)