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

Commit

Permalink
consistent staticBuild defaultExport option and detection
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 15, 2016
1 parent a381d02 commit 550e22c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ function processCompileOpts(options, defaults) {
format: 'umd',
globalDeps: {},
globalName: null,
exportDefault: false,
// conditionalResolutions: {},
// can add a special object here that matches condition predicates to direct module names to use for sfx

Expand Down
19 changes: 18 additions & 1 deletion lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ function getEncoding(canonical, encodings) {

return encodings[canonical] = highestEncoding.toString(16);
}
function getName(encoding, encodings) {
var match
Object.keys(encodings).some(function(e) {
if (encodings[e] == encoding) {
match = e;
return true;
}
});
return match;
}

// used to support leading #!/usr/bin/env in scripts as supported in Node
var hashBangRegEx = /^\#\!.*/;
Expand Down Expand Up @@ -429,7 +439,14 @@ function wrapSFXOutputs(loader, tree, modules, outputs, entryPoints, compileOpts
// for NodeJS execution to work correctly, we need to ensure the scoped module, exports and require variables are nulled out
outputs.unshift("var require = this.require, exports = this.exports, module = this.module;");

outputs.unshift(sfxcore.toString(), "(" + JSON.stringify(entryPoints) + ", " + JSON.stringify(externalDepIds) + ", function(" + compileOpts.systemGlobal + ") {");
// if the first entry point is a dynamic module, then it is exportDefault always by default
var exportDefault = compileOpts.exportDefault;
var exportedLoad = tree[compileOpts.encodeNames && getName(entryPoints[0], cache.encodings) || entryPoints[0]];
if (exportedLoad.metadata.format != 'register' && exportedLoad.metadata.format != 'esm')
exportDefault = true;

outputs.unshift(sfxcore.toString(), "(" + JSON.stringify(entryPoints) + ", " + JSON.stringify(externalDepIds) + ", " +
(exportDefault ? "true" : "false") + ", function(" + compileOpts.systemGlobal + ") {");

outputs.push("})");
return asp(fs.readFile)(path.resolve(__dirname, '../templates/sfx-' + compileOpts.format + '.js'))
Expand Down
7 changes: 6 additions & 1 deletion lib/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,13 @@ exports.rollupTree = function(loader, tree, entryPoints, traceOpts, compileOpts)
.then(function(bundle) {
var entryPointLoad = tree[entryPoint];

var defaultExport = compileOpts.defaultExport;
if (entryPointLoad.metadata.format != 'esm' && entryPointLoad.metadata.format != 'register')
defaultExport = true;

var generateOptions = {
sourceMap: compileOpts.sourceMaps
sourceMap: compileOpts.sourceMaps,
exports: defaultExport ? 'default' : 'named'
};

// for a full tree rollup, we pass all the output options into rollup itself
Expand Down
37 changes: 17 additions & 20 deletions templates/sfx-core-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@
function getESModule(exports) {
var esModule = {};
// don't trigger getters/setters in environments that support them
if (typeof exports == 'object' || typeof exports == 'function') {
var hasOwnProperty = exports && exports.hasOwnProperty;
if ((typeof exports == 'object' || typeof exports == 'function') && exports !== global) {
if (getOwnPropertyDescriptor) {
for (var p in exports) {
if (!trySilentDefineProperty(esModule, exports, p))
setPropertyIfHasOwnProperty(esModule, exports, p, hasOwnProperty);
}
for (var p in exports)
defineOrCopyProperty(esModule, exports, p);
}
else {
for (var p in exports)
setPropertyIfHasOwnProperty(esModule, exports, p, hasOwnProperty);
var hasOwnProperty = exports && exports.hasOwnProperty;
for (var p in exports) {
if (hasOwnProperty && !exports.hasOwnProperty(p))
continue;
esModule[p] = exports[p];
}
}
}
esModule['default'] = exports;
Expand All @@ -158,20 +159,16 @@
return esModule;
}

function setPropertyIfHasOwnProperty(targetObj, sourceObj, propName, hasOwnProperty) {
if (!hasOwnProperty || sourceObj.hasOwnProperty(propName))
targetObj[propName] = sourceObj[propName];
}

function trySilentDefineProperty(targetObj, sourceObj, propName) {
function defineOrCopyProperty(targetObj, sourceObj, propName) {
try {
var d;
if (d = Object.getOwnPropertyDescriptor(sourceObj, propName))
defineProperty(targetObj, propName, d);

return true;
} catch (ex) {
// Object.getOwnPropertyDescriptor threw an exception, fall back to normal set property.
}
catch (ex) {
// Object.getOwnPropertyDescriptor threw an exception, fall back to normal set property
// we dont need hasOwnProperty here because getOwnPropertyDescriptor would have returned undefined above
targetObj[propName] = sourceObj[propName];
return false;
}
}
Expand Down Expand Up @@ -249,7 +246,7 @@
return modules[name] = entry.declarative ? entry.module.exports : entry.esModule;
};

return function(mains, depNames, declare) {
return function(mains, depNames, exportDefault, declare) {
return function(formatDetect) {
formatDetect(function(deps) {
// register external dependencies
Expand All @@ -269,7 +266,7 @@
for (var i = 1; i < mains.length; i++)
load(mains[i]);

if (firstLoad.__useDefault)
if (exportDefault)
return firstLoad['default'];
else
return firstLoad;
Expand Down
2 changes: 1 addition & 1 deletion templates/sfx-core-register.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions templates/sfx-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
return modules[name] = entry.declarative ? entry.module.exports : entry.esModule;
};

return function(mains, depNames, declare) {
return function(mains, depNames, exportDefault, declare) {
return function(formatDetect) {
formatDetect(function(deps) {
var System = {
Expand Down Expand Up @@ -431,7 +431,7 @@
for (var i = 1; i < mains.length; i++)
load(mains[i]);

if (firstLoad.__useDefault)
if (exportDefault)
return firstLoad['default'];
else
return firstLoad;
Expand All @@ -440,7 +440,7 @@
};

})(typeof self != 'undefined' ? self : global)
/* (['mainModule'], ['external-dep'], function($__System) {
/* (['mainModule'], ['external-dep'], false, function($__System) {
System.register(...);
})
(function(factory) {
Expand Down
2 changes: 1 addition & 1 deletion templates/sfx-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/canonicals.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ suite('Canonical Names', function() {
});
assert.equal(builder.getCanonicalName(baseURL + 'src/asdf'), 'trailing/asdf');
assert.equal(builder.getCanonicalName(baseURL + 'src/'), 'trailing/');
assert.equal(builder.getCanonicalName(baseURL + 'src'), 'trailing');
assert.equal(builder.getCanonicalName(baseURL + 'src'), 'trailing/');
})
});

0 comments on commit 550e22c

Please sign in to comment.