mainJs.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.uniMainJsPlugin = void 0;
  4. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  5. const appCss_1 = require("./appCss");
  6. function uniMainJsPlugin({ renderer, appService, }) {
  7. return (0, uni_cli_shared_1.defineUniMainJsPlugin)((opts) => {
  8. return {
  9. name: 'uni:app-nvue-main-js',
  10. enforce: 'pre',
  11. transform(code, id) {
  12. if (opts.filter(id)) {
  13. if (renderer !== 'native') {
  14. return {
  15. code: `import './${uni_cli_shared_1.PAGES_JSON_JS}';import('${appCss_1.APP_CSS_JS}').then(()=>{})`,
  16. map: { mappings: '' },
  17. };
  18. }
  19. if (appService) {
  20. code = code.includes('createSSRApp')
  21. ? createApp(code)
  22. : createLegacyApp(code);
  23. return {
  24. code: `import './${uni_cli_shared_1.MANIFEST_JSON_JS}';\nimport './${uni_cli_shared_1.PAGES_JSON_JS}';\n` +
  25. code,
  26. map: { mappings: '' },
  27. };
  28. }
  29. return {
  30. code: `import './${uni_cli_shared_1.PAGES_JSON_JS}';`,
  31. map: { mappings: '' },
  32. };
  33. }
  34. },
  35. };
  36. });
  37. }
  38. exports.uniMainJsPlugin = uniMainJsPlugin;
  39. function createApp(code) {
  40. return `${code.replace('createSSRApp', 'createVueApp as createSSRApp')};const {app:__app__,Vuex:__Vuex__,Pinia:__Pinia__}=createApp();uni.Vuex=__Vuex__;uni.Pinia=__Pinia__;__app__._component.mpType='app';__app__._component.render=()=>{};__app__.mount('#app');`;
  41. }
  42. function createLegacyApp(code) {
  43. return `function createApp(rootComponent,rootProps){rootComponent.mpTye='app';rootComponent.render=()=>{};const app=createVueApp(rootComponent,rootProps);const oldMount=app.mount;app.mount=(container)=>{const appVm=oldMount.call(app,container);return appVm;};return app;};${code.replace('createApp', 'createVueApp')}`;
  44. }