Skip to content

Commit 6da7e64

Browse files
committed
create a prepublish script that generates the properties.js file
This file will lazy-load properties but also specify the filename in the require explicitly so that it plays better with browserify. Should solve #11
1 parent 4a33c21 commit 6da7e64

File tree

5 files changed

+68
-34
lines changed

5 files changed

+68
-34
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
lib/properties.js

lib/CSSStyleDeclaration.js

+1-31
Original file line numberDiff line numberDiff line change
@@ -206,36 +206,6 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
206206
}
207207
});
208208

209-
var LazyDefinition = function (property, modulePath) {
210-
this.get = function () {
211-
var definition = require(modulePath).definition;
212-
Object.defineProperty(this, property, definition);
213-
return this[property];
214-
};
215-
216-
this.set = function (v) {
217-
var definition = require(modulePath).definition;
218-
Object.defineProperty(this, property, definition);
219-
this[property] = v;
220-
};
221-
222-
this.enumerable = true;
223-
this.configurable = true;
224-
};
225-
226-
/*
227-
*
228-
* http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties
229-
*/
230-
var property_files = fs.readdirSync(__dirname + '/properties');
231-
property_files.forEach(function (property) {
232-
var dashed;
233-
if (property.substr(-3) === '.js') {
234-
property = path.basename(property, '.js');
235-
dashed = camelToDashed(property);
236-
Object.defineProperty(CSSStyleDeclaration.prototype, property, new LazyDefinition(property, './properties/' + property));
237-
Object.defineProperty(CSSStyleDeclaration.prototype, dashed, new LazyDefinition(property, './properties/' + property));
238-
}
239-
});
209+
require('./properties')(CSSStyleDeclaration.prototype);
240210

241211
exports.CSSStyleDeclaration = CSSStyleDeclaration;

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cssstyle",
33
"description": "CSSStyleDeclaration Object Model implementation",
44
"keywords": ["CSS", "CSSStyleDeclaration", "StyleSheet"],
5-
"version": "0.2.18",
5+
"version": "0.2.19",
66
"homepage": "https://github.com/chad3814/CSSStyleDeclaration",
77
"maintainers": [{
88
"name": "Chad Walker",
@@ -33,7 +33,8 @@
3333
"nodeunit": "~0.8.0"
3434
},
3535
"scripts": {
36-
"test": "nodeunit tests"
36+
"test": "./scripts/run_tests.sh",
37+
"prepublish": "node ./scripts/generateTestFiles.js"
3738
},
3839
"licenses": [
3940
{

scripts/generate_properties.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
var camelToDashed = require('../lib/parsers').camelToDashed;
7+
8+
var property_files = fs.readdirSync(path.resolve(__dirname, '../lib/properties'));
9+
var out_file = fs.createWriteStream(path.resolve(__dirname, '../lib/properties.js'), {encoding: 'utf-8'});
10+
11+
out_file.write('\'use strict\';\n\n// autogenerated\n\n');
12+
out_file.write('/*\n *\n * http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties\n */\n\n');
13+
out_file.write('module.exports = function (prototype) {\n');
14+
15+
property_files.forEach(function (property) {
16+
var dashed;
17+
if (property.substr(-3) === '.js') {
18+
property = path.basename(property, '.js');
19+
dashed = camelToDashed(property);
20+
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', {\n');
21+
out_file.write(' get: function () {\n');
22+
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
23+
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
24+
out_file.write(' return this.' + property + ';\n');
25+
out_file.write(' },\n');
26+
out_file.write(' set: function (v) {\n');
27+
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
28+
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
29+
out_file.write(' this.' + property + ' = v;\n');
30+
out_file.write(' },\n');
31+
out_file.write(' enumerable: true,\n');
32+
out_file.write(' configurable: true\n');
33+
out_file.write(' });\n');
34+
if (property !== dashed) {
35+
out_file.write(' Object.defineProperty(prototype, \'' + dashed + '\', {\n');
36+
out_file.write(' get: function () {\n');
37+
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
38+
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
39+
out_file.write(' return this.' + property + ';\n');
40+
out_file.write(' },\n');
41+
out_file.write(' set: function (v) {\n');
42+
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
43+
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
44+
out_file.write(' this.' + property + ' = v;\n');
45+
out_file.write(' },\n');
46+
out_file.write(' enumerable: true,\n');
47+
out_file.write(' configurable: true\n');
48+
out_file.write(' });\n');
49+
}
50+
}
51+
});
52+
53+
out_file.write('};\n');
54+
out_file.end(function (err) {
55+
if (err) {
56+
throw err;
57+
}
58+
});

scripts/run_tests.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
node ./scripts/generate_properties.js
4+
nodeunit tests

0 commit comments

Comments
 (0)