Skip to content

Commit 833de0c

Browse files
Merge pull request #3 from jawa-the-hutt/dev
added forced babel.config.js setup. will overwrite existing.
2 parents be2d407 + 224ea12 commit 833de0c

File tree

333 files changed

+5355
-529
lines changed

Some content is hidden

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

333 files changed

+5355
-529
lines changed

generator/index.js

Lines changed: 151 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
const path = require('path');
2-
const fs = require('fs')
3-
const globby = require('globby');
4-
const rimraf = require('rimraf')
2+
const fs = require('fs-extra');
53
const replace = require('replace-in-file');
64

5+
76
module.exports = (api, options, rootOptions) => {
7+
8+
console.log('options.isNativeOnly - ', options.isNativeOnly)
9+
console.log('options.isNVW - ', options.isNVW)
10+
console.log('options.isNewProject - ', options.isNewProject)
11+
12+
// New Project & Native Only -- should never be able to use Nativescript-Vue-Web
13+
if(options.isNativeOnly && options.isNVW) {
14+
throw Error('Invalid options chosen. You cannot have a Native only project and use Nativescript-Vue-Web')
15+
}
16+
17+
if(options.isNativeOnly)
18+
options.isNVW = false;
819

9-
const commonRendorOptions = {
20+
const commonRenderOptions = {
1021
applicationName: api.generator.pkg.name,
1122
applicationVersion: api.generator.pkg.version,
1223
applicationAndroidVersionCode: api.generator.pkg.version.split('.').join('0'),
1324
applicationDescription: api.generator.pkg.description || api.generator.pkg.name,
1425
applicationLicense: api.generator.pkg.license || 'MIT',
1526
applicationId: options.applicationId,
1627
historyMode: options.historyMode || false,
17-
doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript')
28+
doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript'),
29+
usingBabel: api.hasPlugin('babel'),
30+
usingTS: api.hasPlugin('typescript')
1831
}
1932

2033
console.log('adding to package.json');
@@ -30,107 +43,174 @@ module.exports = (api, options, rootOptions) => {
3043
}
3144
},
3245
scripts: {
46+
"setup-webpack-config": "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance pre",
47+
"remove-webpack-config": "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance post",
3348
"serve:web": "vue-cli-service serve --mode development.web",
34-
"serve:android": "vue-cli-service tns:dev --mode development.android",
35-
"serve:ios": "vue-cli-service tns:dev --mode development.ios",
49+
"serve:android": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=development.android tns run android --bundle && npm run remove-webpack-config",
50+
"serve:ios": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=development.ios tns run ios --bundle && npm run remove-webpack-config",
3651
"build:web": "vue-cli-service build --mode production.web",
37-
"build:android": "vue-cli-service tns:prod --mode production.android",
38-
"build:ios": "vue-cli-service tns:prod --mode production.ios",
39-
},
52+
"build:android": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=production.android tns run android --bundle && npm run remove-webpack-config",
53+
"build:ios": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=production.ios tns run ios --bundle && npm run remove-webpack-config",
54+
},
4055
dependencies: {
4156
'nativescript-vue': '^2.0.2',
4257
'tns-core-modules': '^4.2.1',
4358
},
4459
devDependencies: {
45-
'@babel/core': '^7.1.2',
46-
'@babel/preset-env': '^7.1.0',
47-
'@babel/types': '^7.1.3',
48-
'babel-loader': '^8.0.4',
49-
'babel-traverse': '^6.26.0',
5060
'clean-webpack-plugin': '^0.1.19',
51-
'copy-webpack-plugin': '^4.5.4',
61+
'copy-webpack-plugin': '^4.6.0',
62+
'cross-env': '^5.2.0',
5263
'nativescript-dev-webpack': '^0.17.0',
5364
'nativescript-vue-template-compiler': '^2.0.2',
5465
'nativescript-worker-loader': '~0.9.1',
5566
'replace-in-file': '^3.4.2',
56-
"string-replace-loader": "^2.1.1",
5767
}
5868
})
5969

