Skip to content

Commit 5d2434e

Browse files
author
Julia Bakerink
committed
Added functionality to export entire project in Typescript. Need to address minor syntax bugs.
1 parent 1fdcb10 commit 5d2434e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+936
-14
lines changed

src/components/file_system_interface/ExportProject.vue

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@ export default {
4444
* createExport(this.componentMap['App'].children)
4545
* */
4646
createRouter(location) {
47-
fs.writeFileSync(
48-
path.join(location, "src", "router.js"),
49-
this.createRouterImports(this.componentMap["App"].children) +
50-
this.createExport(this.componentMap["App"].children)
51-
);
47+
if (this.exportAsTypescript === "on") {
48+
fs.writeFileSync(
49+
path.join(location, "src", "router.ts"),
50+
this.createRouterImports(this.componentMap["App"].children) +
51+
this.createExport(this.componentMap["App"].children)
52+
);
53+
} else {
54+
fs.writeFileSync(
55+
path.join(location, "src", "router.js"),
56+
this.createRouterImports(this.componentMap["App"].children) +
57+
this.createExport(this.componentMap["App"].children)
58+
);
59+
}
5260
},
5361
/**
5462
* @description import routed components from the /views/ dir
@@ -211,7 +219,11 @@ export default {
211219
imports += "mapState, mapActions";
212220
} else if (currentComponent.state.length) imports += "mapState";
213221
else imports += "mapActions";
214-
imports += ' } from "vuex"\n';
222+
imports += ' } from "vuex";\n';
223+
}
224+
// if in Typescript mode, import defineComponent
225+
if (this.exportAsTypescript === "on") {
226+
imports += 'import { defineComponent } from "vue";\n';
215227
}
216228
// add imports for children
217229
children.forEach((name) => {
@@ -258,16 +270,25 @@ export default {
258270
methods += " },\n";
259271
}
260272
// concat all code within script tags
261-
let output = "\n\n<script>\n";
262-
output +=
263-
imports + "\nexport default {\n name: '" + componentName + "'";
273+
let output;
274+
if (this.exportAsTypescript === 'on') {
275+
output = "\n\n<script lang='ts'>\n";
276+
output += imports + "\nexport default defineComponent ({\n name: '" + componentName + "'";
277+
} else {
278+
output = "\n\n<script>\n";
279+
output += imports + "\nexport default {\n name: '" + componentName + "'";
280+
}
264281
output += ",\n components: {\n";
265282
output += childrenComponentNames + " },\n";
266283
output += data;
267284
output += computed;
268285
output += methods;
269286
// eslint-disable-next-line no-useless-escape
270-
output += "};\n<\/script>";
287+
if (this.exportAsTypescript === 'on') {
288+
output += "});\n<\/script>";
289+
} else {
290+
output += "};\n<\/script>";
291+
}
271292
return output;
272293
} else {
273294
let str = "";
@@ -279,6 +300,9 @@ export default {
279300
childrenComponentNames += ` ${name},\n`;
280301
});
281302
// eslint-disable-next-line no-useless-escape
303+
if (this.exportAsTypescript === "on") {
304+
return `\n\n<script lang="ts">\nimport { defineComponent } from "vue";\n ${str}\nexport default defineComponent ({\n name: '${componentName}',\n components: {\n${childrenComponentNames} }\n});\n<\/script>`;
305+
}
282306
return `\n\n<script>\n${str}\nexport default {\n name: '${componentName}',\n components: {\n${childrenComponentNames} }\n};\n<\/script>`;
283307
}
284308
},
@@ -323,7 +347,12 @@ export default {
323347
// str += `\n\trouter,
324348
str += `\napp.use(router);`;
325349
str += `\n app.mount('#app');`;
326-
fs.writeFileSync(path.join(location, "src", "main.js"), str);
350+
// if using typescript, export with .ts extension
351+
if (this.exportAsTypescript === "on") {
352+
fs.writeFileSync(path.join(location, "src", "main.ts"), str);
353+
} else {
354+
fs.writeFileSync(path.join(location, "src", "main.js"), str);
355+
}
327356
},
328357
// create babel file
329358
createBabel(location) {
@@ -332,7 +361,27 @@ export default {
332361
str += `\n\t\t'@vue/app'`;
333362
str += `\n\t]`;
334363
str += `\n}`;
335-
fs.writeFileSync(path.join(location, "babel.config.js"), str);
364+
// if using typescript, export with .ts extension
365+
if (this.exportAsTypescript === "on") {
366+
fs.writeFileSync(path.join(location, "src", "babel.config.ts"), str);
367+
} else {
368+
fs.writeFileSync(path.join(location, "babel.config.js"), str);
369+
}
370+
},
371+
createTSConfig(location) {
372+
if (this.exportAsTypescript === "on") {
373+
let str = `{\n\t"extends": "@vue/tsconfig/tsconfig.web.json",
374+
"include": ["src/**/*", "src/**/*.vue"],
375+
"compilerOptions": {
376+
"baseUrl": ".",
377+
"paths": {
378+
"@/*": ["./src/*"]
379+
}
380+
},\n}`;
381+
fs.writeFileSync(path.join(location, "tsconfig.json"), str);
382+
} else {
383+
return;
384+
}
336385
},
337386
// create package.json file
338387
createPackage(location) {
@@ -357,7 +406,13 @@ export default {
357406
str += `\n\t\t"babel-eslint": "^10.0.1",`;
358407
str += `\n\t\t"eslint": "^6.7.2",`;
359408
str += `\n\t\t"eslint-plugin-vue": "^7.0.0-0",`;
360-
str += `\n\t\t"@vue/compiler-sfc": "^3.0.0-0"`;
409+
str += `\n\t\t"@vue/compiler-sfc": "^3.0.0-0",`;
410+
if (this.exportAsTypescript === "on") {
411+
str += `\n\t\t"@vue/tsconfig": "^0.1.3",`;
412+
str += `\n\t\t"typescript": "~4.5.5",`;
413+
str += `\n\t\t"vue-tsc": "^0.31.4",`;
414+
str += `\n\t\t"@babel/preset-typescript": "^7.16.7"`; // not sure we need this?
415+
}
361416
str += `\n\t},`;
362417
str += `\n\t"eslintConfig": {`;
363418
str += `\n\t\t"root": true,`;
@@ -401,6 +456,7 @@ export default {
401456
this.createIndexFile(data);
402457
this.createMainFile(data);
403458
this.createBabel(data);
459+
this.createTSConfig(data);
404460
this.createPackage(data);
405461
// exports images to the /assets folder
406462
// eslint-disable-next-line no-unused-vars
@@ -444,7 +500,7 @@ export default {
444500
},
445501
},
446502
computed: {
447-
...mapState(["componentMap", "imagePath", "routes"]),
503+
...mapState(["componentMap", "imagePath", "routes", "exportAsTypescript"]),
448504
},
449505
};
450506
</script>

testExportProject/babel.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
presets: [
3+
'@vue/app'
4+
]
5+
}

testExportProject/package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "vue-boiler-plate-routes",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "vue-cli-service serve",
7+
"build": "vue-cli-service build",
8+
"lint": "vue-cli-service lint"
9+
},
10+
"dependencies": {
11+
"vue": "^3.2.26",
12+
"vue-router": "^4.0.12",
13+
"vuex": "^4.0.2"
14+
},
15+
"devDependencies": {
16+
"@vue/cli-plugin-babel": "~4.5.0",
17+
"@vue/cli-plugin-eslint": "~4.5.0",
18+
"@vue/cli-service": "~4.5.0",
19+
"babel-eslint": "^10.0.1",
20+
"eslint": "^6.7.2",
21+
"eslint-plugin-vue": "^7.0.0-0",
22+
"@vue/compiler-sfc": "^3.0.0-0"
23+
},
24+
"eslintConfig": {
25+
"root": true,
26+
"env": {
27+
"node": true
28+
},
29+
"extends": [
30+
"plugin:vue/essential",
31+
"eslint:recommended"
32+
],
33+
"rules": {},
34+
"parserOptions": {
35+
"parser": "babel-eslint"
36+
}
37+
},
38+
"postcss": {
39+
"plugins": {
40+
"autoprefixer": {}
41+
}
42+
},
43+
"browserslist": [
44+
"> 1%",
45+
"last 2 versions",
46+
"not ie <= 8"
47+
]
48+
}

testExportProject/public/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
8+
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
9+
<title>New Vue Project</title>
10+
</head>
11+
12+
<body>
13+
<noscript>
14+
<strong>We're sorry but vue-boiler-plate-routes doesn't work properly without JavaScript enabled. Please enable it
15+
to continue.</strong>
16+
</noscript>
17+
<div id="app"></div>
18+
<!-- built files will be auto injected -->
19+
</body>
20+
21+
</html>

testExportProject/src/App.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div id="app">
3+
<div id="nav">
4+
<router-link to="/">HomeView</router-link>
5+
<router-link to="/routeTest">routeTest</router-link>
6+
<router-view></router-view>
7+
</div>
8+
</div>
9+
</template>
10+
11+
<style scoped>
12+
#app {
13+
font-family: 'Avenir', Helvetica, Arial, sans-serif;
14+
-webkit-font-smoothing: antialiased;
15+
-moz-osx-font-smoothing: grayscale;
16+
text-align: center;
17+
color: #2C3E50;
18+
margin-top: 60px;
19+
}
20+
</style>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<div>
3+
<img>
4+
</div>
5+
</template>
6+
7+
<script>
8+
import { mapState, mapActions } from "vuex"
9+
10+
export default {
11+
name: 'Child1',
12+
components: {
13+
},
14+
data () {
15+
return {
16+
testProp: "PLACEHOLDER FOR VALUE",
17+
}
18+
},
19+
computed: {
20+
...mapState([
21+
"state",
22+
]),
23+
},
24+
methods: {
25+
...mapActions([
26+
"action",
27+
]),
28+
},
29+
};
30+
</script>
31+
32+
<style scoped>
33+
</style>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div>
3+
<button></button>
4+
</div>
5+
</template>
6+
7+
<script>
8+
9+
export default {
10+
name: 'Child2',
11+
components: {
12+
},
13+
};
14+
</script>
15+
16+
<style scoped>
17+
</style>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<div>
3+
<div></div>
4+
</div>
5+
</template>
6+
7+
<script>
8+
import Child1 from '@/components/Child1.vue';
9+
import Child2 from '@/components/Child2.vue';
10+
11+
export default {
12+
name: 'Test',
13+
components: {
14+
Child1,
15+
Child2,
16+
},
17+
};
18+
</script>
19+
20+
<style scoped>
21+
</style>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div>
3+
<div></div>
4+
</div>
5+
</template>
6+
7+
<script>
8+
9+
export default {
10+
name: 'routeDiv',
11+
components: {
12+
},
13+
};
14+
</script>
15+
16+
<style scoped>
17+
</style>

testExportProject/src/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createApp } from 'vue'
2+
import App from './App.vue'
3+
import router from './router'
4+
5+
const app = createApp(App)
6+
app.use(router);
7+
app.mount('#app');

0 commit comments

Comments
 (0)