Skip to content

Commit

Permalink
fix: Error rendering manifest on Windows (#75)
Browse files Browse the repository at this point in the history
Window's path separators caused mismatches between the paths in Vite's bundle output's. This meant the manifest couldn't find the outputs for input files, and failed to render.

Co-authored-by: Aaron Klinker <[email protected]>
  • Loading branch information
yjlin0224 and aklinker1 authored Mar 3, 2023
1 parent 25f54d8 commit a38672c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
81 changes: 81 additions & 0 deletions packages/vite-plugin-web-extension/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as vite from "vite";
import { describe, expect, it } from "vitest";
import { ProjectPaths } from "./options";
import {
getInputPaths,
getOutputFile,
removePlugin,
resolveBrowserTagsInObject,
Expand Down Expand Up @@ -184,4 +186,83 @@ describe("Utils", () => {
expect(getOutputFile(input)).toEqual(expected);
});
});

describe("getInputPaths", () => {
const paths: ProjectPaths = {
outDir: "/path/to/project/dist",
rootDir: "/path/to/project",
publicDir: "/path/to/project/public",
};

it("should keep relative paths relative", () => {
const inputs = ["popup/index.html", "background.ts"];
const expected = inputs;

const actual = getInputPaths(paths, inputs);

expect(actual).toEqual(expected);
});

it("should make absolute paths relative", () => {
const inputs = [
"/path/to/content-script.ts",
"/path/to/project/src/options.html",
];
const expected = ["../content-script.ts", "src/options.html"];

const actual = getInputPaths(paths, inputs);

expect(actual).toEqual(expected);
});

it("should convert window paths to unix", () => {
const winPaths: ProjectPaths = {
outDir: "C:\\path\\to\\project\\dist",
rootDir: "C:\\path\\to\\project",
publicDir: "C:\\path\\to\\project\\public",
};
const inputs = ["src\\popup.html"];
const expected = ["src/popup.html"];

const actual = getInputPaths(winPaths, inputs);

expect(actual).toEqual(expected);
});

it("should work with a single, rollup input string", () => {
const input = "index.ts";
const expected = [input];

const actual = getInputPaths(paths, input);

expect(actual).toEqual(expected);
});

it("should work with a list of rollup input strings", () => {
const inputList = ["index.ts", "index.js"];
const expected = inputList;

const actual = getInputPaths(paths, inputList);

expect(actual).toEqual(expected);
});

it("should return the values when a record of rollup inputs is passed in", () => {
const inputRecord = { one: "index.ts", two: "index.js" };
const expected = ["index.ts", "index.js"];

const actual = getInputPaths(paths, inputRecord);

expect(actual).toEqual(expected);
});

it("should work with Vite library options", () => {
const inputRecord: vite.LibraryOptions = { entry: "src/test.ts" };
const expected = [inputRecord.entry];

const actual = getInputPaths(paths, inputRecord);

expect(actual).toEqual(expected);
});
});
});
9 changes: 6 additions & 3 deletions packages/vite-plugin-web-extension/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export function getPublicDir(config: vite.ResolvedConfig): string | undefined {
}

/**
* Return all the input file paths relative to vite's root.
* Return all the input file paths relative to vite's root. They must be relative paths with unix
* separators because they must match vite's bundle output paths. Otherwise the manifest will fail
* to render because a path is different than expected (see #74).
*/
export function getInputPaths(
paths: ProjectPaths,
Expand All @@ -133,8 +135,9 @@ export function getInputPaths(
else inputs = Object.values(input);

return inputs.map((file) => {
if (path.isAbsolute(file)) return path.relative(paths.rootDir, file);
return file;
if (path.isAbsolute(file))
return path.relative(paths.rootDir, file).replaceAll("\\", "/");
return file.replaceAll("\\", "/");
});
}

Expand Down

0 comments on commit a38672c

Please sign in to comment.