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

TypeScript support for config file (vue.config.ts) #2138

Open
biluochun opened this issue Aug 10, 2018 · 25 comments
Open

TypeScript support for config file (vue.config.ts) #2138

biluochun opened this issue Aug 10, 2018 · 25 comments

Comments

@biluochun
Copy link

What problem does this feature solve?

Type tracking when configured.

What does the proposed API look like?

can config width: vue.config.ts
or
vue.config.js:

  const VueConf = require('@vue/conf');
  VueConf({
    // baseUrl/assetsDir/devServer/... conf in here
  });
@yyx990803
Copy link
Member

Yeah could be nice to have.

Also /cc @octref

Is there anyway for VSCode to have special type checking / autocomplete for vue.config.js, like for tsconfig.json?

@yyx990803
Copy link
Member

I think we could have something like webpack does:

// vue.config.ts

import { Config } from '@vue/cli-service'

const config: Config = {
  // ...
}

export default config

If anyone want to work on this that'd be great. /cc @ktsn @HerringtonDarkholme

@yyx990803 yyx990803 changed the title Recommended vue.config compatible with .ts Or introduce a package in vue.config.js, trace type TypeScript support for config file (vue.config.ts) Aug 10, 2018
@octref
Copy link
Member

octref commented Aug 10, 2018

special type checking / autocomplete for vue.config.js

JS/TS have different story for type checking / autocomplete than JSON. They are actually closer to what we have in Vetur — finding out the default export and wrap that in a typed function call.

We have plan on our roadmap to support linting / autocomplete for webpack and I believe when it's done it can be easily used to support vue.config.ts.

@ktsn
Copy link
Member

ktsn commented Aug 11, 2018

I'll work on the vue.config.ts support. 👍

@beeequeue
Copy link

Any updates on this?

@kbtz
Copy link

kbtz commented Apr 17, 2019

We might as well enable JS type checking on vue.config.js.

On tsconfig.json enable JS type checking and make sure the config file is included somehow.
Mine looks like this:

{
	"compilerOptions": {
		// ...
		"checkJs": true,
	},
	"include": [
		// ...
		"vue.config.js"
	],
	// ...
}

Then import ProjectOptions with this specific syntax:

/**
 * @typedef { import("@vue/cli-service").ProjectOptions } Options
 */

Importing the definitions other ways didn't worked for me. Also on vscode I'm not getting any type checking errors, only code completions.

Here's a working example with code completion working in vscode even inside chainWebpack:

// vue.config.js
/**
 * @typedef { import("@vue/cli-service").ProjectOptions } Options
 */

const { ProvidePlugin } = require('webpack')
const { resolve, join } = require('path')

/** @param args {string[]} */
const path = (...args) => resolve(join(__dirname, ...args))

/** @type {Options} */
const options = {
	devServer: {
		host: 'bp.viz',
		port: 80,
	},
	chainWebpack: config => {
		config.merge({
			resolve: {
				modules: ['node_modules', path('src')],
			},
			devtool: 'source-map',
		})

		config.plugin('provide').use(ProvidePlugin, [
			{
				Component: ['vue-property-decorator', 'Component'],
				Base: ['./src/components/Base', 'Base'],
			},
		])
	},
}

module.exports = options

@beeequeue
Copy link

beeequeue commented Apr 17, 2019

Can confirm this works!
I had to install @types/webpack-chain to make sure I got the proper types for chainWebpack as well.

My vue.config.js:

/**
 * @type { ProjectOptions }
 */
module.exports = {
  css: {
    sourceMap: true,
  },
  configureWebpack: {
    target: 'electron-renderer',
  },
  lintOnSave: false,
  chainWebpack: config => {
    const svgRules = config.module.rule('svg')

	// Remove default svg loader
    svgRules.uses.clear()

	// Instead use raw-loader
    svgRules.use('raw-loader').loader('raw-loader')
  },
}

@yoyoys
Copy link

yoyoys commented Apr 18, 2019

Here's my vue.config.js config

/**
 * @typedef { import("@vue/cli-service").ProjectOptions } Options
 * @typedef { import("webpack-chain") ChainWebpack }
 */


/**
 * @type { Options }
 */
module.exports = {
  ....
  ....
  /** @type {ChainWebpack} */
  chainWebpack: config => {
    ....
    ....
  }
}

@Zonciu
Copy link

Zonciu commented Apr 18, 2019

It works without checkJs.
Just install @types/webpack-chain to devDependency and add comments:

/**
 *  @typedef { import("@vue/cli-service").ProjectOptions } Options
 *  @type { Options }
 */
module.exports = {
...
}

@avxkim
Copy link

avxkim commented Apr 28, 2019

What's the status of vue.config.ts support?

@150148313
Copy link

