diff --git a/package.json b/package.json index 2c59ccc..7bac85f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openreplyde/web-docker-vite-plugin", - "version": "1.0.0-alpha.2", + "version": "1.0.0", "type": "module", "main": "lib/plugin.js", "files": [ diff --git a/src/plugin.spec.ts b/src/plugin.spec.ts index 4a8d6ca..0f4217d 100644 --- a/src/plugin.spec.ts +++ b/src/plugin.spec.ts @@ -8,12 +8,12 @@ describe("plugin", function () { selector: "some-element", }; - const plugin = create({basePath: "/", fileName: "filename", entry: './test', ...config}); + const plugin = create({ basePath: "/", fileName: "filename", ...config }); expect(plugin.name).toEqual("ViteWebDockerRemoteFile"); }); - it("generates remote config of event module type", function () { + it("generates remote config of observed module type", function () { expect.assertions(2); const config: ObservedModuleConfig = { @@ -22,7 +22,11 @@ describe("plugin", function () { selector: "some-element", }; - const plugin = create({basePath: "/", fileName: "filename", entry: './test', ...config}) as { + const plugin = create({ + basePath: "/", + fileName: "filename", + ...config, + }) as { name: string; generateBundle: (plugin: any, {}, {}) => void; emitFile: (arg0: { @@ -55,7 +59,11 @@ describe("plugin", function () { pages: ["select", "/heyobi/.*"], }; - const plugin = create({basePath: "/", fileName: "filename", entry: './test', ...config}) as { + const plugin = create({ + basePath: "/", + fileName: "filename", + ...config, + }) as { name: string; generateBundle: (plugin: any, {}, {}) => void; emitFile: (arg0: { @@ -86,7 +94,11 @@ describe("plugin", function () { pages: [], }; - const plugin = create({basePath: "/", fileName: "filename", entry: './test', ...config}) as { + const plugin = create({ + basePath: "/", + fileName: "filename", + ...config, + }) as { name: string; generateBundle: ({}, {}) => void; emitFile: (arg0: { @@ -101,7 +113,7 @@ describe("plugin", function () { plugin.generateBundle( {}, { - "source": { + source: { type: "chunk", fileName: "filename.js", }, @@ -125,7 +137,11 @@ describe("plugin", function () { pages: [], }; - const plugin = create({basePath: "/", fileName: "filename", entry: './test', ...config}) as { + const plugin = create({ + basePath: "/", + fileName: "filename", + ...config, + }) as { name: string; generateBundle: ({}, {}) => void; emitFile: (arg0: { @@ -164,7 +180,11 @@ describe("plugin", function () { pages: [], }; - const plugin = create({basePath: "/", fileName: "filename", entry: './test', ...config}) as { + const plugin = create({ + basePath: "/", + fileName: "filename", + ...config, + }) as { name: string; generateBundle: ({}, {}) => void; emitFile: (arg0: { @@ -197,4 +217,43 @@ describe("plugin", function () { type: "asset", }); }); + it("generates remote config of page module type as shared module", function () { + expect.assertions(2); + + const config: PageModuleConfig = { + pages: [], + type: "page", + module: "some-element", + share: { + name: "vue", + }, + }; + + const plugin = create({ + basePath: "/", + fileName: "filename", + ...config, + }) as { + name: string; + generateBundle: (plugin: any, {}, {}) => void; + emitFile: (arg0: { + fileName: string; + source: string; + type: string; + }) => void; + }; + + plugin.emitFile = vi.fn(); + + expect(plugin.name).toEqual("ViteWebDockerRemoteFile"); + + plugin.generateBundle(plugin, {}, {}); + + expect(plugin.emitFile).toHaveBeenCalledWith({ + fileName: "filename", + source: + '{"version":"1.0.0","type":"page","assets":[],"module":"some-element","pages":[],"share":{"name":"vue"}}', + type: "asset", + }); + }); }); diff --git a/src/plugin.ts b/src/plugin.ts index a704a16..bc4e461 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -20,6 +20,9 @@ export interface PageInclude { export interface PageModuleConfig extends ModuleConfigBase { type: "page"; pages: (string | PageInclude)[]; + share?: { + name: string; + }; } type ModuleConfig = ObservedModuleConfig | PageModuleConfig; @@ -87,10 +90,16 @@ export const create = ( if (config.type === "observed") { configString += `"selector":"${config.selector}"}`; - } else if (config.type === "page" && config.pages) { - configString += `"pages":[${config.pages - .map((p) => `"${p}"`) - .join(",")}]}`; + } else if (config.type === "page") { + if (config.pages) { + configString += `"pages":[${config.pages + .map((p) => `"${p}"`) + .join(",")}]`; + } + if (config.share) { + configString += `,"share":{"name":"${config.share.name}"}`; + } + configString += "}"; } return configString; }; diff --git a/vite.config.ts b/vite.config.ts index fc550e5..7e1a4dd 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -27,6 +27,18 @@ export default defineConfig(({ mode }) => { type: "page", } ), + create( + { + basePath: base, + pages: [".*"], + fileName: "remote-config-test-page-module-shared.json", + module: "page-share-module", + type: "page", + share: { + name: "vue" + } + } + ), ], build: { rollupOptions: {