Skip to content

Commit

Permalink
feat(SSR): SSR support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongkyuuuu committed Sep 30, 2021
1 parent bb37e1b commit 4d9a406
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "NaverMap component for Vue3",
"version": "0.4.0",
"license": "MIT",
"main": "dist/vue3-naver-maps.js",
"main": "dist/vue3-naver-maps.cjs.js",
"module": "dist/vue3-naver-maps.esm.js",
"types": "dist/vue3-naver-maps.d.ts",
"sideEffects": false,
"files": [
Expand Down
86 changes: 60 additions & 26 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,67 @@ import path from "path";
import alias from "@rollup/plugin-alias";
import nodeResolve from "@rollup/plugin-node-resolve";
import typescript from "rollup-plugin-typescript2";
import vuePlugin from "rollup-plugin-vue";
import vue from "rollup-plugin-vue";
import { terser } from "rollup-plugin-terser";

export default {
input: "src/index.ts",
output: [{ file: "dist/vue3-naver-maps.js", format: "es" }],
external: ["vue"],
plugins: [
alias({
entries: [{ find: "@", replacement: path.resolve(__dirname, "src/") }],
}),
nodeResolve({
extensions: [".ts", ".tsx", ".js", ".json", ".vue"],
modules: [path.resolve(__dirname, "./"), "node_modules"],
}),
typescript({
tsconfig: path.resolve(__dirname, "tsconfig.json"),
tsconfigOverride: {
compilerOptions: {
sourceMap: false,
declaration: true,
declarationMap: true,
const pkg = require("./package.json");
const banner = `/*!
* ${pkg.name} v${pkg.version}
* (c) ${new Date().getFullYear()} Dongkyuuuu
* @license MIT
*/`;

const outputConfigs = {
cjs: {
file: pkg.main,
format: "cjs",
},
esm: {
file: pkg.module,
format: "es",
},
};

const createConfigs = (format, output) => {
output.sourcemap = !!process.env.SOURCE_MAP;
output.exports = format === "cjs" ? "named" : "auto";
output.globals = {
vue: "Vue",
};
output.banner = banner;

const pluginVue =
format === "cjs" ? vue({ template: { optimizeSSR: true } }) : vue();

return {
input: "src/index.ts",
external: ["vue"],
plugins: [
alias({
entries: [{ find: "@", replacement: path.resolve(__dirname, "src/") }],
}),
nodeResolve({
extensions: [".ts", ".tsx", ".js", ".json", ".vue"],
modules: [path.resolve(__dirname, "./"), "node_modules"],
}),
typescript({
tsconfig: path.resolve(__dirname, "tsconfig.json"),
tsconfigOverride: {
compilerOptions: {
sourceMap: false,
declaration: true,
declarationMap: true,
},
},
},
exclude: [".yarn", "__tests__"],
}),
vuePlugin(),
terser(),
],
exclude: [".yarn", "__tests__"],
}),
pluginVue,
terser(),
],
output,
};
};

export default Object.keys(outputConfigs).map((format) =>
createConfigs(format, outputConfigs[format])
);
7 changes: 6 additions & 1 deletion src/components/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import {
onUnmounted,
watchEffect,
provide,
inject,
onBeforeMount,
} from "vue";
import { useMapInitOptions, addEventMap, UI_EVENT_MAP } from "../utils";
import { naverMapObject } from "../injectionKeys";
import { naverMapObject, isSSR } from "../injectionKeys";
import { setupScript } from "../config/install";
import type { naverV3 } from "../types";
export default defineComponent({
Expand All @@ -35,6 +38,7 @@ export default defineComponent({
},
},
setup: (props, { emit }) => {
const ssr = inject(isSSR);
const map = ref<naver.maps.Map | null>(null);
const mapRef = ref<HTMLDivElement | null>(null);
const { width, height, mapOptions, initLayers } = toRefs(props);
Expand Down Expand Up @@ -67,6 +71,7 @@ export default defineComponent({
});
});
onBeforeMount(() => (ssr ? setupScript(ssr) : ""));
onMounted(() =>
window.naver ? createMap() : createMapAfterScriptLoaded()
);
Expand Down
5 changes: 3 additions & 2 deletions src/config/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App } from "vue";
import { isSSR } from "../injectionKeys";
import type { naverV3 } from "../types";

export function install(app: App<Element>, options: naverV3.install.options) {
Expand All @@ -8,13 +9,13 @@ export function install(app: App<Element>, options: naverV3.install.options) {

if (!options.clientId) throw new Error(ERROR_MSG_CLIENT);

_setupScript(options);
options.ssr ? app.provide(isSSR, options) : setupScript(options);
}

/**
* vue3-naver-maps script setup
*/
function _setupScript(options: naverV3.install.options) {
export function setupScript(options: naverV3.install.options) {
const isExist = document.getElementById("vue3-naver-maps");
if (!isExist) {
const URL = _createURL(options);
Expand Down
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import NaverInfoWindow from "./components/InfoWindow.vue";
import NaverCircle from "./components/Circle.vue";
import NaverEllipse from "./components/Ellipse.vue";
import NaverRectangle from "./components/Rectangle.vue";
import NaverPolygon from './components/Polygon.vue'
import NaverPolygon from "./components/Polygon.vue";

// export * from "./apis";
export { naverV3 } from "./types";

export {
Expand All @@ -22,8 +21,5 @@ export {
NaverEllipse,
NaverRectangle,
NaverPolygon,
/**
* default
*/
install as default,
};
export default install;
7 changes: 4 additions & 3 deletions src/injectionKeys.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { InjectionKey, Ref } from "vue";
import type { useMap } from "./types";
import { naverV3 } from "./index";

export const naverMapObject: InjectionKey<Ref<naver.maps.Map | null>> = Symbol(
"[vue3-naver-maps]naverMapObject"
);
export const useMapkey: InjectionKey<useMap> = Symbol(
"[vue3-naver-maps]useMap"

export const isSSR: InjectionKey<naverV3.install.options> = Symbol(
"[vue3-naver-maps]Options-SSR"
);
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export declare namespace naverV3 {

interface options {
clientId: string;
ssr: boolean;
category?: category;
subModules?: string;
}
Expand Down

0 comments on commit 4d9a406

Please sign in to comment.