mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-18 19:46:57 +02:00
fix(v2): windows compatibility (#1353)
This commit is contained in:
parent
15bc33df50
commit
5872e2dd91
4 changed files with 37 additions and 3 deletions
|
@ -13,9 +13,23 @@ import {
|
||||||
idx,
|
idx,
|
||||||
getSubFolder,
|
getSubFolder,
|
||||||
normalizeUrl,
|
normalizeUrl,
|
||||||
|
posixPath,
|
||||||
} from '../index';
|
} from '../index';
|
||||||
|
|
||||||
describe('load utils', () => {
|
describe('load utils', () => {
|
||||||
|
test('posixPath', () => {
|
||||||
|
const asserts = {
|
||||||
|
'c:/aaaa\\bbbb': 'c:/aaaa/bbbb',
|
||||||
|
'c:\\aaaa\\bbbb\\★': 'c:\\aaaa\\bbbb\\★',
|
||||||
|
'\\\\?\\c:\\aaaa\\bbbb': '\\\\?\\c:\\aaaa\\bbbb',
|
||||||
|
'c:\\aaaa\\bbbb': 'c:/aaaa/bbbb',
|
||||||
|
'foo\\bar': 'foo/bar',
|
||||||
|
};
|
||||||
|
Object.keys(asserts).forEach(file => {
|
||||||
|
expect(posixPath(file)).toBe(asserts[file]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('fileToComponentName', () => {
|
test('fileToComponentName', () => {
|
||||||
const asserts = {
|
const asserts = {
|
||||||
'index.md': 'MDIndex',
|
'index.md': 'MDIndex',
|
||||||
|
|
|
@ -49,6 +49,21 @@ function fileToComponentName(file) {
|
||||||
return ext ? ext.toUpperCase() + str : str;
|
return ext ? ext.toUpperCase() + str : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Windows backslash paths to posix style paths. E.g: endi\\lie -> endi/lie
|
||||||
|
* @param {string} str windows backslash paths
|
||||||
|
* @returns {string} posix-style path
|
||||||
|
*/
|
||||||
|
function posixPath(str) {
|
||||||
|
const isExtendedLengthPath = /^\\\\\?\\/.test(str);
|
||||||
|
const hasNonAscii = /[^\u0000-\u0080]+/.test(str); // eslint-disable-line
|
||||||
|
|
||||||
|
if (isExtendedLengthPath || hasNonAscii) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
return str.replace(/\\/g, '/');
|
||||||
|
}
|
||||||
|
|
||||||
function generateChunkName(str, prefix) {
|
function generateChunkName(str, prefix) {
|
||||||
const name = str === '/' ? 'index' : kebabHash(str);
|
const name = str === '/' ? 'index' : kebabHash(str);
|
||||||
return prefix ? `${prefix}---${name}` : name;
|
return prefix ? `${prefix}---${name}` : name;
|
||||||
|
@ -153,4 +168,5 @@ module.exports = {
|
||||||
idx,
|
idx,
|
||||||
normalizeUrl,
|
normalizeUrl,
|
||||||
parse,
|
parse,
|
||||||
|
posixPath,
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {generate} = require('@docusaurus/utils');
|
const {generate, posixPath} = require('@docusaurus/utils');
|
||||||
|
|
||||||
module.exports = async function loadPlugins({pluginConfigs = [], context}) {
|
module.exports = async function loadPlugins({pluginConfigs = [], context}) {
|
||||||
// 1. Plugin Lifecycle - Initialization/Constructor
|
// 1. Plugin Lifecycle - Initialization/Constructor
|
||||||
|
@ -48,7 +48,10 @@ module.exports = async function loadPlugins({pluginConfigs = [], context}) {
|
||||||
metadataFileName,
|
metadataFileName,
|
||||||
JSON.stringify(content, null, 2),
|
JSON.stringify(content, null, 2),
|
||||||
);
|
);
|
||||||
const contentPath = path.join('@generated', pluginContentPath);
|
// Note that we need to convert it into POSIX format because
|
||||||
|
// import XXXXX from '@generated\this-is\my\path' is incorrect
|
||||||
|
// import XXXXX from '@generated/this-is/my/path' is correct
|
||||||
|
const contentPath = posixPath(path.join('@generated', pluginContentPath));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
metadataKey,
|
metadataKey,
|
||||||
|
|
|
@ -34,7 +34,8 @@ async function loadRoutes(pluginsRouteConfigs) {
|
||||||
const importStr = isObj ? target.path : target;
|
const importStr = isObj ? target.path : target;
|
||||||
const queryStr = target.query ? `?${stringify(target.query)}` : '';
|
const queryStr = target.query ? `?${stringify(target.query)}` : '';
|
||||||
const chunkName = generateChunkName(name || importStr, prefix);
|
const chunkName = generateChunkName(name || importStr, prefix);
|
||||||
return `() => import(/* webpackChunkName: '${chunkName}' */ '${importStr}${queryStr}')`;
|
const finalStr = JSON.stringify(importStr + queryStr);
|
||||||
|
return `() => import(/* webpackChunkName: '${chunkName}' */ ${finalStr})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateRouteCode(pluginRouteConfig) {
|
function generateRouteCode(pluginRouteConfig) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue