Skip to content

Commit

Permalink
fix: default enable JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
Chieffo2021 committed Jun 17, 2021
1 parent e103054 commit 94e91d2
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 30 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const DEFAULT_VUE_VERSION = 2
export const VUE_COMPILER_SFC_VERSION = '^3.0.5'
export const VITE_VERSION = '^2.1.5'
export const VITE_PLUGIN_VUE_VERSION = '^1.2.1'
export const VITE_PLUGIN_VUE_JSX_VERSION = '^1.1.5'
export const VITE_PLUGIN_VUE_TWO_VERSION = '^1.5.1'
export const VITE_PLUGIN_ENV_COMPATIBLE = '^1.0.0'
export const SASS_VERSION = '^1.34.0'
1 change: 1 addition & 0 deletions src/generate/genePackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function genePackageJson (packageJsonPath: string): void {
if (vueVersion === 3) {
packageJson.devDependencies['@vue/compiler-sfc'] = constants.VUE_COMPILER_SFC_VERSION
packageJson.devDependencies['@vitejs/plugin-vue'] = constants.VITE_PLUGIN_VUE_VERSION
packageJson.devDependencies['@vitejs/plugin-vue-jsx'] = constants.VITE_PLUGIN_VUE_JSX_VERSION
} else if (vueVersion === 2) {
packageJson.devDependencies['vite-plugin-vue2'] = constants.VITE_PLUGIN_VUE_TWO_VERSION
}
Expand Down
24 changes: 3 additions & 21 deletions src/transform/transformVuecli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { DEFAULT_VUE_VERSION } from '../constants/constants'
export class VueCliTransformer implements Transformer {
context : TransformContext = {
vueVersion: DEFAULT_VUE_VERSION,
jsx: this.useJsx(),
config: {},
importList: []
}
Expand Down Expand Up @@ -119,35 +118,18 @@ export class VueCliTransformer implements Transformer {
return config
}

public useJsx () : boolean {
try {
const jsx = require('babel-plugin-transform-vue-jsx')
if (jsx) {
return true
}
} catch (error) {} // eslint-disable-line no-empty

return false
}

public transformVue (context: TransformContext) : void {
const plugins: RawValue[] = []
if (context.vueVersion === 2) {
context.importList.push(
'import { createVuePlugin } from \'vite-plugin-vue2\';'
)
if (context.jsx) {
plugins.push(new RawValue('createVuePlugin({jsx:true})'))
} else {
plugins.push(new RawValue('createVuePlugin()'))
}
plugins.push(new RawValue('createVuePlugin({jsx:true})'))
} else {
context.importList.push('import vue from \'@vitejs/plugin-vue\';')
plugins.push(new RawValue('vue()'))
if (context.jsx) {
context.importList.push('import vueJsx from \'@vitejs/plugin-vue-jsx\';')
plugins.push(new RawValue('vueJsx()'))
}
context.importList.push('import vueJsx from \'@vitejs/plugin-vue-jsx\';')
plugins.push(new RawValue('vueJsx()'))
}

context.importList.push('import envCompatible from \'vite-plugin-env-compatible\';')
Expand Down
3 changes: 2 additions & 1 deletion tests/testdata/generate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"eslint-plugin-vue": "^7.0.0",
"@vitejs/plugin-vue": "^1.2.1",
"vite-plugin-env-compatible": "^1.0.0",
"vite": "^2.1.5"
"vite": "^2.1.5",
"@vitejs/plugin-vue-jsx": "^1.1.5"
},
"eslintConfig": {
"root": true,
Expand Down
2 changes: 0 additions & 2 deletions tests/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('transform vue-cli config', () => {
test('transform vue3', async () => {
const rootDir = path.resolve('./tests/testdata/transform/vue3')
const transformer = new VueCliTransformer();
transformer.context.jsx = true;
const viteConfig = await transformer.transform(rootDir);
expect(viteConfig.plugins).toContainEqual(new RawValue('vue()'));
expect(viteConfig.plugins).toContainEqual(new RawValue('vueJsx()'));
Expand All @@ -22,7 +21,6 @@ describe('transform vue-cli config', () => {
test('transform vue2 with jsx', async() => {
const rootDir = path.resolve('./tests/testdata/transform/vue2');
const transformer = new VueCliTransformer();
transformer.context.jsx = true;
const viteConfig = await transformer.transform(rootDir);
expect(viteConfig.plugins).toContainEqual(new RawValue('createVuePlugin({jsx:true})'));
});
Expand Down

0 comments on commit 94e91d2

Please sign in to comment.