Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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(/\.[^/.]+$/, '')
}
}
6 changes: 6 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\'')
})
})
5 changes: 5 additions & 0 deletions test/test-src-imports.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<h1>vue-parser</h1>
</template>

<script lang='ts' src='./index.ts'></script>