Skip to content

Commit 6cc8cc9

Browse files
author
dongxu.shelton
committed
fix: fix handle assets repetitively error
Change-Id: Ie8e19cd626ea71ae6769e0a1e44799b622bc3359
1 parent 9a02d99 commit 6cc8cc9

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
loader: 'postcss-loader',
2727
options: {
2828
plugins: [
29+
// other plugins ...
2930
require('postcss-file')({
3031
url: 'copy',
3132
assetsPath: 'dist/assets',
@@ -53,6 +54,7 @@ const config = {
5354
postcss({
5455
// ...
5556
plugins: [
57+
// other plugins ...
5658
require('postcss-file')({
5759
url: 'copy',
5860
assetsPath: 'dist/assets',
@@ -66,7 +68,7 @@ const config = {
6668

6769
export default config;
6870
```
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.
71+
#### 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. Additionally, for the full effects for your assets, please put PostCSS File plugin on the lastest of postcss plugins.
7072

7173
# Url Resolve
7274

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-file",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "PostCSS plugin for handle assets",
55
"main": "dist/index.js",
66
"keywords": [

src/index.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,55 @@ import { shouldHandle } from './utils/filters';
44
import { handleAsset } from './utils/urlHandler';
55
import { validateOptions } from './utils/validators';
66

7+
// Url type
78
export type URL = 'copy' | 'inline';
89

9-
export interface PostcssFileOptions {
10+
/**
11+
* PostCSS File plugin options
12+
*
13+
* @property extensions
14+
* @property include
15+
* @property exclude
16+
* @property url
17+
* @property assetsPath
18+
* @property publicPath
19+
* @property hash
20+
*/
21+
export interface PostcssFileOptions extends Object {
22+
/**
23+
* @default undefined
24+
* @description determines which type of files should be handle
25+
*/
1026
extensions?: string[];
27+
/**
28+
* @default undefined
29+
* @description determines where to search for assets
30+
*/
1131
include?: string[];
32+
/**
33+
* @default undefined
34+
* @description determines which folder should be exclided
35+
*/
1236
exclude?: string[];
37+
/**
38+
* @default "inline"
39+
* @description determines how to handle the url, "copy" or "inline". require assetsPath | "inline"
40+
*/
1341
url?: URL;
42+
/**
43+
* @default undefined
44+
* @description where the assets should be copy to.
45+
*/
1446
assetsPath?: string;
47+
/**
48+
* @default undefined
49+
* @description the prefix of output url
50+
*/
1551
publicPath?: string;
52+
/**
53+
* @default false
54+
* @description use hash to be the asset's name
55+
*/
1656
hash?: boolean;
1757
}
1858

@@ -26,7 +66,14 @@ export default postcss.plugin<PostcssFileOptions>('postcss-file', (options): Tra
2666
};
2767

2868
return function(root) {
69+
2970
root.walkDecls(/(background|src)/, function(decl) {
71+
// do nothing if this declaration is already have been handled
72+
if ((decl as any).handled) {
73+
return;
74+
}
75+
// set handled flag to fix the error, which would handle assets repetitively.
76+
(decl as any).handled = true;
3077
// test whether there is url value
3178
if (!/url/.test(decl.value)) {
3279
return;

src/utils/validators.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ const warn = (message: string) => {
55
console.warn(chalk.bold.yellow(message));
66
};
77

8+
/**
9+
* Check the options, and throw error if it's invalid.
10+
*
11+
* @param options
12+
* @throws Invalid options
13+
*/
814
export const validateOptions = (options: PostcssFileOptions | undefined) => {
915
if (!options) {
1016
return false;

0 commit comments

Comments
 (0)