Skip to content

Commit ed07f45

Browse files
committed
feat loadMapping to load previous generated mappings
1 parent eee3696 commit ed07f45

File tree

3 files changed

+168
-11
lines changed

3 files changed

+168
-11
lines changed

README.md

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
This module renames all CSS selectors in the given files. It will collect all selectors from the given CSS files. Do not worry about your selectors, `rcs` will do it for you.
77

8-
**NOT YET IMPLEMENTED** You can also use a config file, if you already had other projects with the same classes. So all your projects have the same minified selector names - always.
8+
You can also use a config file with the combination of [generateMapping](#generateMapping) and [loadMapping](#loadMapping), if you already had other projects with the same classes. So all your projects have the same minified selector names - always.
99

1010
## Contents
1111

@@ -107,7 +107,8 @@ Let's exclude 4 classes and id's: `js`, `flexbox`, `canvas` and `svg`
107107

108108
- [processCss](#processcss)
109109
- [process](#process)
110-
- [generateLibraryFile](#generatelibraryfile)
110+
- [generateMapping](#generateMapping)
111+
- [loadMapping](#loadMapping)
111112
- [includeConfig](#includeconfig)
112113

113114
### processCss
@@ -168,26 +169,26 @@ rcs.process('**/*.js', options, err => {
168169
}
169170
```
170171
171-
### generateLibraryFile
172+
### generateMapping
172173
173-
**generateLibraryFile(pathLocation[, options], callback)**
174+
**generateMapping(pathLocation[, options], callback)**
174175
175-
> *Note:* if you are using the options either cssMapping or cssMappingMin must be set to true
176+
> *Note:* if you are using the options either `cssMapping` or `cssMappingMin must be set to true. Both to `true` at the same time are not valid.
176177

177178
Generates mapping files: all minified, all original selectors or both. They are stored as object in a variable. The file is named as `renaming_map.js` or `renaming_map_min.js`.
178179

179180
Options:
180181

181-
- cssMapping (boolean): writes `renaming_map.js`. Default is `true`
182-
- cssMappingMin (boolean): writes `renaming_map_min.js`. Default is `false`
182+
- cssMapping (string | boolean): writes `renaming_map.js`. If it is a string, the string is the new file name. Default is `true`
183+
- cssMappingMin (string | boolean): writes `renaming_map_min.js`. If it is a string, the string is the new file name. Default is `false`
183184
- extended (boolean): instead of a string it writes an object with meta information. Default is `false`
184-
- **NOT YET IMPLEMENTED** json (boolean): writes an json instead of a js. Default is `false`
185+
- json (boolean): writes an json instead of a js. Default is `true`
185186

186187
```js
187188
const rcs = require('rename-css-selectors')
188189
189190
// the same for html or other files
190-
rcs.generateLibraryFile('./mappings', options, err => {
191+
rcs.generateMapping('./mappings', options, err => {
191192
if (err) return console.error(err)
192193
193194
console.log('Successfully wrote mapping files');
@@ -198,11 +199,36 @@ Output in `renaming_map_min.js`:
198199

199200
```js
200201
var CSS_NAME_MAPPING_MIN = {
201-
'e': 'any-class',
202-
't': 'another-class'
202+
'.e': 'any-class',
203+
'.t': 'another-class'
203204
};
204205
```
205206

207+
### loadMapping
208+
209+
**loadMapping(pathToMapping[, options])**
210+
211+
> *Note:* If you include a file, it **MUST** be the json generated mapping.
212+
213+
Loads the previous generated mapping. This ensures that all your projects have all the time the same renamed selectors.
214+
215+
Options:
216+
217+
- origValues (boolean): Wether the cssMappingMin (`false`) or cssMapping (`true`) should get loaded. Default is `true`
218+
219+
```js
220+
const rcs = require('rename-css-selectors')
221+
222+
// loadMapping is synchronous
223+
// the first parameter can be either a string to the file
224+
// or the json object directly
225+
rcs.loadMapping('./renaming_map_min.json', options);
226+
227+
rcs.process('**/*.html', err => {
228+
...
229+
});
230+
```
231+
206232
### includeConfig
207233

208234
**includeConfig([pathLocation])**

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,37 @@ renameCssSelectors.generateMapping = (pathString, options, cb) => {
230230
}; // /generateMapping
231231

232232
renameCssSelectors.loadMapping = (pathString, options) => {
233+
let selectors = pathString;
233234

235+
const optionsDefault = {
236+
origValues: true
237+
}
238+
239+
options = options || {};
240+
options = _.merge(optionsDefault, options);
241+
242+
if (typeof pathString === 'string') {
243+
selectors = json.readToObjSync(pathString, 'utf8');
244+
}
245+
246+
if (!options.origValues) {
247+
let tempSelectors = {};
248+
249+
for (let key in selectors) {
250+
let value = selectors[key];
251+
let modKey = key.slice(1, key.length);
252+
253+
tempSelectors[key.charAt(0) + value] = modKey;
254+
}
255+
256+
selectors = tempSelectors;
257+
}
258+
259+
if (!selectors || typeof selectors !== 'object') {
260+
return;
261+
}
262+
263+
rcs.selectorLibrary.setValues(selectors);
234264
}; // /loadMapping
235265

236266
/**

test/index.spec.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,107 @@ describe('app.js', () => {
258258
});
259259
});
260260

261+
describe('load mapping', () => {
262+
beforeEach(done => {
263+
app.processCss('**/*.css', {
264+
newPath: testCwd,
265+
cwd: fixturesCwd
266+
}, (err, data) => {
267+
app.generateMapping(testCwd, (err, data) => {
268+
rcs.selectorLibrary.selectors = {};
269+
rcs.selectorLibrary.compressedSelectors = {};
270+
rcs.selectorLibrary.excludes = [];
271+
rcs.nameGenerator.resetCountForTests();
272+
273+
done();
274+
});
275+
});
276+
});
277+
278+
it('should load from an object', done => {
279+
const cssMapping = json.readToObjSync(testCwd + '/renaming_map.json', 'utf8');
280+
281+
app.loadMapping(cssMapping);
282+
283+
app.process('**/*.html', {
284+
newPath: testCwd,
285+
cwd: fixturesCwd
286+
}, (err, data) => {
287+
let newFile = fs.readFileSync(testCwd + '/index.html', 'utf8');
288+
let expectedFile = fs.readFileSync(resultsCwd + '/index.html', 'utf8');
289+
290+
expect(err).to.not.exist;
291+
expect(newFile).to.equal(expectedFile);
292+
293+
done();
294+
});
295+
});
296+
297+
it('should load from a filestring', done => {
298+
app.loadMapping(testCwd + '/renaming_map.json');
299+
300+
app.process('**/*.html', {
301+
newPath: testCwd,
302+
cwd: fixturesCwd
303+
}, (err, data) => {
304+
let newFile = fs.readFileSync(testCwd + '/index.html', 'utf8');
305+
let expectedFile = fs.readFileSync(resultsCwd + '/index.html', 'utf8');
306+
307+
expect(err).to.not.exist;
308+
expect(newFile).to.equal(expectedFile);
309+
310+
done();
311+
});
312+
});
313+
314+
it('should load nothing as it does not exist', done => {
315+
app.loadMapping(testCwd + '/doesnotexist.json');
316+
317+
app.process('**/*.html', {
318+
newPath: testCwd,
319+
cwd: fixturesCwd
320+
}, (err, data) => {
321+
let newFile = fs.readFileSync(testCwd + '/index.html', 'utf8');
322+
let expectedFile = fs.readFileSync(resultsCwd + '/index.html', 'utf8');
323+
324+
expect(err).to.not.exist;
325+
expect(newFile).to.not.equal(expectedFile);
326+
327+
done();
328+
});
329+
});
330+
331+
it('should load from a filestring', done => {
332+
app.processCss('**/*.css', {
333+
newPath: testCwd,
334+
cwd: fixturesCwd
335+
}, (err, data) => {
336+
app.generateMapping(testCwd, { cssMappingMin: true }, (err, data) => {
337+
rcs.selectorLibrary.selectors = {};
338+
rcs.selectorLibrary.compressedSelectors = {};
339+
rcs.selectorLibrary.excludes = [];
340+
rcs.nameGenerator.resetCountForTests();
341+
342+
app.loadMapping(testCwd + '/renaming_map_min.json', { origValues: false });
343+
344+
app.process('**/*.html', {
345+
newPath: testCwd,
346+
cwd: fixturesCwd
347+
}, (err, data) => {
348+
let newFile = fs.readFileSync(testCwd + '/index.html', 'utf8');
349+
let expectedFile = fs.readFileSync(resultsCwd + '/index.html', 'utf8');
350+
351+
expect(err).to.not.exist;
352+
expect(newFile).to.equal(expectedFile);
353+
354+
done();
355+
});
356+
});
357+
});
358+
});
359+
360+
});
361+
261362
describe('include config', () => {
262363
it('should set the config with package.json', done => {
263364
// include config

0 commit comments

Comments
 (0)