use null marker in id

quicker-redirects
gtmnayan 1 year ago
parent 97bae379ec
commit 0860cf3c4b

@ -1,5 +1,5 @@
// @ts-expect-error custom suffix doesn't have types
import js from './redirect.js?minified';
import js from 'minified-raw:./redirect.js';
// prerenderer will choke otherwise
export const prerender = false;

@ -4,24 +4,50 @@ import { readFile } from 'node:fs/promises';
import browserslist from 'browserslist';
import { transformWithEsbuild } from 'vite';
/** @type {import('vite').Plugin[]} */
const plugins = [
raw(['.ttf']),
sveltekit(),
{
name: 'minified-raw-js',
/**
* @param {string[]} ext
* @returns {import("vite").Plugin}
*/
function raw(ext) {
return {
name: 'vite-plugin-raw',
async transform(_, id) {
if (ext.some((e) => id.endsWith(e))) {
const buffer = await readFile(id);
return { code: `export default ${JSON.stringify(buffer)}`, map: null };
}
}
};
}
/** @returns {import('vite').Plugin} */
function minified_raw_plugin() {
const prefix = 'minified-raw:';
return {
name: 'minified-raw-js',
async resolveId(id, importer) {
if (id.startsWith(prefix)) {
const resolved = await this.resolve(id.slice(prefix.length), importer);
return '\0' + prefix + resolved.id;
}
},
async load(id) {
if (id.endsWith('.js?minified')) {
const file = id.replace('?minified', '');
let code = await readFile(file, 'utf-8');
return `export default ${JSON.stringify(
(await transformWithEsbuild(code, file, { minify: true, format: 'esm' })).code
)}`;
if (id.startsWith('\0' + prefix)) {
const real_id = id.slice(1 + prefix.length);
const original = await readFile(real_id, 'utf-8');
const { code } = await transformWithEsbuild(original, real_id, {
minify: true,
format: 'esm'
});
return `export default ${JSON.stringify(code)}`;
}
}
}
];
};
}
/** @type {import('vite').Plugin[]} */
const plugins = [raw(['.ttf']), sveltekit(), minified_raw_plugin()];
// Only enable sharp if we're not in a webcontainer env
if (!process.versions.webcontainer) {
@ -38,22 +64,6 @@ if (!process.versions.webcontainer) {
);
}
/**
* @param {string[]} ext
* @returns {import("vite").Plugin}
*/
function raw(ext) {
return {
name: 'vite-plugin-raw',
async transform(_, id) {
if (ext.some((e) => id.endsWith(e))) {
const buffer = await readFile(id);
return { code: `export default ${JSON.stringify(buffer)}`, map: null };
}
}
};
}
/** @type {import('vite').UserConfig} */
const config = {
logLevel: 'info',

Loading…
Cancel
Save