Skip to content

Commit d4bddb7

Browse files
committed
Feat: add processJs
1 parent c1ca3bf commit d4bddb7

File tree

11 files changed

+495
-2
lines changed

11 files changed

+495
-2
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Let's exclude 4 classes and id's: `js`, `flexbox`, `canvas` and `svg`
207207
## Methods
208208

209209
- [processCss](#processcss)
210+
- [processJs](#processjs)
210211
- [process](#process)
211212
- [generateMapping](#generateMapping)
212213
- [loadMapping](#loadMapping)
@@ -241,6 +242,32 @@ rcs.processCss('**/*.css', options, err => {
241242
})
242243
```
243244

245+
### processJs
246+
247+
**process(src[, options], callback)**
248+
249+
> **Important!** processCss should run first, otherwise there are no minified selectors
250+
251+
Options:
252+
253+
- overwrite (boolean): ensures that it does not overwrite the same file accidently. Default is `false`
254+
- cwd (string): the working directory in which to seach. Default is `process.cwd()`
255+
- newPath (string): in which folder the new files should go. Default is `rcs`
256+
- flatten (boolean): flatten the hierarchie - no subfolders. Default is `false`
257+
- jsx (boolean): if the file is a react jsx file. Default is `false`
258+
259+
Example:
260+
261+
```js
262+
const rcs = require('rename-css-selectors')
263+
264+
rcs.processJs('**/*.js', options, err => {
265+
if (err) return console.error(err)
266+
267+
console.log('Successfully wrote new javascript files')
268+
})
269+
```
270+
244271
### process
245272

246273
**process(src[, options], callback)**

lib/process/process.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ const rcsProcess = (pathString, options, cb) => {
107107

108108
// check wether to replace javascript files or not
109109
if (options.replaceJs) {
110-
data = rcs.replace.bufferJs(bufferData);
110+
const isJsx = !!options.jsx;
111+
112+
data = rcs.replace.bufferJs(bufferData, { jsx: isJsx });
111113
} else {
112114
data = rcs.replace.buffer(bufferData);
113115
}

lib/process/processSync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const processSync = (pathString, options) => {
3939

4040
if (options.collectSelectors) {
4141
data = rcs.replace.bufferCss(fs.readFileSync(path.join(options.cwd, filePath)), options);
42-
} else if (options.replaceJS) {
42+
} else if (options.replaceJs) {
4343
data = rcs.replace.bufferJs(fs.readFileSync(path.join(options.cwd, filePath)), options);
4444
} else {
4545
data = rcs.replace.buffer(fs.readFileSync(path.join(options.cwd, filePath)), options);

lib/processJs/processJs.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const rcsProcess = require('../process/process');
4+
5+
/**
6+
* process over all css files - set and replace
7+
* does exactly the same as renameCssSelectors.process, but set the collectSelectors option to true
8+
*
9+
* @param {pathString} pathString this pathString can be either an expression for `glob` or a filepath
10+
* @param {processOptions} options
11+
* @param {Function} cb the callback
12+
* @return {Function} cb
13+
*/
14+
const processCss = (pathString, options, cb) => {
15+
// set cb if options are not set
16+
if (typeof cb !== 'function') {
17+
cb = options;
18+
options = {};
19+
}
20+
21+
// set the css power for renameCssSelectors.process
22+
options.replaceJs = true;
23+
24+
rcsProcess(pathString, options, cb);
25+
} // /processCss
26+
27+
module.exports = processCss;

lib/processJs/processJsSync.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const processSync = require('../process/processSync');
4+
5+
/**
6+
* The ansynchronous method for processCss
7+
*/
8+
const processJsSync = (pathString, options) => {
9+
options = options || {};
10+
11+
options.replaceJs = true;
12+
13+
processSync(pathString, options);
14+
} // /processJsSync
15+
16+
module.exports = processJsSync;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'use strict';
2+
3+
const processJs = require('../processJs');
4+
5+
const fs = require('fs-extra');
6+
const rcs = require('rcs-core');
7+
const json = require('json-extra');
8+
const expect = require('chai').expect;
9+
10+
const testCwd = 'test/files/testCache';
11+
const fixturesCwd = 'test/files/fixtures';
12+
const resultsCwd = 'test/files/results';
13+
14+
describe('processJs/processJs', () => {
15+
before(() => {
16+
rcs.nameGenerator.resetCountForTests();
17+
rcs.selectorLibrary.fillLibrary(fs.readFileSync(fixturesCwd + '/css/style.css', 'utf8'))
18+
});
19+
20+
afterEach(() => {
21+
fs.removeSync(testCwd);
22+
});
23+
24+
it('should process js files', done => {
25+
processJs('js/main.txt', {
26+
newPath: testCwd,
27+
cwd: fixturesCwd
28+
}, err => {
29+
let newFile = fs.readFileSync(testCwd + '/js/main.txt', 'utf8');
30+
let expectedFile = fs.readFileSync(resultsCwd + '/js/main.txt', 'utf8');
31+
32+
expect(err).to.not.exist;
33+
expect(newFile).to.equal(expectedFile);
34+
35+
done();
36+
});
37+
});
38+
39+
it('should process jsx files', done => {
40+
processJs('js/react.txt', {
41+
newPath: testCwd,
42+
cwd: fixturesCwd,
43+
jsx: true,
44+
}, err => {
45+
let newFile = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
46+
let expectedFile = fs.readFileSync(resultsCwd + '/js/react.txt', 'utf8');
47+
48+
expect(err).to.not.exist;
49+
expect(newFile).to.equal(expectedFile);
50+
51+
done();
52+
});
53+
});
54+
55+
it('should not process jsx files', done => {
56+
processJs('js/react.txt', {
57+
newPath: testCwd,
58+
cwd: fixturesCwd,
59+
}, err => {
60+
let newFile = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
61+
let expectedFile = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
62+
63+
expect(err).to.not.exist;
64+
expect(newFile).to.equal(expectedFile);
65+
66+
done();
67+
});
68+
});
69+
70+
it('should process complex files', done => {
71+
processJs('js/complex.txt', {
72+
newPath: testCwd,
73+
cwd: fixturesCwd,
74+
}, err => {
75+
let newFile = fs.readFileSync(testCwd + '/js/complex.txt', 'utf8');
76+
let expectedFile = fs.readFileSync(testCwd + '/js/complex.txt', 'utf8');
77+
78+
expect(err).to.not.exist;
79+
expect(newFile).to.equal(expectedFile);
80+
81+
done();
82+
});
83+
});
84+
85+
it('should not process multiple files', done => {
86+
processJs('js/*.txt', {
87+
newPath: testCwd,
88+
cwd: fixturesCwd,
89+
jsx: true,
90+
}, err => {
91+
let newFile = fs.readFileSync(testCwd + '/js/complex.txt', 'utf8');
92+
let newFileTwo = fs.readFileSync(testCwd + '/js/main.txt', 'utf8');
93+
let newFileThree = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
94+
let expectedFile = fs.readFileSync(resultsCwd + '/js/complex.txt', 'utf8');
95+
let expectedFileTwo = fs.readFileSync(resultsCwd + '/js/main.txt', 'utf8');
96+
let expectedFileThree = fs.readFileSync(resultsCwd + '/js/react.txt', 'utf8');
97+
98+
expect(err).to.not.exist;
99+
expect(newFile).to.equal(expectedFile);
100+
expect(newFileTwo).to.equal(expectedFileTwo);
101+
expect(newFileThree).to.equal(expectedFileThree);
102+
103+
done();
104+
});
105+
});
106+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
const processJsSync = require('../processJsSync');
4+
5+
const fs = require('fs-extra');
6+
const rcs = require('rcs-core');
7+
const json = require('json-extra');
8+
const expect = require('chai').expect;
9+
10+
const testCwd = 'test/files/testCache';
11+
const fixturesCwd = 'test/files/fixtures';
12+
const resultsCwd = 'test/files/results';
13+
14+
describe('processJsSync/processJsSync', () => {
15+
before(() => {
16+
rcs.nameGenerator.resetCountForTests();
17+
rcs.selectorLibrary.fillLibrary(fs.readFileSync(fixturesCwd + '/css/style.css', 'utf8'))
18+
});
19+
20+
afterEach(() => {
21+
fs.removeSync(testCwd);
22+
});
23+
24+
it('should process js files', () => {
25+
processJsSync('js/main.txt', {
26+
newPath: testCwd,
27+
cwd: fixturesCwd
28+
});
29+
30+
let newFile = fs.readFileSync(testCwd + '/js/main.txt', 'utf8');
31+
let expectedFile = fs.readFileSync(resultsCwd + '/js/main.txt', 'utf8');
32+
33+
expect(newFile).to.equal(expectedFile);
34+
});
35+
36+
it('should process jsx files', () => {
37+
processJsSync('js/react.txt', {
38+
newPath: testCwd,
39+
cwd: fixturesCwd,
40+
jsx: true,
41+
});
42+
43+
let newFile = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
44+
let expectedFile = fs.readFileSync(resultsCwd + '/js/react.txt', 'utf8');
45+
46+
expect(newFile).to.equal(expectedFile);
47+
});
48+
49+
it('should not process jsx files', () => {
50+
processJsSync('js/react.txt', {
51+
newPath: testCwd,
52+
cwd: fixturesCwd,
53+
});
54+
55+
let newFile = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
56+
let expectedFile = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
57+
58+
expect(newFile).to.equal(expectedFile);
59+
});
60+
61+
it('should process complex files', () => {
62+
processJsSync('js/complex.txt', {
63+
newPath: testCwd,
64+
cwd: fixturesCwd,
65+
});
66+
67+
let newFile = fs.readFileSync(testCwd + '/js/complex.txt', 'utf8');
68+
let expectedFile = fs.readFileSync(testCwd + '/js/complex.txt', 'utf8');
69+
70+
expect(newFile).to.equal(expectedFile);
71+
});
72+
73+
it('should not process multiple files', () => {
74+
processJsSync('js/*.txt', {
75+
newPath: testCwd,
76+
cwd: fixturesCwd,
77+
jsx: true,
78+
});
79+
80+
let newFile = fs.readFileSync(testCwd + '/js/complex.txt', 'utf8');
81+
let newFileTwo = fs.readFileSync(testCwd + '/js/main.txt', 'utf8');
82+
let newFileThree = fs.readFileSync(testCwd + '/js/react.txt', 'utf8');
83+
let expectedFile = fs.readFileSync(resultsCwd + '/js/complex.txt', 'utf8');
84+
let expectedFileTwo = fs.readFileSync(resultsCwd + '/js/main.txt', 'utf8');
85+
let expectedFileThree = fs.readFileSync(resultsCwd + '/js/react.txt', 'utf8');
86+
87+
expect(newFile).to.equal(expectedFile);
88+
expect(newFileTwo).to.equal(expectedFileTwo);
89+
expect(newFileThree).to.equal(expectedFileThree);
90+
});
91+
});

test/files/fixtures/js/complex.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Finds the index of the first character
3+
* that's not common between the two given strings.
4+
*
5+
* @return {number} the index of the character where the strings diverge
6+
*/
7+
function firstDifferenceIndex(string1, string2) {
8+
var minLen = Math.min(string1.length, string2.length);
9+
for (var i = 0; i < minLen; i++) {
10+
if (string1.charAt(i) !== string2.charAt(i)) {
11+
return i;
12+
}
13+
}
14+
return string1.length === string2.length ? -1 : minLen;
15+
}
16+
17+
/***/ (function(module, exports) {
18+
19+
// removed by extract-text-webpack-plugin
20+
module.exports = {"table":"jp-block","key":"jp-pseudo"};
21+
22+
/***/ }),
23+
/* 523 */
24+
/***/ (function(module, exports, __webpack_require__) {
25+
26+
var defineProperty = __webpack_require__(531);
27+
28+
/**
29+
* The base implementation of `assignValue` and `assignMergeValue` without
30+
* value checks.
31+
*
32+
* @private
33+
* @param {Object} object The object to modify.
34+
* @param {string} key The key of the property to assign.
35+
* @param {*} value The value to assign.
36+
*/
37+
function baseAssignValue(object, key, value) {
38+
if (key == '__proto__' && defineProperty) {
39+
defineProperty(object, key, {
40+
'configurable': true,
41+
'enumerable': true,
42+
'value': value,
43+
'writable': true
44+
});
45+
} else {
46+
object[key] = value;
47+
}
48+
}
49+
50+
module.exports = baseAssignValue;
51+
52+
53+
/***/ })

0 commit comments

Comments
 (0)