(function (exports, require, module, __filename, __dirname) { import * as path from 'path';
^

SyntaxError: Unexpected token *

@Christilut
Copy link

Would like this too. I'm writing scripts for prerendering that I call in my vue.config.js and I prefer to write everything in TypeScript.

@n1kk
Copy link

n1kk commented Oct 4, 2019

Any update on this?

Also might be a good idea to add ability to specify path to tsconfig.json since it may need to be different from default one for your project.

Aside of type checking this will make it easier to reuse ts code from your codebase. Currently I'm using ts-node with a custom tsconfig because my projects compiles to esnext but it is not yet supported by default in node, so I need a separate config to override target as well as add node types since this is compilation process that can use nodejs specific modules.

My IDE does a good job on type-checking and auto-completion but I have to import all utils as require("...").name or require("...").default. Having a proper vue.config.ts support would make code so much nicer and cleaner.

@phil294
Copy link

phil294 commented Nov 28, 2019

Note: Webpack itself does support various languages for their config files: https://webpack.js.org/configuration/configuration-languages/
This however mostly only includes file extensions and not type hints

krmax44 added a commit to krmax44/clef.ninja that referenced this issue Dec 30, 2019
vue.config.ts doesn't work yet, see vuejs/vue-cli#2138
@zwmmm
Copy link

zwmmm commented Jan 6, 2020

Support?

@GrayStrider
Copy link

GrayStrider commented Feb 1, 2020

Bump, I need to import some things from .ts files, would be really nice to have this feature. What are my other options, if any?

Edit:

require('ts-node').register({
  transpileOnly: true
})

seem to do the trick, but ES6 import/export still doesn't work, so I guess I'll just copy the code over?

@n1kk
Copy link

n1kk commented Feb 2, 2020

@GrayStrider you can create vue.config.ts and move all your code to it and use import and export in there, and in vue.config.js you can import it, but keep in mind that if your project output is not supported by node you will need to have additional tsconfig.js that has module set to commonjs:

require('ts-node').register({
  project: _PATH_TO_TSCONFIG_WITH_COMMONJS_,
})

module.exports = require("./vue.config.ts").config;

And then your vue.config.ts can look like this

import { ProjectOptions } from "@vue/cli-service"

export const config: ProjectOptions = {
    // ... your vue config here
};

Now you have type checks and code completion in your vue config.

I keep additional tsconfig with node support in my ./scripts folder, then I can also use it to write my projects build/deploy/etc scripts in ts so I could reuse my code in them and run then from cli directly using ts-script shebang.

@cheesytim
Copy link

Any changes?

@undergroundwires
Copy link

Bump, looking forward to this 🙏 Any updates?

@kondaurovDev
Copy link

I've created vue.config.ts, now it looks:

import { ProjectOptions } from "@vue/cli-service"
import * as path from 'path'

const conf: ProjectOptions = {
    publicPath: process.env.BASE_URL || "/",
    chainWebpack: config => {
        config.resolve
        .alias
        .set('@public', path.resolve(__dirname, 'src/module/public/'));
    },
    devServer: {
        proxy: {
            '^/api': {
                target: "http://localhost:8072",
                pathRewrite: {'^/api' : ''}
            }
        }
    },
    configureWebpack: {
        devtool: 'source-map',
    },
    productionSourceMap: false
}

module.exports = conf

Added script to package.json
"compile:config": "tsc -m commonjs --esModuleInterop vue.config.ts"

Added vue.config.js to gitignore

Now if i've changed configuration i just need to type: pnpm run compile:config and vue.config.js appears :)

I use pnpm, not yarn or npm

@Penguinlay
Copy link

Would love to see vue.config.ts for vue 3.x!

@NWYLZW
Copy link

NWYLZW commented Nov 7, 2021

I did it!!!

  1. install ts-node cross-env
  2. edit your package.json
{
  "scripts": {
    // a little ugly, it can be optimized
    "serve": "cross-env VUE_CLI_SERVICE_CONFIG_PATH=./vue.config.ts ts-node ./node_modules/@vue/cli-service/bin/vue-cli-service.js serve"
  },
}
  1. run it

@jim3692
Copy link

jim3692 commented Jan 12, 2022

@n1kk 's solution seems cleaner. I had to add tsconfig-paths as well for my use case.

require('ts-node').register({
	project: _PATH_TO_TSCONFIG_WITH_COMMONJS_,
});
require('tsconfig-paths/register'); // In case you need paths
module.exports = require("./vue.config.ts").config;

@markhalliwell
Copy link

Any progress on this?

@lmcsu
Copy link

lmcsu commented Jun 11, 2022

Finally got it working.

vue.config.js:

// eslint-disable-next-line n/no-deprecated-api
const isTsRegistered = require.extensions['.ts'];

if (!isTsRegistered) {
    require('ts-node').register({
        project: './tsconfig.json',
        compilerOptions: {
            module: 'commonjs',
        },
    });
}

module.exports = require('./vue.config.ts').default;

vue.config.ts:

import { defineConfig } from '@vue/cli-service';

export default defineConfig({
   ...

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

No branches or pull requests