60-
console.log('deleting from package.json');
61-
70+
// if the project is using babel, then load appropriate packages
71+
if(api.hasPlugin('babel')) {
72+
api.extendPackage({
73+
devDependencies: {
74+
'@babel/core': '^7.1.2',
75+
'@babel/preset-env': '^7.1.0',
76+
'@babel/types': '^7.1.3',
77+
'babel-loader': '^8.0.4',
78+
'babel-traverse': '^6.26.0',
79+
}
80+
})
81+
}
6282

83+
console.log('deleting from package.json');
6384
api.extendPackage(pkg => {
64-
// delete pkg.dependencies['vue']
65-
delete pkg.devDependencies[
66-
// 'vue-template-compiler',
67-
'babel-core'
68-
]
69-
// delete pkg.browserslist
85+
// if the project is using babel, then delete babel-core
86+
if(api.hasPlugin('babel')) {
87+
delete pkg.devDependencies[
88+
'babel-core'
89+
]
90+
}
91+
// we will be replacing these
7092
delete pkg.scripts['serve'],
7193
delete pkg.scripts['build']
7294

7395
})
7496

7597
console.log('doing template rendering');
7698

77-
// render the App_Resources files
78-
api.render('./templates/App_Resources', commonRendorOptions)
79-
8099
// use the answer from the invoke prompt and if it's a new project use the new template
81100
// and if it is an existing project, use the existing template
82101
if(options.isNewProject) {
83-
api.render('./templates/simple/new', commonRendorOptions)
84-
} else {
85-
api.render('./templates/simple/existing', commonRendorOptions)
102+
103+
// New Project and not using Nativescript-Vue-Web
104+
if(!options.isNVW && !options.isNativeOnly) {
105+
api.render('./templates/simple/without-nvw/new', commonRenderOptions)
106+
107+
if(api.hasPlugin('vue-router')){
108+
api.injectImports('src/main.js', `import router from '~/router'`)
109+
api.injectRootOptions('src/main.js', `router`)
110+
}
111+
112+
if(api.hasPlugin('vuex')){
113+
api.injectImports('src/main.js', `import store from '~/store'`)
114+
api.injectRootOptions('src/main.js', `store`)
115+
api.injectImports('app/main.js', `import store from 'src/store'`)
116+
api.injectRootOptions('app/main.js', `store`)
117+
118+
}
119+
}
120+
121+
// New Project and is using Nativescript-Vue-Web
122+
if(options.isNVW && !options.isNativeOnly) {
123+
124+
}
125+
126+
// New Project & Native Only -- should never be able to use Nativescript-Vue-Web
127+
if(!options.isNVW && options.isNativeOnly) {
128+
api.render('./templates/simple/native-only/new', commonRenderOptions);
129+
}
130+
131+
if(options.isNativeOnly && options.isNVW) {
132+
// should never reach this block of code
133+
}
134+
135+
136+
} else { // Exising Project
137+
138+
// Existing Project and not using Nativescript-Vue-Web
139+
if(!options.isNVW && !options.isNativeOnly) {
140+
api.render('./templates/simple/without-nvw/existing', commonRenderOptions)
141+
}
142+
143+
// Existing Project and is using Nativescript-Vue-Web
144+
if(options.isNVW && !options.isNativeOnly) {
145+
146+
}
147+
148+
// Existing Project & Native Only -- should never be able to use Nativescript-Vue-Web
149+
if(!options.isNVW && options.isNativeOnly) {
150+
api.render('./templates/simple/native-only/existing', commonRenderOptions)
151+
}
152+
153+
if(options.isNVW && options.isNativeOnly) {
154+
// should never reach this block of code
155+
}
156+
86157
}
87158

