mirror of
https://github.com/systemjs/systemjs.git
synced 2026-01-18 14:53:14 +00:00
implements #759, defaultExtension js default, disabled for plugin syntax
This commit is contained in:
parent
961bc37fd7
commit
c2ab7142ee
@ -58,7 +58,7 @@
|
||||
throw new TypeError('Can only load node core modules in Node.');
|
||||
this.set(name, this.newModule(getESModule(this._nodeRequire(name.substr(6)))));
|
||||
}
|
||||
return normalize.call(this, name, parentName);
|
||||
return normalize.apply(this, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@ -53,7 +53,8 @@
|
||||
});
|
||||
|
||||
hook('normalize', function(normalize) {
|
||||
return function(name, parentName, parentAddress) {
|
||||
return function(name, parentName) {
|
||||
var args = arguments;
|
||||
var loader = this;
|
||||
var conditionalMatch = name.match(conditionalRegEx);
|
||||
if (conditionalMatch) {
|
||||
@ -77,7 +78,7 @@
|
||||
|
||||
var pluginLoader = loader.pluginLoader || loader;
|
||||
|
||||
return pluginLoader['import'](conditionModule, parentName, parentAddress)
|
||||
return pluginLoader['import'](conditionModule, parentName)
|
||||
.then(function(m) {
|
||||
if (conditionExport === undefined) {
|
||||
// CommonJS case
|
||||
@ -105,11 +106,11 @@
|
||||
else
|
||||
name = name.replace(conditionalRegEx, '');
|
||||
}
|
||||
return normalize.call(loader, name, parentName, parentAddress);
|
||||
return normalize.apply(loader, args);
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.resolve(normalize.call(loader, name, parentName, parentAddress));
|
||||
return Promise.resolve(normalize.apply(loader, args));
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@ hookConstructor(function(constructor) {
|
||||
});
|
||||
|
||||
hook('normalize', function(normalize) {
|
||||
return function(name, parentName, parentAddress) {
|
||||
return function(name, parentName) {
|
||||
if (name.substr(0, 1) != '.' && name.substr(0, 1) != '/' && !name.match(absURLRegEx)) {
|
||||
var bestMatch, bestMatchLength = 0;
|
||||
|
||||
@ -39,6 +39,6 @@ hook('normalize', function(normalize) {
|
||||
name = this.map[bestMatch] + name.substr(bestMatch.length);
|
||||
}
|
||||
|
||||
return normalize.call(this, name, parentName, parentAddress);
|
||||
return normalize.apply(this, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
* basePath: 'lib', // optionally only use a subdirectory within the package
|
||||
* main: 'index.js', // when not set, package name is requested directly
|
||||
* format: 'amd',
|
||||
* defaultExtension: 'js',
|
||||
* defaultExtension: 'ts', // defaults to 'js', can be set to false
|
||||
* meta: {
|
||||
* '*.ts': {
|
||||
* loader: 'typescript'
|
||||
@ -168,34 +168,31 @@
|
||||
// given the package subpath, return the resultant combined path
|
||||
// defaultExtension is only added if the path does not have
|
||||
// loader package meta or exact package meta
|
||||
function toPackagePath(pkgName, pkg, basePath, subPath, defaultJSExtension) {
|
||||
function toPackagePath(pkgName, pkg, basePath, subPath, isPlugin) {
|
||||
var normalized = pkgName + '/' + basePath + subPath;
|
||||
|
||||
var hasLoadMeta = false;
|
||||
var hasLoadMeta = isPlugin;
|
||||
if (pkg.meta)
|
||||
getMetaMatches(pkg.meta, basePath, pkgName, normalized, function(matchMeta, matchDepth) {
|
||||
if (matchMeta.loader || matchDepth == 0)
|
||||
hasLoadMeta = true;
|
||||
});
|
||||
|
||||
return normalized + (hasLoadMeta ? '' : getDefaultExtension(pkg, subPath, defaultJSExtension));
|
||||
return normalized + (hasLoadMeta ? '' : getDefaultExtension(pkg, subPath));
|
||||
}
|
||||
|
||||
function getDefaultExtension(pkg, subPath, defaultJSExtension) {
|
||||
// work out what the defaultExtension is
|
||||
var defaultExtension = '';
|
||||
if ('defaultExtension' in pkg && subPath[subPath.length - 1] != '/') {
|
||||
if (pkg.defaultExtension !== false && subPath.substr(subPath.length - pkg.defaultExtension.length - 1) != '.' + pkg.defaultExtension)
|
||||
defaultExtension = '.' + pkg.defaultExtension;
|
||||
function getDefaultExtension(pkg, subPath) {
|
||||
// don't apply extensions to folders or if defaultExtension = false
|
||||
if (subPath[subPath.length - 1] != '/' && pkg.defaultExtension !== false) {
|
||||
// work out what the defaultExtension is and add if not there already
|
||||
var defaultExtension = '.' + (pkg.defaultExtension || 'js');
|
||||
if (subPath.substr(subPath.length - defaultExtension.length) != defaultExtension)
|
||||
return defaultExtension;
|
||||
}
|
||||
// apply defaultJSExtensions if defaultExtension not set
|
||||
else if (defaultJSExtension) {
|
||||
defaultExtension = '.js';
|
||||
}
|
||||
return defaultExtension;
|
||||
return '';
|
||||
}
|
||||
|
||||
function applyPackageConfig(normalized, pkgName, sync, defaultJSExtension) {
|
||||
function applyPackageConfig(normalized, pkgName, sync, isPlugin) {
|
||||
var loader = this;
|
||||
var pkg = loader.packages[pkgName];
|
||||
|
||||
@ -207,15 +204,15 @@
|
||||
|
||||
// allow for direct package name normalization with trailling "/" (no main)
|
||||
if (normalized.length == pkgName.length + 1 && normalized[pkgName.length] == '/')
|
||||
return normalized + (defaultJSExtension && normalized.substr(normalized.length - 3, 3) != '.js' ? '.js' : '');
|
||||
return normalized;
|
||||
|
||||
// no submap if name is package itself
|
||||
if (normalized.length == pkgName.length)
|
||||
return normalized + getDefaultExtension(pkg, '', defaultJSExtension);
|
||||
return normalized + (loader.defaultJSExtensions && normalized.substr(normalized.length - 3, 3) != '.js' ? '.js' : '');
|
||||
|
||||
// sync normalize does not apply package map
|
||||
if (sync || !pkg.map)
|
||||
return toPackagePath(pkgName, pkg, basePath, normalized.substr(pkgName.length + 1), defaultJSExtension);
|
||||
return toPackagePath(pkgName, pkg, basePath, normalized.substr(pkgName.length + 1));
|
||||
|
||||
var subPath = '.' + normalized.substr(pkgName.length);
|
||||
|
||||
@ -225,7 +222,7 @@
|
||||
if (mapped)
|
||||
return mapped;
|
||||
|
||||
var defaultExtension = getDefaultExtension(pkg, subPath.substr(2), defaultJSExtension);
|
||||
var defaultExtension = isPlugin && getDefaultExtension(pkg, subPath.substr(2));
|
||||
if (defaultExtension)
|
||||
return envMap(loader, pkgName, pkg.map, subPath + defaultExtension);
|
||||
})
|
||||
@ -236,13 +233,13 @@
|
||||
return loader.normalizeSync(pkgName);
|
||||
// internal package map
|
||||
else if (mapped.substr(0, 2) == './')
|
||||
return toPackagePath(pkgName, pkg, basePath, mapped.substr(2), defaultJSExtension);
|
||||
return toPackagePath(pkgName, pkg, basePath, mapped.substr(2), isPlugin);
|
||||
// global package map
|
||||
else
|
||||
return loader.normalize.call(loader, mapped);
|
||||
}
|
||||
else {
|
||||
return toPackagePath(pkgName, pkg, basePath, normalized.substr(pkgName.length + 1), defaultJSExtension);
|
||||
return toPackagePath(pkgName, pkg, basePath, normalized.substr(pkgName.length + 1), isPlugin);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -250,7 +247,7 @@
|
||||
var packageConfigPathsRegExps = {};
|
||||
var pkgConfigPromises = {};
|
||||
function createPackageNormalize(normalize, sync) {
|
||||
return function(name, parentName) {
|
||||
return function(name, parentName, isPlugin) {
|
||||
// apply contextual package map first
|
||||
if (parentName) {
|
||||
var parentPackage = getPackage.call(this, parentName) ||
|
||||
@ -279,7 +276,7 @@
|
||||
var normalized = normalize.call(this, name, parentName);
|
||||
|
||||
// undo defaultJSExtension
|
||||
if (normalized.substr(normalized.length - 3, 3) != '.js')
|
||||
if (defaultJSExtension && normalized.substr(normalized.length - 3, 3) != '.js')
|
||||
defaultJSExtension = false;
|
||||
if (defaultJSExtension)
|
||||
normalized = normalized.substr(0, normalized.length - 3);
|
||||
@ -341,13 +338,13 @@
|
||||
)
|
||||
.then(function() {
|
||||
// finally apply the package config we just created to the current request
|
||||
return applyPackageConfig.call(loader, normalized, pkgPath, sync, defaultJSExtension);
|
||||
return applyPackageConfig.call(loader, normalized, pkgPath, sync, isPlugin);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (pkgName)
|
||||
return applyPackageConfig.call(loader, normalized, pkgName, sync, defaultJSExtension);
|
||||
return applyPackageConfig.call(loader, normalized, pkgName, sync, isPlugin);
|
||||
|
||||
// add back defaultJSExtension if not a package
|
||||
if (defaultJSExtension)
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
hook('normalize', function(normalize) {
|
||||
|
||||
return function(name, parentName) {
|
||||
var normalized = normalize.call(this, name, parentName);
|
||||
var normalized = normalize.apply(this, arguments);
|
||||
|
||||
// if the module is in the registry already, use that
|
||||
if (this.has(normalized))
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
(function() {
|
||||
|
||||
// sync or async plugin normalize function
|
||||
function normalizePlugin(normalize, name, parentName, sync) {
|
||||
function normalizePlugin(normalize, name, parentName, isPlugin, sync) {
|
||||
var loader = this;
|
||||
// if parent is a plugin, normalize against the parent plugin argument only
|
||||
if (parentName) {
|
||||
@ -64,9 +64,11 @@
|
||||
return normalizePluginParts(argumentName, pluginName);
|
||||
}
|
||||
else {
|
||||
// third argument represents that this is a plugin call
|
||||
// which in turn will skip default extension adding within packages
|
||||
return Promise.all([
|
||||
loader.normalize(argumentName, parentName),
|
||||
loader.normalize(pluginName, parentName)
|
||||
loader.normalize(argumentName, parentName, true),
|
||||
loader.normalize(pluginName, parentName, true)
|
||||
])
|
||||
.then(function(normalized) {
|
||||
return normalizePluginParts(normalized[0], normalized[1]);
|
||||
@ -74,20 +76,20 @@
|
||||
}
|
||||
}
|
||||
else {
|
||||
return normalize.call(loader, name, parentName);
|
||||
return normalize.call(loader, name, parentName, isPlugin);
|
||||
}
|
||||
}
|
||||
|
||||
// async plugin normalize
|
||||
hook('normalize', function(normalize) {
|
||||
return function(name, parentName) {
|
||||
return normalizePlugin.call(this, normalize, name, parentName, false);
|
||||
return function(name, parentName, isPlugin) {
|
||||
return normalizePlugin.call(this, normalize, name, parentName, isPlugin, false);
|
||||
};
|
||||
});
|
||||
|
||||
hook('normalizeSync', function(normalizeSync) {
|
||||
return function(name, parentName) {
|
||||
return normalizePlugin.call(this, normalizeSync, name, parentName, true);
|
||||
return function(name, parentName, isPlugin) {
|
||||
return normalizePlugin.call(this, normalizeSync, name, parentName, isPlugin, true);
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user