Browse Source

support function moduleContext option

legacy-quote-reserved-properties
Rich-Harris 8 years ago
parent
commit
2929f1a771
  1. 6
      browser/path.js
  2. 35
      src/Bundle.js
  3. 1
      src/Module.js
  4. 19
      src/ast/nodes/ThisExpression.js
  5. 2
      src/utils/path.js

6
browser/path.js

@ -78,9 +78,3 @@ export function resolve ( ...paths ) {
return resolvedParts.join( '/' ); // TODO windows...
}
export function join ( ...parts ) {
const separator = '/';
const replaceExp = new RegExp(separator+'{1,}', 'g');
return parts.join(separator).replace(replaceExp, separator);
}

35
src/Bundle.js

@ -72,32 +72,17 @@ export default class Bundle {
this.externalModules = [];
this.context = String( options.context );
if (options.moduleContext) {
if (typeof options.moduleContext === 'object') {
const moduleContext = {};
for (const key in options.moduleContext) {
let resolvedKey;
if (isAbsolute(key)) {
resolvedKey = normalize(key);
} else {
const useWorkingDir = key.substring(0, 2) === `./` ? true : false;
let absoluteKey;
if (useWorkingDir) {
absoluteKey = join(process.cwd(), key.substring(2));
} else {
absoluteKey = join(dirname(options.entry), key);
}
resolvedKey = normalize(absoluteKey);
}
moduleContext[resolvedKey] = options.moduleContext[key];
}
this.moduleContext = moduleContext;
} else {
throw new Error('options.moduleContext must be an object');
}
if ( typeof options.moduleContext === 'function' ) {
this.getModuleContext = id => options.moduleContext( id ) || this.context;
} else if ( typeof options.moduleContext === 'object' ) {
const moduleContext = new Map();
Object.keys( options.moduleContext ).forEach( key => {
moduleContext.set( resolve( key ), options.moduleContext[ key ] );
});
this.getModuleContext = id => moduleContext.get( id ) || this.context;
} else {
this.getModuleContext = () => this.context;
}
if ( typeof options.external === 'function' ) {

1
src/Module.js

@ -46,6 +46,7 @@ export default class Module {
this.bundle = bundle;
this.id = id;
this.excludeFromSourcemap = /\0/.test( id );
this.context = bundle.getModuleContext( id );
// all dependencies
this.sources = [];

19
src/ast/nodes/ThisExpression.js

@ -1,29 +1,12 @@
import Node from '../Node.js';
import {normalize} from '../../utils/path';
function getContext (module) {
let context = module.bundle.context;
const moduleContext = module.bundle.moduleContext;
if (moduleContext) {
const moduleId = normalize(module.id);
const candidates = Object.keys(moduleContext)
.map(relPath => moduleId.indexOf(relPath) >= 0 ? moduleContext[relPath] : undefined)
.filter(context => !!context);
if (candidates.length) {
context = String( candidates[candidates.length - 1] );
}
}
return context;
}
export default class ThisExpression extends Node {
initialise ( scope ) {
const lexicalBoundary = scope.findLexicalBoundary();
if ( lexicalBoundary.isModuleScope ) {
this.alias = getContext(this.module);
this.alias = this.module.context;
if ( this.alias === 'undefined' ) {
this.module.bundle.onwarn( 'The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten' );
}

2
src/utils/path.js

@ -13,4 +13,4 @@ export function normalize ( path ) {
return path.replace( /\\/g, '/' );
}
export { basename, dirname, extname, relative, resolve, join} from 'path';
export { basename, dirname, extname, relative, resolve } from 'path';

Loading…
Cancel
Save