Find ES6-module in which entity is declared

时间:2017-10-12 09:58:35

标签: javascript webpack ecmascript-6 webpack-2 ecmascript-harmony

TL;DR There is some imported entity on ES6-module. Should find original ES module and line number, where this entity has been initially declared - not re-imported / re-exported.

There is some hierarchy of nested imported ES6-modules, some of which are not controlled by library code and had been imported from well-known place - it's some user-side configuration file.

Imagine following files structure:

config.js (not controlled)

import nested from './nested';
export default { 
    param1: { aaa: 10, bbb: 20  },
    param2: nested
}

nested.js (not controlled)

export default { 
    ccc: 30,
    ddd: 40
}

index.js (controlled)

import config from './config'
const ddd = config.ddd;
if(notValid(ddd)) {
    const decl = %FIND_DECLARATION%(ddd);
    console.log('Config variable "config.ddd" is not valid');
    console.log('You declared it here: ', decl);
}

Output should be like following

You declared it here: 
File webpack://./nested.js , line 2
    ddd: 40
    ^^^^^^^

Should write function %FIND_DECLARATION%, which can find original ES-module and line number for some object declaration, and retrieve it's source code

Environment is webpack. Any dirty hacks are welcome if can't be solve without it.

0 个答案:

没有答案