mirror of
https://github.com/zhigang1992/postcss-partial-process.git
synced 2026-01-12 17:33:02 +08:00
add 'auto' value to removeComments + change default option values + add tests
This commit is contained in:
32
CHANGELOG.md
32
CHANGELOG.md
@@ -1,5 +1,29 @@
|
||||
# Change Log
|
||||
This project adheres to [Semantic Versioning](http://semver.org/).
|
||||
# Changelog
|
||||
|
||||
## [1.0.0] - 2018-05-03
|
||||
- First release
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [2.0.0] - 2019-04-04
|
||||
|
||||
### Added
|
||||
- `removeComments` can now be set to `'auto'`, meaning comments are removed unless they are important (e.g. `/*! postcss-partial-process start */`)
|
||||
- More tests
|
||||
|
||||
### Changed
|
||||
- Default values have changed for the following options:
|
||||
- `startComment` is now `postcss-partial-process start`
|
||||
- `endComment` is now `postcss-partial-process end`
|
||||
- `removeComments` is now `'auto'`
|
||||
- Updated dependencies
|
||||
|
||||
### Fixed
|
||||
- Important comments are now detected properly
|
||||
|
||||
## 1.0.0 - 2018-05-03
|
||||
|
||||
Initial release
|
||||
|
||||
[Unreleased]: https://github.com/benface/postcss-partial-process/compare/v2.0.0...HEAD
|
||||
[2.0.0]: https://github.com/benface/postcss-partial-process/compare/v1.0.0...v2.0.0
|
||||
|
||||
26
README.md
26
README.md
@@ -2,19 +2,27 @@
|
||||
|
||||
[PostCSS] plugin that runs other plugins on parts of a CSS file delimited by comments.
|
||||
|
||||
[PostCSS]: https://github.com/postcss/postcss
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install postcss-partial-process
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
postcss([ require('postcss-partial-process')({
|
||||
startComment: 'start postcss-partial-process', // default value
|
||||
endComment: 'end postcss-partial-process', // default value
|
||||
removeComments: true, // false by default
|
||||
plugins = [
|
||||
require('[any postcss plugin you want]'), // this will only affect the CSS between the comments "start postcss-partial-process" and "end postcss-partial-process"
|
||||
],
|
||||
}) ])
|
||||
postcss([
|
||||
require('postcss-partial-process')({
|
||||
startComment: 'postcss-partial-process start', // default
|
||||
endComment: 'postcss-partial-process end', // default
|
||||
removeComments: 'auto', // default, means the comments are removed unless they are important (e.g. /*! like this */); also accepts `true` or `false`
|
||||
plugins = [
|
||||
require('some postcss plugin'), // this plugin will only run between the comments "postcss-partial-process start" and "postcss-partial-process end"
|
||||
],
|
||||
}),
|
||||
])
|
||||
```
|
||||
|
||||
See [PostCSS] docs for examples for your environment.
|
||||
|
||||
[PostCSS]: https://github.com/postcss/postcss
|
||||
|
||||
17
index.js
17
index.js
@@ -1,9 +1,9 @@
|
||||
var postcss = require('postcss');
|
||||
const postcss = require('postcss');
|
||||
|
||||
module.exports = postcss.plugin('postcss-partial-process', ({
|
||||
startComment = 'start postcss-partial-process',
|
||||
endComment = 'end postcss-partial-process',
|
||||
removeComments = false,
|
||||
startComment = 'postcss-partial-process start',
|
||||
endComment = 'postcss-partial-process end',
|
||||
removeComments = 'auto',
|
||||
plugins = []
|
||||
} = {}) => {
|
||||
return root => {
|
||||
@@ -22,20 +22,23 @@ module.exports = postcss.plugin('postcss-partial-process', ({
|
||||
root.each(node => {
|
||||
let starting = false;
|
||||
let ending = false;
|
||||
let removeComment = false;
|
||||
|
||||
if (node.type === 'comment') {
|
||||
let matches = node.toString().match(/\/\*\s*(.*?)\s*\*\//, '');
|
||||
let comment = node.toString();
|
||||
let matches = comment.match(/\/\*!?\s*(.*?)\s*\*\//, '');
|
||||
matches = matches || [];
|
||||
if (matches.length > 1) {
|
||||
starting = !processing && matches[1] === startComment;
|
||||
ending = processing && matches[1] === endComment;
|
||||
removeComment = removeComments === true || (removeComments === 'auto' && !comment.startsWith('/*!'));
|
||||
}
|
||||
}
|
||||
|
||||
if (ending) {
|
||||
end(result => {
|
||||
node.before(result.root);
|
||||
if (removeComments) {
|
||||
if (removeComment) {
|
||||
node.remove();
|
||||
}
|
||||
});
|
||||
@@ -48,7 +51,7 @@ module.exports = postcss.plugin('postcss-partial-process', ({
|
||||
|
||||
if (starting) {
|
||||
partialRoot = postcss.root();
|
||||
if (removeComments) {
|
||||
if (removeComment) {
|
||||
node.remove();
|
||||
}
|
||||
processing = true;
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
var postcss = require('postcss');
|
||||
|
||||
var plugin = require('./');
|
||||
|
||||
function run(input, output, opts) {
|
||||
return postcss([ plugin(opts) ]).process(input)
|
||||
.then(result => {
|
||||
expect(result.css).toEqual(output);
|
||||
expect(result.warnings().length).toBe(0);
|
||||
});
|
||||
}
|
||||
|
||||
it('compiles', () => {
|
||||
return run('a{ }', 'a{ }', { });
|
||||
});
|
||||
7162
package-lock.json
generated
7162
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
33
package.json
33
package.json
@@ -8,30 +8,21 @@
|
||||
"postcss-plugin"
|
||||
],
|
||||
"author": "Benoît Rouleau <benoit.rouleau@icloud.com>",
|
||||
"license": "MIT",
|
||||
"repository": "benface/postcss-partial-process",
|
||||
"bugs": {
|
||||
"url": "https://github.com/benface/postcss-partial-process/issues"
|
||||
},
|
||||
"license": "ISC",
|
||||
"repository": "https://github.com/benface/postcss-partial-process.git",
|
||||
"bugs": "https://github.com/benface/postcss-partial-process/issues",
|
||||
"homepage": "https://github.com/benface/postcss-partial-process",
|
||||
"scripts": {
|
||||
"test": "jest",
|
||||
"release": "f(){ release-it $1 && github-release-from-changelog ;};f"
|
||||
},
|
||||
"dependencies": {
|
||||
"postcss": "^6.0.16"
|
||||
"postcss": "^7.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^4.18.2",
|
||||
"eslint-config-postcss": "^2.0.2",
|
||||
"jest": "^22.4.2"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest && eslint *.js"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "eslint-config-postcss/es5",
|
||||
"env": {
|
||||
"jest": true
|
||||
}
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node"
|
||||
"github-release-from-changelog": "^1.3.2",
|
||||
"jest": "^24.7.1",
|
||||
"postcss-prepend-selector": "^0.3.1",
|
||||
"release-it": "^10.4.0"
|
||||
}
|
||||
}
|
||||
|
||||
120
test.js
Normal file
120
test.js
Normal file
@@ -0,0 +1,120 @@
|
||||
const postcss = require('postcss');
|
||||
const partialProcess = require('./');
|
||||
const prependSelector = require('postcss-prepend-selector');
|
||||
|
||||
function run(input, output, opts) {
|
||||
return postcss([
|
||||
partialProcess(opts),
|
||||
])
|
||||
.process(input)
|
||||
.then(result => {
|
||||
expect(result.css).toEqual(output);
|
||||
expect(result.warnings().length).toBe(0);
|
||||
});
|
||||
}
|
||||
|
||||
test('it compiles', () => {
|
||||
return run('a {}', 'a {}', {});
|
||||
});
|
||||
|
||||
test('rules inside the comments are processed', () => {
|
||||
return run(`
|
||||
/* postcss-partial-process start */
|
||||
a {}
|
||||
b {}
|
||||
/* postcss-partial-process end */
|
||||
`, `
|
||||
#a a {}
|
||||
#a b {}
|
||||
`, {
|
||||
plugins: [
|
||||
prependSelector({ selector: '#a ' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('rules outside the comments are not processed', () => {
|
||||
return run(`
|
||||
body {}
|
||||
/* postcss-partial-process start */
|
||||
a {}
|
||||
/* postcss-partial-process end */
|
||||
b {}
|
||||
a {}
|
||||
`, `
|
||||
body {}
|
||||
#a a {}
|
||||
b {}
|
||||
a {}
|
||||
`, {
|
||||
plugins: [
|
||||
prependSelector({ selector: '#a ' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('start/end comments can be customized', () => {
|
||||
return run(`
|
||||
/* yo start */
|
||||
.test { hello: world; }
|
||||
/* yo stop */
|
||||
b {}
|
||||
`, `
|
||||
#a .test { hello: world; }
|
||||
b {}
|
||||
`, {
|
||||
startComment: 'yo start',
|
||||
endComment: 'yo stop',
|
||||
plugins: [
|
||||
prependSelector({ selector: '#a ' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('comments are not removed with removeComments: false', () => {
|
||||
return run(`
|
||||
/* postcss-partial-process start */
|
||||
a {}
|
||||
/* postcss-partial-process end */
|
||||
`, `
|
||||
/* postcss-partial-process start */
|
||||
#a a {}
|
||||
/* postcss-partial-process end */
|
||||
`, {
|
||||
removeComments: false,
|
||||
plugins: [
|
||||
prependSelector({ selector: '#a ' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('important comments are not removed by default', () => {
|
||||
return run(`
|
||||
/*! postcss-partial-process start */
|
||||
a {}
|
||||
/*! postcss-partial-process end */
|
||||
`, `
|
||||
/*! postcss-partial-process start */
|
||||
#a a {}
|
||||
/*! postcss-partial-process end */
|
||||
`, {
|
||||
plugins: [
|
||||
prependSelector({ selector: '#a ' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('important comments are removed with removeComments: true', () => {
|
||||
return run(`
|
||||
/*! postcss-partial-process start */
|
||||
a {}
|
||||
/*! postcss-partial-process end */
|
||||
`, `
|
||||
#a a {}
|
||||
`, {
|
||||
removeComments: true,
|
||||
plugins: [
|
||||
prependSelector({ selector: '#a ' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user