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

feat: Support for Additional Language Modules (Experimental) #2267

Merged
merged 1 commit into from
Dec 30, 2022

Conversation

johnsoncodehk
Copy link
Member

@johnsoncodehk johnsoncodehk commented Dec 30, 2022

Add vueCompilerOptions.experimentalAdditionalLanguageModules option to support config additional LangaugeModule, it can be used to generate virtual files for arbitrary source files other than .vue, or to add global virtual files.

Note that this option is only affect for *.ts when the TypeScript Plugin or Takeover mode is enabled, but always affect for *.vue.

And Show Virtual Files command only works for generated virtual files from source files, for global virtual files you can only debug with Write Virtual Files command for now.

Example

  • tsconfig.json:
{
  "vueCompilerOptions": {
    "experimentalAdditionalLanguageModules": ["./nuxt-virtual-files.js"]
  }
}
  • nuxt-virtual-files.js:
/**
 * @type {import('@volar/language-core').LanguageModule}
 */
module.exports = {
    createFile() {
        return;
    },
    updateFile() {
        return;
    },
    proxyLanguageServiceHost(host) {

        const ts = host.getTypeScriptModule();
        const nitroScript = {
            projectVersion: '',
            fileName: host.getCurrentDirectory() + '/.nuxt/types/nitro.d.ts',
            _version: 0,
            _snapshot: ts.ScriptSnapshot.fromString(''),
            get version() {
                this.update();
                return this._version;
            },
            get snapshot() {
                this.update();
                return this._snapshot;
            },
            update() {
                if (!host.getProjectVersion || host.getProjectVersion() !== this.projectVersion) {
                    this.projectVersion = host.getProjectVersion?.() ?? '';
                    const newText = this.generateText();
                    if (newText !== this._snapshot.getText(0, this._snapshot.getLength())) {
                        console.log(newText);
                        this._version++;
                        this._snapshot = ts.ScriptSnapshot.fromString(newText);
                    }
                }
            },
            generateText() {
                const projectFileNames = host.getScriptFileNames().map(fileName => fileName.replace(host.getCurrentDirectory(), ''));
                const apiFiles = projectFileNames.filter(fileName => fileName.startsWith('/server/api') && fileName.endsWith('.ts'));
                const apiFileBasePaths = apiFiles.map(fileName => fileName.slice('/server/'.length, -'.ts'.length));
                return `
                    // Generated by nitro
                    declare module '@nuxt/nitro' {
                        type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T
                        interface InternalApi {
                            ${apiFileBasePaths.map(name => `'/${name}': Awaited<ReturnType<typeof import("../../server/${name}").default>>`).join(('\n'))}
                        }
                    }
                    export {}
                `;
            },
        };

        return {
            getScriptFileNames() {
                return [
                    ...host.getScriptFileNames(),
                    nitroScript.fileName,
                ];
            },
            getScriptVersion(fileName) {
                if (fileName === nitroScript.fileName) {
                    return nitroScript.version;
                }
                return host.getScriptVersion(fileName);
            },
            getScriptSnapshot(fileName) {
                if (fileName === nitroScript.fileName) {
                    return nitroScript.snapshot;
                }
                return host.getScriptSnapshot(fileName);
            },
        }
    },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant