Skip to content

Commit

Permalink
Handle both script and script setup (#6)
Browse files Browse the repository at this point in the history
* Handle both script and script setup

* Add test case for mixed script setup and script

---------

Co-authored-by: Bobby Wertman <bwertman@rapidcourt.com>
  • Loading branch information
CasualSuperman and bwertman-rc authored May 30, 2024
1 parent d71e513 commit 3f95dc7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ module.exports = function(content, options) {

const result = compiler.parse(content, { sourceMap: false });
const { styles, script, scriptSetup } = result.descriptor;
const usedScript = script || scriptSetup;

const dependencies = [];

if (usedScript?.content) {
if (usedScript.attrs?.lang === 'ts') {
dependencies.push(...detectiveTypeScript(usedScript.content, options));
} else {
dependencies.push(...detectiveEs6(usedScript.content, options));
for (const usedScript of [script, scriptSetup]) {
if (usedScript?.content) {
if (usedScript.attrs?.lang === 'ts') {
dependencies.push(...detectiveTypeScript(usedScript.content, options));
} else {
dependencies.push(...detectiveEs6(usedScript.content, options));
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ import OtherComponent from "./OtherComponent.vue";
<template>
<OtherComponent />
</template>
`);
assert.equal(deps.length, 2);
assert.equal(deps[0], 'mylib');
assert.equal(deps[1], './OtherComponent.vue');
});

it('retrieves the dependencies of script block lang ts using both setup and normal syntax', () => {
const deps = detective(`<script lang="ts">
import { foo, bar } from "mylib";
</script>
<script setup lang="ts">
import OtherComponent from "./OtherComponent.vue";
</script>
<template>
<OtherComponent />
</template>
`);
assert.equal(deps.length, 2);
assert.equal(deps[0], 'mylib');
Expand Down

0 comments on commit 3f95dc7

Please sign in to comment.