Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stylus browser build support #2642

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions browser/README.md
@@ -0,0 +1,3 @@
### stylus-browser

scripts for stylus browser version build
96 changes: 96 additions & 0 deletions browser/compile.js
@@ -0,0 +1,96 @@

/**
* Module dependencies.
*/

var fs = require('fs');

// package.json

var info = JSON.parse(fs.readFileSync('browser/stylus.json', 'utf8'));

// BIFs
var bifs = fs.readFileSync('lib/functions/index.styl', 'utf8');

// refactored version of weepy's
// https://github.com/weepy/brequire/blob/master/browser/brequire.js

var browser = {

/**
* Require a module.
*/

require: function require(p){
var path = require.resolve(p)
, mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
},

/**
* Resolve module path.
*/

resolve: function(path){
var orig = path
, reg = path + '.js'
, index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
},

/**
* Return relative require().
*/

relative: function(parent) {
return function(p){
if ('.' != p[0]) return require(p);

var path = parent.split('/')
, segs = p.split('/');
path.pop();

for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}

return require(path.join('/'));
};
},

/**
* Register a module.
*/

register: function(path, fn){
require.modules[path] = fn;
}
};

// read scripts

console.error();
console.log(browser.require + '\n\n');
console.log('var bifs = "' + bifs.replace(/"/g, "'").replace(/\n/g, '\\n\\\n') + '";');
console.log('require.modules = {};\n\n');
console.log('require.resolve = ' + browser.resolve + ';\n\n');
console.log('require.register = ' + browser.register + ';\n\n');
console.log('require.relative = ' + browser.relative + ';\n\n');
info.scripts.forEach(function(path){
console.error(' \033[90m- %s\033[0m', path);
var js = fs.readFileSync(path, 'utf8');
path = path.replace('lib/', '');
console.log('\nrequire.register("%s", function(module, exports, require){\n', path);
console.log(js);
console.log('\n});// module: %s\n', path);
});
console.error();
2 changes: 2 additions & 0 deletions browser/foot.js
@@ -0,0 +1,2 @@
return require('stylus');
})();
2 changes: 2 additions & 0 deletions browser/head.js
@@ -0,0 +1,2 @@

var stylus = (function(){
24 changes: 24 additions & 0 deletions browser/polyfills.js
@@ -0,0 +1,24 @@

/**
* constructor.name polyfill
*/

if (Function.prototype.name === undefined && Object.defineProperty !== undefined) {
Object.defineProperty(Function.prototype, 'name', {
get: function() {
var regex = /function\s([^(]{1,})\(/
, match = regex.exec(this.toString());
return match && match.length > 1 ? match[1].trim() : '';
}
});
}

/**
* String.prototype.trimRight polyfill
*/

if (String.prototype.trimRight === undefined) {
String.prototype.trimRight = function() {
return String(this).replace(/\s+$/, '');
};
}
6 changes: 6 additions & 0 deletions browser/stylus.json
@@ -0,0 +1,6 @@
{
"name": "stylus",
"scripts": [
"lib/visitor/normalizer.js"
]
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
},
"scripts": {
"prepublish": "npm prune",
"build:browser": "node ./browser/compile.js | cat browser/head.js - browser/foot.js > stylus.js",
"test": "mocha test/ test/middleware/ --require chai --bail --check-leaks --reporter dot",
"test-cov": "mocha test/ test/middleware/ --require chai --bail --reporter html-cov > coverage.html"
},
Expand Down