Browse Source

Add support for module-specific context with 'moduleContext' option

legacy-quote-reserved-properties
Nikhil Tilwalli 9 years ago
parent
commit
4fdf3fb9a2
  1. 6
      browser/path.js
  2. 29
      src/Bundle.js
  3. 20
      src/ast/nodes/ThisExpression.js
  4. 1
      src/rollup.js
  5. 2
      src/utils/path.js
  6. 8
      test/form/custom-module-context/_config.js
  7. 5
      test/form/custom-module-context/_expected/amd.js
  8. 3
      test/form/custom-module-context/_expected/cjs.js
  9. 1
      test/form/custom-module-context/_expected/es.js
  10. 6
      test/form/custom-module-context/_expected/iife.js
  11. 9
      test/form/custom-module-context/_expected/umd.js
  12. 1
      test/form/custom-module-context/main.js
  13. 2
      test/test.js

6
browser/path.js

@ -78,3 +78,9 @@ 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);
}

29
src/Bundle.js

@ -17,7 +17,7 @@ import transformBundle from './utils/transformBundle.js';
import collapseSourcemaps from './utils/collapseSourcemaps.js';
import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
import callIfFunction from './utils/callIfFunction.js';
import { dirname, isRelative, isAbsolute, normalize, relative, resolve } from './utils/path.js';
import { dirname, isRelative, isAbsolute, normalize, relative, resolve, join } from './utils/path.js';
import BundleScope from './ast/scopes/BundleScope.js';
export default class Bundle {
@ -72,6 +72,33 @@ 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.external === 'function' ) {
this.isExternal = options.external;

20
src/ast/nodes/ThisExpression.js

@ -1,11 +1,29 @@
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 = this.module.bundle.context;
this.alias = getContext(this.module);
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' );
}

1
src/rollup.js

@ -24,6 +24,7 @@ const ALLOWED_KEYS = [
'indent',
'interop',
'intro',
'moduleContext',
'moduleId',
'moduleName',
'noConflict',

2
src/utils/path.js

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

8
test/form/custom-module-context/_config.js

@ -0,0 +1,8 @@
module.exports = {
description: 'allows custom module-specific context',
options: {
moduleContext: {
'main.js': 'lolwut'
}
}
};

5
test/form/custom-module-context/_expected/amd.js

@ -0,0 +1,5 @@
define(function () { 'use strict';
lolwut.prop = '???';
});

3
test/form/custom-module-context/_expected/cjs.js

@ -0,0 +1,3 @@
'use strict';
lolwut.prop = '???';

1
test/form/custom-module-context/_expected/es.js

@ -0,0 +1 @@
lolwut.prop = '???';

6
test/form/custom-module-context/_expected/iife.js

@ -0,0 +1,6 @@
(function () {
'use strict';
lolwut.prop = '???';
}());

9
test/form/custom-module-context/_expected/umd.js

@ -0,0 +1,9 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
lolwut.prop = '???';
})));

1
test/form/custom-module-context/main.js

@ -0,0 +1 @@
this.prop = '???';

2
test/test.js

@ -90,7 +90,7 @@ describe( 'rollup', function () {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( () => {
throw new Error( 'Missing expected error' );
}, err => {
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, context, dest, entry, exports, external, footer, format, globals, indent, interop, intro, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' );
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, context, dest, entry, exports, external, footer, format, globals, indent, interop, intro, moduleContext, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' );
});
});

Loading…
Cancel
Save