Skip to content

Commit

Permalink
feat: check require.resolve for web
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 26, 2023
1 parent 0eaaf38 commit 97b01e6
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/vue-language-core/src/utils/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ function createParsedCommandLineBase(
const plugins = pluginPaths
.map<VueLanguagePlugin | undefined>((pluginPath: string) => {
try {
pluginPath = resolvePath(pluginPath);
return require(pluginPath);
const resolvedPath = resolvePath(pluginPath);
if (resolvedPath) {
return require(resolvedPath);
}
}
catch (error) {
console.warn('Load plugin failed', pluginPath, error);
Expand All @@ -104,22 +106,31 @@ function createParsedCommandLineBase(
...content.raw.vueCompilerOptions,
};

vueOptions.hooks = vueOptions.hooks?.map(resolvePath);
vueOptions.experimentalAdditionalLanguageModules = vueOptions.experimentalAdditionalLanguageModules?.map(resolvePath);
vueOptions.hooks = vueOptions.hooks
?.map(resolvePath)
.filter((hook): hook is NonNullable<typeof hook> => !!hook);
vueOptions.experimentalAdditionalLanguageModules = vueOptions.experimentalAdditionalLanguageModules
?.map(resolvePath)
.filter((module): module is NonNullable<typeof module> => !!module);

return {
...content,
vueOptions,
};

function resolvePath(scriptPath: string) {
function resolvePath(scriptPath: string): string | undefined {
try {
scriptPath = require.resolve(scriptPath, { paths: [folder] });
if (require?.resolve) {
scriptPath = require.resolve(scriptPath, { paths: [folder] });
}
else {
console.log('failed to resolve path:', scriptPath, 'require.resolve is not supported in web');
}
}
catch (error) {
console.warn(error);
}
return scriptPath;
return;
}
}

Expand Down

0 comments on commit 97b01e6

Please sign in to comment.