Skip to content

Commit

Permalink
feat: modify format for generating 'vite.config.js'
Browse files Browse the repository at this point in the history
  • Loading branch information
konpeki622 committed Aug 3, 2021
1 parent c0dea50 commit a0eeea2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
21 changes: 18 additions & 3 deletions src/generate/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ function isObject (obj) {
return obj !== null && (typeof obj === 'object' || typeof obj === 'function')
}

function isEmptyObject (obj) {
let isEmpty = true
for (const key in obj) {
if (obj[key] !== null && obj[key] !== undefined) {
isEmpty = false
break
}
if (typeof obj[key] === 'object') {
isEmpty = isEmptyObject(obj[key])
}
}
return isEmpty
}

export function serializeObject (val: unknown): string {
const seen = []

function serializeInternal (val, pad) {
const newLine = '\n'
const indent = ' '
const indent = ' '
pad = pad || ''
const curIndent = pad + indent

Expand Down Expand Up @@ -61,11 +75,12 @@ export function serializeObject (val: unknown): string {
return val.value
}

const objKeys = Object.keys(val)
if (objKeys.length === 0) {
if (isEmptyObject(val)) {
return '{}'
}

const objKeys = Object.keys(val)

seen.push(val)
const ret =
'{' +
Expand Down
4 changes: 1 addition & 3 deletions src/template/vite.config.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ import path from 'path';
<% }); -%>

// https://vitejs.dev/config/
export default defineConfig(
<%- USER_CONFIG %>
)
export default defineConfig(<%- USER_CONFIG %>)
2 changes: 1 addition & 1 deletion src/transform/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function transformImporters (context: TransformContext, astParsingResult?
context.importers.push(
'import { createVuePlugin } from \'vite-plugin-vue2\';'
)
plugins.push(new RawValue('createVuePlugin({jsx:true})'))
plugins.push(new RawValue('createVuePlugin({ jsx: true })'))
}
if (astParsingResult && astParsingResult.parsingResult.FindRequireContextParser && astParsingResult.parsingResult.FindRequireContextParser.length > 0) {
context.importers.push('import ViteRequireContext from \'@originjs/vite-plugin-require-context\';')
Expand Down
52 changes: 26 additions & 26 deletions tests/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ test('serializeObject', () => {
}
const resultB = serializeObject(objB);
expect(resultB).toMatch('{\n' +
' key1: {\n' +
' key3: false,\n' +
' key4: 0,\n' +
' key5: \'0\',\n' +
' abbreviatedKey: \'abbreviatedKey\'\n' +
' }\n' +
' key1: {\n' +
' key3: false,\n' +
' key4: 0,\n' +
' key5: \'0\',\n' +
' abbreviatedKey: \'abbreviatedKey\'\n' +
' }\n' +
'}');

const objC = {
Expand All @@ -90,11 +90,11 @@ test('serializeObject', () => {
}
const resultC = serializeObject(objC);
expect(resultC).toMatch('{\n' +
' key1: [\n' +
' false,\n' +
' 0,\n' +
' \'0\'\n' +
' ]\n' +
' key1: [\n' +
' false,\n' +
' 0,\n' +
' \'0\'\n' +
' ]\n' +
'}'
);

Expand All @@ -105,14 +105,14 @@ test('serializeObject', () => {
}
const resultD = serializeObject(objD);
expect(resultD).toMatch('{\n' +
' key1: [\n' +
' {\n' +
' key3: false,\n' +
' key4: 0,\n' +
' key5: \'0\',\n' +
' abbreviatedKey: \'abbreviatedKey\'\n' +
' }\n' +
' ]\n' +
' key1: [\n' +
' {\n' +
' key3: false,\n' +
' key4: 0,\n' +
' key5: \'0\',\n' +
' abbreviatedKey: \'abbreviatedKey\'\n' +
' }\n' +
' ]\n' +
'}'
);

Expand All @@ -127,12 +127,12 @@ test('serializeObject', () => {
}
const resultE = serializeObject(objE);
expect(resultE).toMatch('{\n' +
' key3: function () { console.log(\'anonymous function\'); },\n' +
' key4: function () { console.log(\'anonymous function2\'); },\n' +
' key5: () => { console.log(\'arrow function\'); },\n' +
' key6: () => { console.log(\'arrow function\'); },\n' +
' key7() { console.log(\'abbreviated function\'); },\n' +
' key8() { console.log(\'abbreviated function\'); },\n' +
' key9() { /* key9 */ console.log(\'abbreviated function\'); }\n' +
' key3: function () { console.log(\'anonymous function\'); },\n' +
' key4: function () { console.log(\'anonymous function2\'); },\n' +
' key5: () => { console.log(\'arrow function\'); },\n' +
' key6: () => { console.log(\'arrow function\'); },\n' +
' key7() { console.log(\'abbreviated function\'); },\n' +
' key8() { console.log(\'abbreviated function\'); },\n' +
' key9() { /* key9 */ console.log(\'abbreviated function\'); }\n' +
'}');
});
2 changes: 1 addition & 1 deletion tests/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('transform vue-cli config', () => {
const rootDir = path.resolve('./tests/testdata/transform/vue2');
const transformer = new VueCliTransformer();
const viteConfig = await transformer.transform(rootDir);
expect(viteConfig.plugins).toContainEqual(new RawValue('createVuePlugin({jsx:true})'));
expect(viteConfig.plugins).toContainEqual(new RawValue('createVuePlugin({ jsx: true })'));
});

test('transform devServer', () => {
Expand Down

0 comments on commit a0eeea2

Please sign in to comment.