Skip to content

Commit

Permalink
Merge pull request #448 from commercetools/juanjcamposct/connector-st…
Browse files Browse the repository at this point in the history
…yles-cleanup

feat(spa): Cleanup connector styles on closing SPA
  • Loading branch information
juanjcamposct authored Nov 20, 2024
2 parents 26cc10e + 23cdf75 commit 30ddf40
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions enabler/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import { resolve } from 'path';
import { defineConfig } from 'vite';
import { resolve } from "path";
import { defineConfig } from "vite";
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";

export default defineConfig({
plugins: [
cssInjectedByJsPlugin(),
cssInjectedByJsPlugin({
injectCodeFunction: function injectCodeCustomRunTimeFunction(
cssCode: string,
options
) {
try {
if (typeof document != "undefined") {
var elementStyle = document.createElement("style");
// this attribute will allow the client application using this connector to
// identify the style tag and remove it if needed for cleanup purposes
elementStyle.setAttribute("data-ctc-connector-styles", "");
for (const attribute in options.attributes) {
elementStyle.setAttribute(
attribute,
options.attributes[attribute]
);
}
elementStyle.appendChild(document.createTextNode(cssCode));
document.head.appendChild(elementStyle);
}
} catch (e) {
console.error("vite-plugin-css-injected-by-js", e);
}
},
}),
],
build: {
outDir: resolve(__dirname, 'public'),
outDir: resolve(__dirname, "public"),
lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, 'src/main.ts'),
name: 'Connector',
formats: ['es','umd'],
entry: resolve(__dirname, "src/main.ts"),
name: "Connector",
formats: ["es", "umd"],
// the proper extensions will be added
fileName: (format) => `connector-enabler.${format}.js`,
},
},
})
});

0 comments on commit 30ddf40

Please sign in to comment.