Skip to content

Commit 023185d

Browse files
committed
can now render babel.config.js as needed
one single App_Resources template dir instead of several
1 parent 825dc2e commit 023185d

File tree

258 files changed

+93
-1702
lines changed

Some content is hidden

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

258 files changed

+93
-1702
lines changed

generator/index.js

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module.exports = (api, options, rootOptions) => {
99
console.log('options.isNVW - ', options.isNVW)
1010
console.log('options.isNewProject - ', options.isNewProject)
1111

12+
const newline = process.platform === 'win32' ? '\r\n' : '\n';
13+
14+
1215
// New Project & Native Only -- should never be able to use Nativescript-Vue-Web
1316
if(options.isNativeOnly && options.isNVW) {
1417
throw Error('Invalid options chosen. You cannot have a Native only project and use Nativescript-Vue-Web')
@@ -46,8 +49,8 @@ module.exports = (api, options, rootOptions) => {
4649
"setup-webpack-config": "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance pre",
4750
"remove-webpack-config": "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance post",
4851
"serve:web": "vue-cli-service serve --mode development.web",
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",
52+
"serve:android": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=development.android tns run android --bundle",
53+
"serve:ios": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=development.ios tns run ios --bundle",
5154
"build:web": "vue-cli-service build --mode production.web",
5255
"build:android": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=production.android tns run android --bundle && npm run remove-webpack-config",
5356
"build:ios": "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=production.ios tns run ios --bundle && npm run remove-webpack-config",
@@ -140,6 +143,14 @@ module.exports = (api, options, rootOptions) => {
140143
// should never reach this block of code
141144
}
142145

146+
// create the babel.config.js file
147+
if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
148+
applyBabelConfig(api, './babel.config.js');
149+
}
150+
151+
// copy App_Resources to the ./app folder
152+
copyDirs('./templates/App_Resources', './app/App_Resources')
153+
143154

144155
} else { // Exising Project
145156

@@ -162,30 +173,52 @@ module.exports = (api, options, rootOptions) => {
162173
// should never reach this block of code
163174
}
164175

176+
// create the babel.config.js file
177+
if(api.hasPlugin('babel') && fs.existsSync('./example/babel.config.js')) {
178+
applyBabelConfig(api, './example/babel.config.js');
179+
}
180+
181+
// copy App_Resources to the ./app folder
182+
copyDirs('./templates/App_Resources', './example/app/App_Resources')
183+
165184
}
166185

167-
168-
169186

170-
187+
188+
171189
api.onCreateComplete(() => {
172190

173-
const newline = process.platform === 'win32' ? '\r\n' : '\n';
174191
const gitignorePath = api.resolve('.gitignore');
175-
const gitignoreWebpackConfig = api.resolve('.webpack.config.js');
176192

177-
// // setup string replacement options for babel.config.js file
178-
// if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
179-
// const replaceOptions = {
180-
// files: './babel.config.js',
181-
// from: ' \'@vue/app\'',
182-
// to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
183-
// }
184-
// replace(replaceOptions, (err, changes) => {
185-
// if (err) throw err;
186-
// });
187-
// }
193+
// setup string replacement options for babel.config.js file
194+
if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
195+
196+
const replaceOptions = {
197+
files: '',
198+
from: ' \'@vue/app\'',
199+
to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
200+
}
201+
202+
// edit babel.config.js with options we need
203+
if(options.isNewProject) {
204+
replaceOptions.files = './babel.config.js';
205+
replace(replaceOptions, (err, changes) => {
206+
if (err) throw err;
207+
});
208+
} else {
209+
replaceOptions.files = './example/babel.config.js';
210+
fs.ensureFile(replaceOptions.files, err => {
211+
if (err) throw err;
212+
replace(replaceOptions, (err, changes) => {
213+
if (err) throw err;
214+
});
215+
})
216+
217+
}
218+
219+
}
188220

221+
189222

190223
// for new projects that are native only, move files/dirs and delete others
191224
if(options.isNewProject && options.isNativeOnly) {
@@ -205,10 +238,16 @@ module.exports = (api, options, rootOptions) => {
205238
})
206239
})
207240

241+
// remove src directory as we don't need it
208242
fs.remove('./src', err => {
209243
if (err) throw err
210244
})
211245

246+
// remove public directory as we don't need it
247+
fs.remove('./public', err => {
248+
if (err) throw err
249+
})
250+
212251
}
213252

214253

@@ -256,3 +295,36 @@ module.exports = (api, options, rootOptions) => {
256295

257296

258297
}
298+
299+
300+
const applyBabelConfig = module.exports.applyBabelConfig = async (api, filePath) => {
301+
302+
try {
303+
api.render(files => {
304+
files[filePath] = api.genJSConfig({
305+
plugins: ["@babel/plugin-syntax-dynamic-import"],
306+
presets: [
307+
'@vue/app'
308+
]
309+
})
310+
})
311+
312+
} catch(err) {
313+
throw err
314+
}
315+
}
316+
317+
const copyDirs = module.exports.copyDirs = async (srcPath, destPath) => {
318+
319+
const baseDir = extractCallDir()
320+
const source = path.resolve(baseDir, srcPath)
321+
await fs.copy(source, destPath)
322+
323+
}
324+
325+
// extract callsite file location using error stack
326+
const extractCallDir = module.exports.extractCallDir = () => {
327+
const obj = {}
328+
Error.captureStackTrace(obj)
329+
return path.dirname(obj.stack.split('\n')[3].match(/\s\((.*):\d+:\d+\)$/)[1])
330+
}

0 commit comments

Comments
 (0)