Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
require.resolve support
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Mar 8, 2016
1 parent fac9748 commit 8fc2e90
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 63 deletions.
46 changes: 35 additions & 11 deletions compilers/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,31 @@ exports.CJSRequireTransformer = CJSRequireTransformer;


// convert CommonJS into System.registerDynamic
function CJSRegisterTransformer(name, deps, address, optimize, globals, systemGlobal) {
function CJSRegisterTransformer(name, deps, path, optimize, static, globals, systemGlobal) {
this.name = name;
this.deps = deps;
this.address = address;
this.path = path;
this.usesFilePaths = false;
this.usesRequireResolve = false;
this.optimize = optimize;
this.static = static;
this.globals = globals;
this.systemGlobal = systemGlobal;
return ParseTreeTransformer.call(this);
}

CJSRegisterTransformer.prototype = Object.create(ParseTreeTransformer.prototype);

CJSRegisterTransformer.prototype.transformCallExpression = function(tree) {
// require.resolve
if (!this.usesRequireResolve && tree.operand.type == 'MEMBER_EXPRESSION' &&
tree.operand.operand.identifierToken && tree.operand.operand.identifierToken.value == '$__require' &&
tree.operand.memberName && tree.operand.memberName.value == 'resolve') {
this.usesRequireResolve = true;
}
return ParseTreeTransformer.prototype.transformCallExpression.call(this, tree);
}

CJSRegisterTransformer.prototype.transformMemberExpression = function(tree) {
if (this.optimize && tree.operand.operand && tree.operand.operand.identifierToken &&
tree.operand.operand.identifierToken.value == 'process' &&
Expand Down Expand Up @@ -124,7 +136,18 @@ CJSRegisterTransformer.prototype.transformScript = function(tree) {
var scriptItemList = tree.scriptItemList;
var nl = '\n ';

if (this.usesFilePaths)
if (this.usesFilePaths && this.static)
scriptItemList = parseStatements([
"var __filename = '" + this.path + "', __dirname = '" + this.path.split('/').slice(0, -1).join('/') + "';"
]).concat(scriptItemList);

if (this.usesRequireResolve && !this.static) {
scriptItemList = parseStatements([
"$__require.resolve = function(request) { return " + this.systemGlobal + ".get('@@cjs-helpers').requireResolve(request, module.id); };"
]).concat(scriptItemList);
}

if (this.usesFilePaths && !this.static)
scriptItemList = parseStatements([
"var $__pathVars = " + this.systemGlobal + ".get('@@cjs-helpers').getPathVars(module.id), __filename = $__pathVars.filename, __dirname = $__pathVars.dirname;"
]).concat(scriptItemList);
Expand All @@ -142,17 +165,17 @@ CJSRegisterTransformer.prototype.transformScript = function(tree) {
globalExpression += ';';
}

var useStrict = hasRemoveUseStrict(scriptItemList) && [createUseStrictDirective()] || [];
var useStrict = hasRemoveUseStrict(scriptItemList) ? [createUseStrictDirective()] : [];

scriptItemList = useStrict.concat(parseStatements([
globalExpression + nl + 'var define, global = this, GLOBAL = this;'
(globalExpression ? globalExpression + nl : '') + 'var define, global = this, GLOBAL = this;'
])).concat(scriptItemList).concat(parseStatements([
'return module.exports;'
]));

// wrap everything in System.register
return new Script(tree.location, parseStatements([
this.systemGlobal + '.registerDynamic(' + (this.name ? '"' + this.name + '", ' : '') + JSON.stringify(this.deps) + ', true, function($__require, exports, module) {\n',
this.systemGlobal + '.registerDynamic(' + (this.name ? '"' + this.name + '", ' : '') + JSON.stringify(this.deps) + ', true, function($__require, exports, module) {',
'});'], scriptItemList));
};
exports.CJSRegisterTransformer = CJSRequireTransformer;
Expand Down Expand Up @@ -183,7 +206,12 @@ exports.compile = function(load, opts, loader) {
for (var g in load.metadata.globals) {
globals[g] = normalize && load.depMap[load.metadata.globals[g]] || load.metadata.globals[g];
}
transformer = new CJSRegisterTransformer(!opts.anonymous && load.name, deps, load.path, opts.production, globals, opts.systemGlobal);
if (opts.static) {
var path = load.path;
if (path.substr(0, loader.baseURL.length) == loader.baseURL)
path = path.substr(loader.baseURL.length);
}
transformer = new CJSRegisterTransformer(!opts.anonymous && load.name, deps, path, opts.production, opts.static, globals, opts.systemGlobal);
tree = transformer.transformAny(tree);

var output = compiler.write(tree, load.path);
Expand All @@ -199,10 +227,6 @@ exports.compile = function(load, opts, loader) {
});
};

exports.sfx = function(loader) {
return require('fs').readFileSync(require('path').resolve(__dirname, '../templates/cjs-helpers.min.js')).toString();
};

function remap(source, map, fileName) {
var options = {script: true};
var compiler = new traceur.Compiler(options);
Expand Down
8 changes: 2 additions & 6 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ function minify(output, fileName, mangle, uglifyOpts) {

ast = ast.transform(uglify.Compressor(uglifyOpts.compress));
ast.figure_out_scope();
if (mangle !== false) {
ast.mangle_names({
// NB should be possible to remote this
except: ['require']
});
}
if (mangle !== false)
ast.mangle_names();

var sourceMap;
if (output.sourceMap)
Expand Down
42 changes: 0 additions & 42 deletions templates/cjs-helpers.js

This file was deleted.

1 change: 0 additions & 1 deletion templates/cjs-helpers.min.js

This file was deleted.

3 changes: 1 addition & 2 deletions templates/minify.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
uglifyjs sfx-core.js -cm | sed 's/.$//' > sfx-core.min.js
uglifyjs sfx-core-register.js -cm | sed 's/.$//' > sfx-core-register.min.js
uglifyjs amd-helpers.js -cm > amd-helpers.min.js
uglifyjs global-helpers.js -cm > global-helpers.min.js
uglifyjs cjs-helpers.js -cm > cjs-helpers.min.js
uglifyjs global-helpers.js -cm > global-helpers.min.js
2 changes: 1 addition & 1 deletion test/arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ suite('Bundle Expressions', function() {
builder.trace('*.js - [amd-*] - [sfx-format-*]')
.then(function(tree) {
assert.deepEqual(Object.keys(tree).sort(), [
'Buffer.js', 'amd.js', 'babel', 'cjs-globals.js', 'cjs.js', 'component.jsx!jsx.js', 'file.json', 'first.js',
'Buffer.js', 'amd.js', 'babel', 'cjs-globals.js', 'cjs-resolve.js', 'cjs.js', 'component.jsx!jsx.js', 'file.json', 'first.js',
'global-inner.js', 'global-outer.js', 'global.js', 'jquery-cdn', 'jquery.js', 'json-plugin.js', 'jsx.js', 'plugin.js', 'runtime.js',
'second.js', 'some.js!plugin.js', 'text-plugin.js', 'text.txt!text-plugin.js', 'third.js', 'umd.js']);
})
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test-tree/cjs-resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require.resolve('./first.js');
11 changes: 11 additions & 0 deletions test/test-build.html
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@
.catch(done);
});

test('CJS require.resolve', function(done) {
System['import']('output/cjs-resolve.js').then(function(m) {
return System['import']('cjs-resolve.js');
})
.then(function(m) {
ok(m.substr(m.length - 27, 27) == 'fixtures/test-tree/first.js');
done();
})
.catch(done);
});

(/PhantomJS/.test(window.navigator.userAgent) ? test.skip : test)('Runtime', function(done) {
System['import']('output/runtime.js').then(function() {
return System['import']('runtime.js');
Expand Down
4 changes: 4 additions & 0 deletions test/test-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ function doTests(transpiler) {
return builder.bundle('cjs-globals.js - output/amd-8.js', 'test/output/cjs-globals.js');
})

.then(function() {
return builder.bundle('cjs-resolve.js', 'test/output/cjs-resolve.js');
})

.then(function() {
return builder.bundle('runtime.js', 'test/output/runtime.js');
})
Expand Down

0 comments on commit 8fc2e90

Please sign in to comment.