Skip to content

Commit

Permalink
feat(js): support finding imports in .vue files
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Sep 6, 2023
1 parent 1572749 commit 041e0f4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
118 changes: 117 additions & 1 deletion packages/nx/src/native/plugins/js/ts_import_locators.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::fs::read_to_string;
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;
Expand All @@ -9,7 +10,7 @@ use tracing::debug;
use tracing::trace;

use swc_common::comments::SingleThreadedComments;
use swc_common::{BytePos, SourceMap, Spanned};
use swc_common::{BytePos, FileName, SourceMap, Spanned};
use swc_ecma_ast::EsVersion::EsNext;
use swc_ecma_parser::error::Error;
use swc_ecma_parser::lexer::Lexer;
Expand Down Expand Up @@ -816,6 +817,121 @@ import 'a4'; import 'a5';
);
}

#[test]
fn should_find_imports_in_vue_files() {
let temp_dir = TempDir::new().unwrap();
temp_dir
.child("test.vue")
.write_str(
r#"
<script setup lang="ts">
import { VueComponent } from './component.vue'
import('./dynamic-import.vue')
</script>
<template>
<WelcomeItem>
<template #icon>
<DocumentationIcon />
</template>
<template #heading>Documentation</template>
Vue’s
<a href="https://vuejs.org/" target="_blank" rel="noopener">official documentation</a>
provides you with all information you need to get started.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<ToolingIcon />
</template>
<template #heading>Tooling</template>
This project is served and bundled with
<a href="https://vitejs.dev/guide/features.html" target="_blank" rel="noopener">Vite</a>. The
recommended IDE setup is
<a href="https://code.visualstudio.com/" target="_blank" rel="noopener">VSCode</a> +
<a href="https://github.com/johnsoncodehk/volar" target="_blank" rel="noopener">Volar</a>. If
you need to test your components and web pages, check out
<a href="https://www.cypress.io/" target="_blank" rel="noopener">Cypress</a> and
<a href="https://on.cypress.io/component" target="_blank">Cypress Component Testing</a>.
<br />
More instructions are available in <code>README.md</code>.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<EcosystemIcon />
</template>
<template #heading>Ecosystem</template>
Get official tools and libraries for your project:
<a href="https://pinia.vuejs.org/" target="_blank" rel="noopener">Pinia</a>,
<a href="https://router.vuejs.org/" target="_blank" rel="noopener">Vue Router</a>,
<a href="https://test-utils.vuejs.org/" target="_blank" rel="noopener">Vue Test Utils</a>, and
<a href="https://github.com/vuejs/devtools" target="_blank" rel="noopener">Vue Dev Tools</a>. If
you need more resources, we suggest paying
<a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">Awesome Vue</a>
a visit.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<CommunityIcon />
</template>
<template #heading>Community</template>
Got stuck? Ask your question on
<a href="https://chat.vuejs.org" target="_blank" rel="noopener">Vue Land</a>, our official
Discord server, or
<a href="https://stackoverflow.com/questions/tagged/vue.js" target="_blank" rel="noopener"
>StackOverflow</a
>. You should also subscribe to
<a href="https://news.vuejs.org" target="_blank" rel="noopener">our mailing list</a> and follow
the official
<a href="https://twitter.com/vuejs" target="_blank" rel="noopener">@vuejs</a>
twitter account for latest news in the Vue world.
</WelcomeItem>
<WelcomeItem>
<template #icon>
<SupportIcon />
</template>
<template #heading>Support Vue</template>
As an independent project, Vue relies on community backing for its sustainability. You can help
us by
<a href="https://vuejs.org/sponsor/" target="_blank" rel="noopener">becoming a sponsor</a>.
</WelcomeItem>
</template>
"#,
)
.unwrap();

let test_file_path = temp_dir.display().to_string() + "/test.vue";

let results = find_imports(HashMap::from([(
String::from("a"),
vec![test_file_path.clone()],
)]));

let result = results.get(0).unwrap();

assert_eq!(
result.static_import_expressions,
vec![String::from("./component.vue")]
);
assert_eq!(
result.dynamic_import_expressions,
vec![String::from("./dynamic-import.vue")]
);
}

#[test]
fn should_find_imports_in_all_sorts_of_import_statements() {
let temp_dir = TempDir::new().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ export function buildExplicitTypeScriptDependencies({

const filesToProcess: Record<string, string[]> = {};

const moduleExtensions = ['.ts', '.js', '.tsx', '.jsx', '.mts', '.mjs'];
const moduleExtensions = [
'.ts',
'.js',
'.tsx',
'.jsx',
'.mts',
'.mjs',
'.vue',
];

for (const [project, fileData] of Object.entries(fileMap)) {
filesToProcess[project] ??= [];
Expand Down

0 comments on commit 041e0f4

Please sign in to comment.