Skip to content

Commit 22dd51f

Browse files
committed
refactory preservePath resolution
1 parent e3c35e2 commit 22dd51f

File tree

3 files changed

+50
-97
lines changed

3 files changed

+50
-97
lines changed

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Sections |
1313
[Options](#options) |
1414
[Custom Hash Function](#custom-hash-function) |
1515
[Transform](#using-transform) |
16-
[Preserving paths](#preserving-paths) |
1716
[Using postcss-import](#using-postcss-import) |
1817
[About lifecyle and the fileMeta object](#lifecyle) |
1918
[Roadmap](#roadmap) |
@@ -25,11 +24,26 @@ Sections |
2524
With [npm](https://npmjs.com/package/postcss-copy) do:
2625

2726
```
28-
npm install postcss-copy
27+
$ npm install postcss-copy
2928
```
3029

3130
## <a name="quick-start"></a> Quick Start
3231

32+
### Using [postcss-cli](https://github.com/postcss/postcss-cli)
33+
```js
34+
// postcss.config.js
35+
module.exports = {
36+
plugins: [
37+
require('postcss-copy')({
38+
dest: 'dist'
39+
})
40+
]
41+
};
42+
```
43+
```bash
44+
$ postcss src/index.css
45+
```
46+
3347
### Using [Gulp](https://github.com/postcss/gulp-postcss)
3448

3549
```js
@@ -40,13 +54,13 @@ var postcssCopy = require('postcss-copy');
4054
gulp.task('buildCss', function () {
4155
var processors = [
4256
postcssCopy({
43-
basePath: 'src',
57+
basePath: ['src', 'otherSrc']
4458
dest: 'dist'
4559
})
4660
];
4761

4862
return gulp
49-
.src('src/**/*.css')
63+
.src(['src/**/*.css', 'otherSrc/**/*.css'])
5064
.pipe(postcss(processors))
5165
.pipe(gulp.dest('dist'));
5266
});
@@ -81,7 +95,8 @@ var copyOpts = {
8195
```
8296

8397
#### preservePath ({boolean} default = false)
84-
Flag option that can be used to preserve the directory structure of your assets
98+
Flag option to notify to postcss-copy that your CSS files destination are going to preserve the directory structure.
99+
It's helpful if you are using `postcss-cli` with the --base option or `gulp-postcss` with multiple files (e.g: `gulp.src('src/**/*.css')`)
85100

86101
#### ignore ({string | string[] | function} default = [])
87102
Option to ignore assets in your CSS file.
@@ -170,15 +185,6 @@ var copyOpts = {
170185
};
171186
```
172187

173-
#### <a name="preserving-paths"></a> Preserving paths
174-
Some tools of postcss like postcss-cli or gulp-postcss can be used to preserve the relative paths of each src CSS files.
175-
There is some conditions in postcss-copy where we keep the structure directories and others where we not based on the `to` and `from` postcss options.
176-
177-
These are the rules:
178-
- If the `to` option is not set, postcss-copy is going to use the `dest` path and will not preserve the original path.
179-
- If the `to` option is set and is the same as `from` (e.g: gulp-postcss do that by default) is going to preserve the directory structure.
180-
- If you are running postcss for multiple files (e.g: in gulp-postcss `gulp.src('src/**/*.css')`) and `to` option is set, you have to change `preservePath` to true.
181-
182188
#### <a name="using-postcss-import"></a> Using copy with postcss-import
183189
[postcss-import](https://github.com/postcss/postcss-import) is a great plugin that allow us work our css files in a modular way with the same behavior of CommonJS.
184190

@@ -252,7 +258,7 @@ The lifecyle of the copy process is:
252258
10. Define template for the new asset
253259
11. Add ```resultAbsolutePath``` and ```extra``` properties in the fileMeta object
254260
12. Write in destination
255-
13. Write the new URL in the PostCSS node value. ([read more about preserving paths](#preserving-paths))
261+
13. Write the new URL in the PostCSS node value.
256262

257263
## <a name="roadmap"></a> On roadmap
258264

src/__tests__/common-tests.json

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -227,73 +227,4 @@
227227
],
228228
"no-modified": "assets/0ed7c955a2951f04.jpg"
229229
}
230-
}, {
231-
"name": "template: 'assets/[hash].[ext][query]', with postcss.to && preservePath === false",
232-
"opts": {
233-
"template": "assets/[hash].[ext][query]",
234-
"to": "styles/index.css"
235-
},
236-
"assertions": {
237-
"index": [{
238-
"desc": "@index.css => process url image (simple)",
239-
"match": "../assets/b6c8f21e92b50900.jpg"
240-
}, {
241-
"desc": "@index.css => process url image (with parameters)",
242-
"match": "../assets/0ed7c955a2951f04.jpg?#iefix&v=4.4.0"
243-
}, {
244-
"desc": "@index.css => extra rules in image (simple)",
245-
"match": "url('../assets/b6c8f21e92b50900.jpg') center center",
246-
"regex-simple": true
247-
}],
248-
"component": [],
249-
"external_libs": [],
250-
"exists": [
251-
"assets/b6c8f21e92b50900.jpg",
252-
"assets/0ed7c955a2951f04.jpg"
253-
],
254-
"no-modified": "assets/0ed7c955a2951f04.jpg"
255-
}
256-
}, {
257-
"name": "template: '[path]/[name].[ext][query]', with postcss.to == postcss.from",
258-
"opts": {
259-
"template": "[path]/[name].[ext][query]",
260-
"to": "equal-from"
261-
},
262-
"assertions": {
263-
"index": [],
264-
"component": [{
265-
"desc": "@component/index.css => process url image (simple)",
266-
"match": "images/component.jpg"
267-
}, {
268-
"desc": "@component/index.css => process url image (with parameters)",
269-
"match": "../images/test.jpg?#iefix&v=4.4.0"
270-
}],
271-
"external_libs": [],
272-
"exists": [
273-
"component/images/component.jpg",
274-
"images/test.jpg"
275-
]
276-
}
277-
}, {
278-
"name": "template: '[path]/[name].[ext][query]', with preservePath === true",
279-
"opts": {
280-
"template": "[path]/[name].[ext][query]",
281-
"preservePath": true,
282-
"to": "component/index.css"
283-
},
284-
"assertions": {
285-
"index": [],
286-
"component": [{
287-
"desc": "@component/index.css => process url image (simple)",
288-
"match": "images/component.jpg"
289-
}, {
290-
"desc": "@component/index.css => process url image (with parameters)",
291-
"match": "../images/test.jpg?#iefix&v=4.4.0"
292-
}],
293-
"external_libs": [],
294-
"exists": [
295-
"component/images/component.jpg",
296-
"images/test.jpg"
297-
]
298-
}
299230
}]

src/index.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,43 @@ function findBasePath(paths, pathname) {
5656
* @returns {string}
5757
*/
5858
function defineCSSDestPath(dirname, fileMeta, result, opts) {
59-
if (!result.opts.to) {
60-
return opts.dest;
61-
}
62-
6359
const from = path.resolve(result.opts.from);
64-
const to = path.resolve(result.opts.to);
60+
let to;
6561

66-
if (to === from || opts.preservePath && dirname === path.dirname(from)) {
62+
if (result.opts.to) {
6763
/**
68-
* if to === results.opts.from we can't use it as a valid dest path
64+
* if to === from we can't use it as a valid dest path
6965
* e.g: gulp-postcss comes with this problem
7066
*
71-
* if dirname === path.dirname(result.opts.from) with this
72-
* condition we can check if the postcss-copy runs after postcss-import
73-
* and if preservePath is activated preserve the structure of
74-
* the assets directory
7567
*/
76-
return path.join(opts.dest, path.relative(fileMeta.basePath, dirname));
68+
to = path.resolve(result.opts.to);
69+
to = to === from ? opts.dest : path.dirname(to);
70+
} else {
71+
to = opts.dest;
72+
}
73+
74+
if (opts.preservePath) {
75+
let srcPath;
76+
let basePath;
77+
78+
if (dirname === path.dirname(from)) {
79+
srcPath = dirname;
80+
basePath = fileMeta.basePath;
81+
} else {
82+
/**
83+
* dirname !== path.dirname(result.opts.from) means that
84+
* postcss-import is grouping different css files in
85+
* only one destination, so, the relative path must be defined
86+
* based on the CSS file where we read the @imports
87+
*/
88+
srcPath = path.dirname(from);
89+
basePath = findBasePath(opts.basePath, from);
90+
}
91+
92+
return path.join(to, path.relative(basePath, srcPath));
7793
}
7894

79-
return path.dirname(to);
95+
return to;
8096
}
8197

8298
/**

0 commit comments

Comments
 (0)