Skip to content

Commit

Permalink
Consolidate entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Sep 28, 2022
1 parent d996885 commit ea8a4fa
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 44 deletions.
2 changes: 1 addition & 1 deletion dev/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createApp } from 'vue';
import Dev from './serve.vue';
// To register individual components where they are used (serve.vue) instead of using the
// library as a whole, comment/remove this import and it's corresponding "app.use" call
import VueMaplibreGl from '@/entry.esm';
import VueMaplibreGl from '@/entry';

const app = createApp(Dev);
app.use(VueMaplibreGl);
Expand Down
2 changes: 1 addition & 1 deletion dev/serve.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

<script lang="ts">
import { defineComponent, ref, toRef, watch } from 'vue';
import { MglDefaults, MglEvent, StyleSwitchItem, useMap } from '@/entry.esm';
import { MglDefaults, MglEvent, StyleSwitchItem, useMap } from '@/entry';
import { mdiCursorDefaultClick } from '@mdi/js';
import { LineLayout, LinePaint, MapLayerMouseEvent } from 'maplibre-gl';
Expand Down
25 changes: 0 additions & 25 deletions src/entry.esm.ts

This file was deleted.

38 changes: 22 additions & 16 deletions src/entry.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// iife/cjs usage extends esm default export - so import it all
import plugin, * as components from '@/entry.esm';
import type { App } from 'vue';

// Attach named exports directly to plugin. IIFE/CJS will
// only expose one global var, with component exports exposed as properties of
// that global var (eg. plugin.component)
type NamedExports = Exclude<typeof components, 'default'>;
type ExtendedPlugin = typeof plugin & NamedExports;
Object.entries(components).forEach(([ componentName, component ]) => {
if (componentName !== 'default') {
const key = componentName as Exclude<keyof NamedExports, 'default'>;
const val = component as Exclude<ExtendedPlugin, typeof plugin>;
// @ts-ignore
(plugin as ExtendedPlugin)[ key ] = val;
}
});
// Import vue components
import * as components from '@/components/index';

export default plugin;
// install function executed by Vue.use()
// Can be passed as either Vue.use(install) or Vue.use(thisModule)
export function install(app: App) {
Object.entries(components).forEach(([componentName, component]) => {
app.component(componentName, component);
});
}

// To allow individual component use, export components
// each can be registered via Vue.component()
export * from '@/components/index';

// addition exports
export * from '@/components/types';
export { useMap } from './components/mapRegistry';
export { defaults as MglDefaults } from './components/defaults';
export { usePositionWatcher, Position } from './components/controls/shared';

export default module.exports;
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({

build: {
lib: {
entry: resolve(__dirname, 'src/entry.esm.ts'),
entry: resolve(__dirname, 'src/entry.ts'),
name: 'VueMaplibreGl',
fileName: (format) => {
let desc = { cjs: 'ssr', es: 'esm', iife: 'min' }[format] || format;
Expand Down

0 comments on commit ea8a4fa

Please sign in to comment.