|
1 | 1 | const{ VueLoaderPlugin}=require('vue-loader');
|
| 2 | +constspawn=require('cross-spawn'); |
| 3 | + |
| 4 | +functionfindFreePort(startingPort=8098){ |
| 5 | +letfound=false; |
| 6 | +letport=startingPort; |
| 7 | + |
| 8 | +constisPortFree=(port)=> |
| 9 | +newPromise((resolve)=>{ |
| 10 | +constserver=require('http') |
| 11 | +.createServer() |
| 12 | +.listen(port,'0.0.0.0',()=>{ |
| 13 | +server.close(); |
| 14 | +resolve(true); |
| 15 | +}) |
| 16 | +.on('error',()=>{ |
| 17 | +resolve(false); |
| 18 | +}); |
| 19 | +}); |
| 20 | + |
| 21 | +constfindFreePort=()=>{ |
| 22 | +isPortFree(port).then((isFree)=>{ |
| 23 | +if(!isFree){ |
| 24 | +port++; |
| 25 | +returnfindFreePort(); |
| 26 | +} |
| 27 | +found=true; |
| 28 | +}); |
| 29 | +}; |
| 30 | + |
| 31 | +findFreePort(); |
| 32 | + |
| 33 | +while(!found){ |
| 34 | +process._tickCallback(); |
| 35 | +conststart=Date.now(); |
| 36 | +while(Date.now()-start<100){ |
| 37 | +// busy wait... not ideal, but we need to find a port synchronously... |
| 38 | +} |
| 39 | +} |
| 40 | + |
| 41 | +returnport; |
| 42 | +} |
| 43 | + |
| 44 | +functionstartVueDevtools(port,isAndroid=false){ |
| 45 | +console.log(`[VueDevtools] Starting standalone Vue Devtools on port${port}`); |
| 46 | +if(isAndroid){ |
| 47 | +console.log( |
| 48 | +`[VueDevtools] If the app doesn't automatically connect, check if http traffic is allowed. (e.g. on Android, you may need to set android:usesCleartextTraffic="true" in AndroidManifest.xml)` |
| 49 | +); |
| 50 | +} |
| 51 | +spawn(require.resolve('@vue/devtools/bin.js'),[],{ |
| 52 | +stdio:'ignore', |
| 53 | +env:{ |
| 54 | + ...process.env, |
| 55 | +PORT:port, |
| 56 | +}, |
| 57 | +}); |
| 58 | +} |
2 | 59 |
|
3 | 60 | /**
|
4 | 61 | *@param {typeof import("@nativescript/webpack")} webpack
|
5 | 62 | */
|
6 | 63 | module.exports=(webpack)=>{
|
7 | 64 | webpack.useConfig('vue');
|
8 | 65 |
|
9 |
| -webpack.chainWebpack((config)=>{ |
| 66 | +webpack.chainWebpack((config,env)=>{ |
| 67 | +constadditionalDefines={ |
| 68 | +__VUE_PROD_DEVTOOLS__:false, |
| 69 | +}; |
| 70 | + |
| 71 | +// todo: support configuring the devtools host/port from the nativescript.config.ts... |
| 72 | +if(!!env.vueDevtools){ |
| 73 | +// find a free port for the devtools |
| 74 | +constvueDevtoolsPort=findFreePort(8098); |
| 75 | +constisAndroid=webpack.Utils.platform.getPlatformName()==='android'; |
| 76 | + |
| 77 | +// on android simulators, localhost is not the host machine... |
| 78 | +constvueDevtoolsHost=isAndroid |
| 79 | + ?'http://10.0.2.2' |
| 80 | + :'http://localhost'; |
| 81 | + |
| 82 | +additionalDefines['__VUE_PROD_DEVTOOLS__']=true; |
| 83 | +additionalDefines['__NS_VUE_DEVTOOLS_HOST__']= |
| 84 | +JSON.stringify(vueDevtoolsHost); |
| 85 | +additionalDefines['__NS_VUE_DEVTOOLS_PORT__']=vueDevtoolsPort; |
| 86 | + |
| 87 | +constdevtoolsEntryPath=require.resolve('./devtools.js'); |
| 88 | +constentryPath=webpack.Utils.platform.getEntryPath(); |
| 89 | +constpaths=config.entry('bundle').values(); |
| 90 | +constentryIndex=paths.indexOf(entryPath); |
| 91 | + |
| 92 | +if(entryIndex===-1){ |
| 93 | +// if the app entry is not found, add the devtools entry at the beginning - generally should not happen, but just in case. |
| 94 | +paths.unshift(entryPath); |
| 95 | +}else{ |
| 96 | +// insert devtools entry before the app entry, but after globals etc. |
| 97 | +paths.splice(entryIndex,0,devtoolsEntryPath); |
| 98 | +} |
| 99 | + |
| 100 | +config.entry('bundle').clear().merge(paths); |
| 101 | + |
| 102 | +// start the devtools... |
| 103 | +startVueDevtools(vueDevtoolsPort,isAndroid); |
| 104 | +} |
| 105 | + |
10 | 106 | // resolve any imports from "vue" to "nativescript-vue"
|
11 | 107 | config.resolve.alias.set('vue','nativescript-vue');
|
12 | 108 |
|
@@ -41,7 +137,7 @@ module.exports = (webpack) => {
|
41 | 137 | config.plugin('DefinePlugin').tap((args)=>{
|
42 | 138 | Object.assign(args[0],{
|
43 | 139 | __VUE_OPTIONS_API__:true,
|
44 |
| -__VUE_PROD_DEVTOOLS__:false, |
| 140 | +...additionalDefines, |
45 | 141 | });
|
46 | 142 |
|
47 | 143 | returnargs;
|
|