Support theming in dev server

This commit is contained in:
RMidhunSuresh 2022-04-10 14:49:19 +05:30
parent a6b6fef6d2
commit ff98ef4465

View file

@ -13,17 +13,45 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const path = require('path');
async function readCSSSource(location) { async function readCSSSource(location) {
const fs = require("fs").promises; const fs = require("fs").promises;
const path = require("path"); const path = require("path");
const resolvedLocation = path.resolve(__dirname, "../../", `${location}/theme.css`); const resolvedLocation = path.resolve(__dirname, "../../", `${location}/theme.css`);
const data = await fs.readFile(resolvedLocation); const data = await fs.readFile(resolvedLocation);
return data; return data;
} }
async function appendVariablesToCSS(variables, cssSource) { function getRootSectionWithVariables(variables) {
return cssSource + `:root{\n${Object.entries(variables).reduce((acc, [key, value]) => acc + `--${key}: ${value};\n`, "")} }\n\n`; return `:root{\n${Object.entries(variables).reduce((acc, [key, value]) => acc + `--${key}: ${value};\n`, "")} }\n\n`;
}
function appendVariablesToCSS(variables, cssSource) {
return cssSource + getRootSectionWithVariables(variables);
}
function findLocationFromThemeName(name, locations) {
const themeLocation = locations.find(location => {
const manifest = require(`${location}/manifest.json`);
if (manifest.name === name) {
return true;
}
});
if (!themeLocation) {
throw new Error(`Cannot find location from theme name "${name}"`);
}
return themeLocation;
}
function findManifestFromThemeName(name, locations) {
for (const location of locations) {
const manifest = require(`${location}/manifest.json`);
if (manifest.name === name) {
return manifest;
}
}
throw new Error(`Cannot find manifest from theme name "${name}"`);
} }
function parseBundle(bundle) { function parseBundle(bundle) {
@ -68,11 +96,20 @@ function parseBundle(bundle) {
module.exports = function buildThemes(options) { module.exports = function buildThemes(options) {
let manifest, variants, defaultDark, defaultLight; let manifest, variants, defaultDark, defaultLight;
let isDevelopment = false;
const virtualModuleId = '@theme/'
const resolvedVirtualModuleId = '\0' + virtualModuleId;
return { return {
name: "build-themes", name: "build-themes",
enforce: "pre", enforce: "pre",
configResolved(config) {
if (config.command === "serve") {
isDevelopment = true;
}
},
async buildStart() { async buildStart() {
const { manifestLocations } = options; const { manifestLocations } = options;
for (const location of manifestLocations) { for (const location of manifestLocations) {
@ -106,69 +143,108 @@ module.exports = function buildThemes(options) {
} }
}, },
resolveId(id) {
if (id.startsWith(virtualModuleId)) {
return isDevelopment? '\0' + id: false;
}
},
async load(id) { async load(id) {
const result = id.match(/(.+)\/theme.css\?variant=(.+)/); if (isDevelopment) {
if (result) { if (id.startsWith(resolvedVirtualModuleId)) {
const [, location, variant] = result; let [theme, variant, file] = id.substr(resolvedVirtualModuleId.length).split("/");
const cssSource = await readCSSSource(location); if (theme === "default") {
const config = variants[variant]; theme = "Element";
return await appendVariablesToCSS(config.variables, cssSource); }
} if (!variant || variant === "default") {
return null; variant = "light";
}, }
if (!file) {
transformIndexHtml(_, ctx) { file = "index.js";
let darkThemeLocation, lightThemeLocation; }
for (const [, bundle] of Object.entries(ctx.bundle)) { switch (file) {
if (bundle.name === defaultDark) { case "index.js": {
darkThemeLocation = bundle.fileName; const location = findLocationFromThemeName(theme, options.manifestLocations);
} return `import "${path.resolve(`${location}/theme.css`)}";` +
if (bundle.name === defaultLight) { `import "@theme/${theme}/${variant}/variables.css"`;
lightThemeLocation = bundle.fileName; }
case "variables.css": {
const manifest = findManifestFromThemeName(theme, options.manifestLocations);
const variables = manifest.values.variants[variant].variables;
const css = getRootSectionWithVariables(variables);
return css;
}
}
} }
} }
return [ else {
{ const result = id.match(/(.+)\/theme.css\?variant=(.+)/);
tag: "link", if (result) {
attrs: { const [, location, variant] = result;
rel: "stylesheet", const cssSource = await readCSSSource(location);
type: "text/css", const config = variants[variant];
media: "(prefers-color-scheme: dark)", return await appendVariablesToCSS(config.variables, cssSource);
href: `./${darkThemeLocation}`, }
} return null;
}, }
{ },
tag: "link",
attrs: {
rel: "stylesheet",
type: "text/css",
media: "(prefers-color-scheme: light)",
href: `./${lightThemeLocation}`,
}
},
];
},
generateBundle(_, bundle) { transformIndexHtml(_, ctx) {
const { assetMap, chunkMap, runtimeThemeChunk } = parseBundle(bundle); if (isDevelopment) {
for (const [location, chunkArray] of chunkMap) { // Don't add default stylesheets to index.html on dev
const manifest = require(`${location}/manifest.json`); return;
const compiledVariables = options.compiledVariables.get(location); }
const derivedVariables = compiledVariables["derived-variables"]; let darkThemeLocation, lightThemeLocation;
const icon = compiledVariables["icon"]; for (const [, bundle] of Object.entries(ctx.bundle)) {
manifest.source = { if (bundle.name === defaultDark) {
"built-asset": chunkArray.map(chunk => assetMap.get(chunk.fileName).fileName), darkThemeLocation = bundle.fileName;
"runtime-asset": assetMap.get(runtimeThemeChunk.fileName).fileName, }
"derived-variables": derivedVariables, if (bundle.name === defaultLight) {
"icon": icon lightThemeLocation = bundle.fileName;
};
const name = `theme-${manifest.name}.json`;
this.emitFile({
type: "asset",
name,
source: JSON.stringify(manifest),
});
} }
} }
return [
{
tag: "link",
attrs: {
rel: "stylesheet",
type: "text/css",
media: "(prefers-color-scheme: dark)",
href: `./${darkThemeLocation}`,
}
},
{
tag: "link",
attrs: {
rel: "stylesheet",
type: "text/css",
media: "(prefers-color-scheme: light)",
href: `./${lightThemeLocation}`,
}
},
];
},
generateBundle(_, bundle) {
const { assetMap, chunkMap, runtimeThemeChunk } = parseBundle(bundle);
for (const [location, chunkArray] of chunkMap) {
const manifest = require(`${location}/manifest.json`);
const compiledVariables = options.compiledVariables.get(location);
const derivedVariables = compiledVariables["derived-variables"];
const icon = compiledVariables["icon"];
manifest.source = {
"built-asset": chunkArray.map(chunk => assetMap.get(chunk.fileName).fileName),
"runtime-asset": assetMap.get(runtimeThemeChunk.fileName).fileName,
"derived-variables": derivedVariables,
"icon": icon
};
const name = `theme-${manifest.name}.json`;
this.emitFile({
type: "asset",
name,
source: JSON.stringify(manifest),
});
}
}
} }
} }