diff --git a/src/index.ts b/src/index.ts index 3a1d4e7..69722bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ export interface Options { export function parse (input: string, tag: string, options?: Options): string { const emptyExport = options && options.emptyExport !== undefined ? options.emptyExport : true const node = getNode(input, tag, options) + const src = getSrcAttribute(node) let parsed = padContent(node, input) // Add a default export of empty object if target tag script not found. @@ -27,7 +28,14 @@ export function parse (input: string, tag: string, options?: Options): string { parsed = '// tslint:disable\nimport Vue from \'vue\'\nexport default Vue\n' } - return parsed + if (parsed && tag === 'script' && src) { + const splitted = parsed.split('\n') + splitted.pop() + splitted.push(`export { default } from '${src}'`) + parsed = splitted.join('\n') + } + + return parsed; } /** @@ -101,3 +109,15 @@ export function getNode (input: string, tag: string, options?: Options): Node { return tagFound && (langEmpty || langMatch) }) as Node } + +function getSrcAttribute(node: Node): string | undefined { + if (!node) return; + + const attr = node.attrs.find((attr: {name: string; value: string}) => { + return attr.name === 'src' + }) + + if (attr) { + return attr.value.replace(/\.[^/.]+$/, '') + } +} diff --git a/test/index.spec.js b/test/index.spec.js index a2706b8..305eb22 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -94,4 +94,10 @@ describe('Tests', function () { expect(parsed).to.be.equal('// tslint:disable\nimport Vue from \'vue\'\nexport default Vue\n') }) + it('should return import/export statements if imports external file', function() { + const contents = fs.readFileSync('./test/test-src-imports.vue', 'utf8') + const parsed = vueParser.parse(contents, 'script', { lang: 'ts' }) + + expect(parsed).to.be.equal('// tslint:disable\n//\n//\n//\nexport { default } from \'./index\'') + }) }) diff --git a/test/test-src-imports.vue b/test/test-src-imports.vue new file mode 100644 index 0000000..ed70ad0 --- /dev/null +++ b/test/test-src-imports.vue @@ -0,0 +1,5 @@ + + +