Skip to content

Commit

Permalink
Merge pull request #1777 from guybedford/self
Browse files Browse the repository at this point in the history
Allow modules to import themselves
  • Loading branch information
lukastaegert committed Dec 15, 2017
2 parents 89ba34f + 648eccd commit 4324d20
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 76 deletions.
13 changes: 0 additions & 13 deletions src/Bundle.js
Expand Up @@ -450,19 +450,6 @@ export default class Bundle {
externalModule.traceExport( importDeclaration.name );
} );
} else {
if ( resolvedId === module.id ) {
// need to find the actual import declaration, so we can provide
// a useful error message. Bit hoop-jumpy but what can you do
const declaration = module.ast.body.find( node => {
return ( node.isImportDeclaration || node.isExportDeclaration ) && node.source.value === source;
} );
const declarationType = /Export/.test( declaration.type ) ? 'export' : 'import';
module.error( {
code: 'CANNOT_IMPORT_SELF',
message: `A module cannot ${declarationType} itself`
}, declaration.start );
}

module.resolvedIds[ source ] = resolvedId;
return this.fetchModule( resolvedId, module.id );
}
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/can-import-self-treeshake/_config.js
@@ -0,0 +1,9 @@
module.exports = {
description: 'direct self import',
warnings: [
{
code: 'EMPTY_BUNDLE',
message: `Generated an empty bundle`
}
]
};
3 changes: 3 additions & 0 deletions test/function/samples/can-import-self-treeshake/lib.js
@@ -0,0 +1,3 @@
import * as lib from './lib';
export const op = x => x * x;
export const run = () => 4;
2 changes: 2 additions & 0 deletions test/function/samples/can-import-self-treeshake/main.js
@@ -0,0 +1,2 @@
import { run } from './lib.js';
run();
8 changes: 8 additions & 0 deletions test/function/samples/can-import-self/_config.js
@@ -0,0 +1,8 @@
var assert = require( 'assert' );

module.exports = {
description: 'a module importing its own bindings',
exports: function ( exports ) {
assert.equal( exports.result, 4 );
}
};
3 changes: 3 additions & 0 deletions test/function/samples/can-import-self/lib.js
@@ -0,0 +1,3 @@
import * as lib from './lib';
export const op = x => x * x;
export const run = () => lib.op( 2 );
2 changes: 2 additions & 0 deletions test/function/samples/can-import-self/main.js
@@ -0,0 +1,2 @@
import { run } from './lib.js';
export const result = run();
20 changes: 0 additions & 20 deletions test/function/samples/cannot-export-self/_config.js

This file was deleted.

1 change: 0 additions & 1 deletion test/function/samples/cannot-export-self/main.js

This file was deleted.

20 changes: 0 additions & 20 deletions test/function/samples/cannot-import-self-empty/_config.js

This file was deleted.

1 change: 0 additions & 1 deletion test/function/samples/cannot-import-self-empty/main.js

This file was deleted.

20 changes: 0 additions & 20 deletions test/function/samples/cannot-import-self/_config.js

This file was deleted.

1 change: 0 additions & 1 deletion test/function/samples/cannot-import-self/main.js

This file was deleted.

0 comments on commit 4324d20

Please sign in to comment.