Skip to content

Commit

Permalink
add share interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Jazi committed May 8, 2024
1 parent 70515df commit efef259
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
75 changes: 67 additions & 8 deletions src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -101,7 +113,7 @@ describe("plugin", function () {
plugin.generateBundle(
{},
{
"source": {
source: {
type: "chunk",
fileName: "filename.js",
},
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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",
});
});
});
17 changes: 13 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export interface PageInclude {
export interface PageModuleConfig extends ModuleConfigBase {
type: "page";
pages: (string | PageInclude)[];
share?: {
name: string;
};
}

type ModuleConfig = ObservedModuleConfig | PageModuleConfig;
Expand Down Expand Up @@ -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;
};
Expand Down
12 changes: 12 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit efef259

Please sign in to comment.