159+
160+
161+
162+
88163
api.onCreateComplete(() => {
89164

90165
const newline = process.platform === 'win32' ? '\r\n' : '\n';
91-
const webpackConfigFile = api.resolve('./webpack.config.js');
92-
const src = api.resolve('src');
93166
const gitignorePath = api.resolve('.gitignore');
167+
const gitignoreWebpackConfig = api.resolve('.webpack.config.js');
168+
169+
// // setup string replacement options for babel.config.js file
170+
// if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
171+
// const replaceOptions = {
172+
// files: './babel.config.js',
173+
// from: ' \'@vue/app\'',
174+
// to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
175+
// }
176+
// replace(replaceOptions, (err, changes) => {
177+
// if (err) throw err;
178+
// });
179+
// }
180+
181+
182+
// for new projects that are native only, move files/dirs and delete others
183+
if(options.isNewProject && options.isNativeOnly) {
184+
185+
// move store.js file from ./src to ./app
186+
if(api.hasPlugin('vuex')) {
187+
fs.move('./src/store.js', './app/store.js', (err) => {
188+
if(err) throw err;
189+
})
190+
}
94191

95-
// // // // delete the 'public' directory
96-
// // // const publicPath = api.resolve('public')
97-
// // // if(fs.existsSync(publicPath)) {
98-
// // // rimraf.sync(publicPath)
99-
// // // }
100-
101-
// rename any webpack.config.js file that might already be there
102-
if(fs.existsSync(webpackConfigFile)) {
103-
fs.rename(webpackConfigFile, './webpack.config.old', (err) => {
104-
if (err) throw err;
105-
});
106-
}
192+
// move assets directory from ./src/assets to ./app/assets
193+
fs.ensureDir('./src/assets', err => {
194+
if(err) throw err;
195+
fs.move('./src/assets', './app/assets', err => {
196+
if (err) throw err;
197+
})
198+
})
107199

108-
// rename main.js to main.web.js
109-
if(fs.existsSync(path.resolve(src, 'main.js'))) {
110-
fs.rename(path.resolve(src, 'main.js'), path.resolve(src, 'main.web.js'), (err) => {
111-
if (err) throw err;
112-
});
113-
}
200+
fs.remove('./src', err => {
201+
if (err) throw err
202+
})
114203

115-
// setup string replacement options for babel.config.js file
116-
if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
117-
const replaceOptions = {
118-
files: './babel.config.js',
119-
from: ' \'@vue/app\'',
120-
to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
121-
}
122-
replace(replaceOptions, (err, changes) => {
123-
if (err) throw err;
124-
});
125204
}
126205

206+
127207
// write out environmental files
128-
const developmentAndroid = 'NODE_ENV=development' + newline + 'VUE_PLATFORM=android' + newline + 'VUE_APP_MODE=native';
129-
const developmentIOS = 'NODE_ENV=development' + newline + 'VUE_PLATFORM=ios' + newline + 'VUE_APP_MODE=native';
130-
const developmentWeb = 'NODE_ENV=development' + newline + 'VUE_PLATFORM=web' + newline + 'VUE_APP_MODE=web';
131-
const productionAndroid = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=android' + newline + 'VUE_APP_MODE=native';
132-
const productionIOS = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=ios' + newline + 'VUE_APP_MODE=native';
133-
const productionWeb = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=web' + newline + 'VUE_APP_MODE=web';
208+
const developmentAndroid = 'NODE_ENV=development' + newline + 'VUE_APP_PLATFORM=android' + newline + 'VUE_APP_MODE=native';
209+
const developmentIOS = 'NODE_ENV=development' + newline + 'VUE_APP_PLATFORM=ios' + newline + 'VUE_APP_MODE=native';
210+
const developmentWeb = 'NODE_ENV=development' + newline + 'VUE_APP_PLATFORM=web' + newline + 'VUE_APP_MODE=web';
211+
const productionAndroid = 'NODE_ENV=production' + newline + 'VUE_APP_PLATFORM=android' + newline + 'VUE_APP_MODE=native';
212+
const productionIOS = 'NODE_ENV=production' + newline + 'VUE_APP_PLATFORM=ios' + newline + 'VUE_APP_MODE=native';
213+
const productionWeb = 'NODE_ENV=production' + newline + 'VUE_APP_PLATFORM=web' + newline + 'VUE_APP_MODE=web';
134214

135215
fs.writeFileSync('./.env.development.android', developmentAndroid, { encoding: 'utf8' }, (err) => {if (err) throw err;});
136216
fs.writeFileSync('./.env.development.ios', developmentIOS, { encoding: 'utf8' }, (err) => {if (err) throw err;});
@@ -142,8 +222,8 @@ module.exports = (api, options, rootOptions) => {
142222

143223
// write nsconfig.json
144224
const nsconfig = {
145-
'appPath': 'src',
146-
'appResourcesPath': 'src/App_Resources'
225+
'appPath': 'app',
226+
'appResourcesPath': 'app/App_Resources'
147227
}
148228
fs.writeFileSync('./nsconfig.json', JSON.stringify(nsconfig, null, 2), {encoding: 'utf8'}, (err) => {if (err) throw err;});
149229

@@ -156,7 +236,7 @@ module.exports = (api, options, rootOptions) => {
156236
gitignoreContent = '';
157237
}
158238

159-
const gitignoreAdditions = newline + '# NativeScript application' + newline + 'hooks' + newline + 'platforms'
239+
const gitignoreAdditions = newline + '# NativeScript application' + newline + 'hooks' + newline + 'platforms' + newline + './webpack.config.js'
160240
if (gitignoreContent.indexOf(gitignoreAdditions) === -1) {
161241
gitignoreContent += gitignoreAdditions
162242

@@ -165,6 +245,6 @@ module.exports = (api, options, rootOptions) => {
165245

166246
})
167247

168-
248+
169249

170250
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<%_ if (!rootOptions.router) { _%>
2+
<%# This code is the same as having a router. #%>
3+
<%# Setting this space aside for future possible use in the template #%>
4+
<template>
5+
<Page>
6+
<ActionBar :title="navbarTitle"/>
7+
<GridLayout rows="auto, auto">
8+
<Button text="Home" @tap="goToHomePage" row="0"/>
9+
<Button text="About" @tap="goToAboutPage" row="1"/>
10+
</GridLayout>
11+
</Page>
12+
</template>
13+
<script>
14+
// ~ is an alias to /src
15+
import Home from '~/views/Home';
16+
import About from '~/views/About';
17+
18+
export default {
19+
20+
data() {
21+
return {
22+
navbarTitle: 'App.android.vue',
23+
}
24+
},
25+
methods: {
26+
goToHomePage() {
27+
this.$navigateTo(Home);
28+
},
29+
goToAboutPage() {
30+
this.$navigateTo(About);
31+
}
32+
}
33+
}
34+
35+
</script>
36+
<%_ } else { _%>
37+
<%# This code is the same as not having a router. #%>
38+
<%# See note above #%>
39+
<template>
40+
<Page>
41+
<ActionBar :title="navbarTitle"/>
42+
<GridLayout rows="auto, auto">
43+
<Button text="Home" @tap="goToHomePage" row="0"/>
44+
<Button text="About" @tap="goToAboutPage" row="1"/>
45+
</GridLayout>
46+
</Page>
47+
</template>
48+
<script>
49+
// ~ is an alias to /src
50+
import Home from '~/views/Home';
51+
import About from '~/views/About';
52+
53+
export default {
54+
55+
data() {
56+
return {
57+
navbarTitle: 'App.android.vue',
58+
}
59+
},
60+
methods: {
61+
goToHomePage() {
62+
this.$navigateTo(Home);
63+
},
64+
goToAboutPage() {
65+
this.$navigateTo(About);
66+
}
67+
}
68+
}
69+
70+
</script>
71+
<%_ } _%>
72+
73+
<%_ if (rootOptions.cssPreprocessor !== 'stylus') { _%>
74+
<style<%-
75+
rootOptions.cssPreprocessor
76+
? ` lang="${
77+
rootOptions.cssPreprocessor === 'sass'
78+
? 'scss'
79+
: rootOptions.cssPreprocessor
80+
}"`
81+
: ``
82+
%>>
83+
ActionBar {
84+
color: #42b983;
85+
}
86+
</style>
87+
<%_ } else { _%>
88+
<style lang="stylus">
89+
ActionBar
90+
color: #42b983
91+
</style>
92+
<%_ } _%>

0 commit comments

Comments
 (0)