From 8b5dee744f22ca9f4d0c7b5e866f2ef556f56cae Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 May 2015 14:20:30 -0400 Subject: [PATCH] support export from --- src/Module.js | 19 +++++++++++++++++-- .../export-from-internal-module/_config.js | 7 +++++++ .../export-from-internal-module/foo.js | 1 + .../export-from-internal-module/main.js | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/samples/export-from-internal-module/_config.js create mode 100644 test/samples/export-from-internal-module/foo.js create mode 100644 test/samples/export-from-internal-module/main.js diff --git a/src/Module.js b/src/Module.js index c165eb6..3c0ecdd 100644 --- a/src/Module.js +++ b/src/Module.js @@ -12,7 +12,9 @@ export default class Module { constructor ({ path, code, bundle }) { this.path = path; this.relativePath = relative( bundle.base, path ).slice( 0, -3 ); // remove .js - this.code = new MagicString( code ); + this.code = new MagicString( code, { + filename: path + }); this.bundle = bundle; this.ast = parse( code, { @@ -56,10 +58,12 @@ export default class Module { this.exportStatements = []; this.ast.body.forEach( node => { + let source; + // import foo from './foo'; // import { bar } from './bar'; if ( node.type === 'ImportDeclaration' ) { - const source = node.source.value; + source = node.source.value; node.specifiers.forEach( specifier => { const isDefault = specifier.type === 'ImportDefaultSpecifier'; @@ -97,6 +101,9 @@ export default class Module { // export var foo = 42; // export function foo () {} else if ( node.type === 'ExportNamedDeclaration' ) { + // export { foo } from './foo'; + source = node.source && node.source.value; + if ( node.specifiers.length ) { // export { foo, bar, baz } node.specifiers.forEach( specifier => { @@ -107,6 +114,14 @@ export default class Module { localName, exportedName }; + + if ( source ) { + this.imports[ localName ] = { + source, + localName, + name: exportedName + }; + } }); } diff --git a/test/samples/export-from-internal-module/_config.js b/test/samples/export-from-internal-module/_config.js new file mode 100644 index 0000000..cc774bc --- /dev/null +++ b/test/samples/export-from-internal-module/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'exports from an internal module', + exports: function ( exports, assert ) { + assert.equal( exports.foo, 42 ); + }, + solo: true +}; \ No newline at end of file diff --git a/test/samples/export-from-internal-module/foo.js b/test/samples/export-from-internal-module/foo.js new file mode 100644 index 0000000..c3f7843 --- /dev/null +++ b/test/samples/export-from-internal-module/foo.js @@ -0,0 +1 @@ +export var foo = 42; \ No newline at end of file diff --git a/test/samples/export-from-internal-module/main.js b/test/samples/export-from-internal-module/main.js new file mode 100644 index 0000000..95ec079 --- /dev/null +++ b/test/samples/export-from-internal-module/main.js @@ -0,0 +1 @@ +export { foo } from './foo'; \ No newline at end of file