Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replacing all usages of path.join with posixJoin (#29) #30

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ const REGEXES = {
HASH_REGEX: '(?<hash>[A-Z2-7]{8})',
NAME_REGEX: '(?<name>[^\\s\\/]+)',
};
// This function joins a path, and in case of windows, it converts backward slashes ('\') forward slashes ('/').
function posixJoin(...paths) {
const joined = path_1.default.join(...paths);
if (path_1.default.sep === '/') {
return joined;
}
return joined.split(path_1.default.sep).join(path_1.default.posix.sep);
}
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
Expand Down Expand Up @@ -53,7 +61,7 @@ const htmlPlugin = (configuration = { files: [], }) => {
// We therefore try to extract the dir, name and hash from the "main"-output, and try to find all files with
// the same [name] and [dir].
// This should always include the "main"-output, as well as all relatedOutputs
const joinedPathOfMatch = path_1.default.join(pathOfMatchedOutput.dir, pathOfMatchedOutput.name);
const joinedPathOfMatch = posixJoin(pathOfMatchedOutput.dir, pathOfMatchedOutput.name);
const findVariablesRegexString = escapeRegExp(entryNames)
.replace('\\[hash\\]', REGEXES.HASH_REGEX)
.replace('\\[name\\]', REGEXES.NAME_REGEX)
Expand Down Expand Up @@ -115,7 +123,7 @@ const htmlPlugin = (configuration = { files: [], }) => {
targetPath = joinWithPublicPath(publicPath, path_1.default.relative(outDir, filepath));
}
else {
const htmlFileDirectory = path_1.default.join(outDir, htmlFileConfiguration.filename);
const htmlFileDirectory = posixJoin(outDir, htmlFileConfiguration.filename);
targetPath = path_1.default.relative(path_1.default.dirname(htmlFileDirectory), filepath);
}
const ext = path_1.default.parse(filepath).ext;
Expand Down Expand Up @@ -200,7 +208,7 @@ const htmlPlugin = (configuration = { files: [], }) => {
document.head.appendChild(linkTag);
}
injectFiles(dom, collectedOutputFiles, outdir, publicPath, htmlFileConfiguration);
const out = path_1.default.join(outdir, htmlFileConfiguration.filename);
const out = posixJoin(outdir, htmlFileConfiguration.filename);
await fs_1.promises.mkdir(path_1.default.dirname(out), {
recursive: true,
});
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ const REGEXES = {
NAME_REGEX: '(?<name>[^\\s\\/]+)',
}

// This function joins a path, and in case of windows, it converts backward slashes ('\') forward slashes ('/').
function posixJoin(...paths: string[]): string {
const joined = path.join(...paths)
if (path.sep === '/') {
return joined
}
return joined.split(path.sep).join(path.posix.sep)
}

function escapeRegExp(text: string): string {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}
Expand Down Expand Up @@ -71,7 +80,7 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui
// We therefore try to extract the dir, name and hash from the "main"-output, and try to find all files with
// the same [name] and [dir].
// This should always include the "main"-output, as well as all relatedOutputs
const joinedPathOfMatch = path.join(pathOfMatchedOutput.dir, pathOfMatchedOutput.name)
const joinedPathOfMatch = posixJoin(pathOfMatchedOutput.dir, pathOfMatchedOutput.name)
const findVariablesRegexString = escapeRegExp(entryNames)
.replace('\\[hash\\]', REGEXES.HASH_REGEX)
.replace('\\[name\\]', REGEXES.NAME_REGEX)
Expand Down Expand Up @@ -140,7 +149,7 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui
if (publicPath) {
targetPath = joinWithPublicPath(publicPath, path.relative(outDir, filepath))
} else {
const htmlFileDirectory = path.join(outDir, htmlFileConfiguration.filename)
const htmlFileDirectory = posixJoin(outDir, htmlFileConfiguration.filename)
targetPath = path.relative(path.dirname(htmlFileDirectory), filepath)
}
const ext = path.parse(filepath).ext
Expand Down Expand Up @@ -238,7 +247,7 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui

injectFiles(dom, collectedOutputFiles, outdir, publicPath, htmlFileConfiguration)

const out = path.join(outdir, htmlFileConfiguration.filename)
const out = posixJoin(outdir, htmlFileConfiguration.filename)
await fs.mkdir(path.dirname(out), {
recursive: true,
})
Expand Down