From 2929f1a771bf24fbcea3bccd89d4c084323cee2c Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 9 Oct 2016 12:25:08 -0400 Subject: [PATCH] support function moduleContext option --- browser/path.js | 6 ------ src/Bundle.js | 35 ++++++++++----------------------- src/Module.js | 1 + src/ast/nodes/ThisExpression.js | 19 +----------------- src/utils/path.js | 2 +- 5 files changed, 13 insertions(+), 50 deletions(-) diff --git a/browser/path.js b/browser/path.js index 84efd9d..279ba66 100644 --- a/browser/path.js +++ b/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); -} diff --git a/src/Bundle.js b/src/Bundle.js index 7b06181..66deba6 100644 --- a/src/Bundle.js +++ b/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' ) { diff --git a/src/Module.js b/src/Module.js index 3abdd4c..0cb02db 100644 --- a/src/Module.js +++ b/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 = []; diff --git a/src/ast/nodes/ThisExpression.js b/src/ast/nodes/ThisExpression.js index b6b8f68..5d295fe 100644 --- a/src/ast/nodes/ThisExpression.js +++ b/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' ); } diff --git a/src/utils/path.js b/src/utils/path.js index 1f8af19..d2ce64d 100644 --- a/src/utils/path.js +++ b/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';