Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add feature about custom options to use own tsconfig.json #4468

Closed
Closed
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
5 changes: 4 additions & 1 deletion packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { runTsc } from '@volar/typescript/lib/quickstart/runTsc';
import * as vue from '@vue/language-core';
import { createCliOptions } from './options';

const windowsPathReg = /\\/g;

export function run() {

let runExtensions = ['.vue'];

const customOptions = createCliOptions();

const extensionsChangedException = new Error('extensions changed');
const main = () => runTsc(
require.resolve('typescript/lib/tsc'),
runExtensions,
(ts, options) => {
const { configFilePath } = options.options;
const vueOptions = typeof configFilePath === 'string'
? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
? vue.createParsedCommandLine(ts, ts.sys, (customOptions?.project || configFilePath).replace(windowsPathReg, '/')).vueOptions
: vue.resolveVueCompilerOptions({});
const allExtensions = [
...vueOptions.extensions,
Expand Down
35 changes: 35 additions & 0 deletions packages/tsc/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
interface Options {
Project: 'project',
};
interface CustomOptions extends Record<Options[keyof Options], string> {};

const ARGS_ACCESS_NUM = 2;
const OPTIONS = {
Project: 'project',
} as const satisfies Options;
const OPTIONS_PREFIX = '--';

export function createCliOptions(): CustomOptions | undefined {
const args = structuredClone(process.argv);
let parsedOptions: CustomOptions | undefined
args.slice(ARGS_ACCESS_NUM).forEach((arg, i) => {
const options = Object.values(OPTIONS)
const optionStrings = options.map(t => `${OPTIONS_PREFIX}${t}`);
if (!optionStrings.includes(arg)) {
return;
}
if (args[i + 1] == null) {
return;
}
const findIndex = optionStrings.findIndex(s => s === arg);
if (findIndex !== -1 && !!options[findIndex]) {
parsedOptions = {
...(parsedOptions ?? {}),
[options[findIndex]]: args.slice(ARGS_ACCESS_NUM)[findIndex + 1],
};
// required to remove custom options and value for executing runTsc()
process.argv = process.argv.filter((_, j) => j !== i + ARGS_ACCESS_NUM && j !== i + ARGS_ACCESS_NUM + 1);
}
});
return parsedOptions;
};
Loading