fix: nested static in static breaks build (#953)

This commit is contained in:
Endilie Yacop Sucipto 2018-09-12 07:31:10 +08:00 committed by Yangshun Tay
parent 11faa436ea
commit 4a2b7cf614

View file

@ -159,15 +159,12 @@ async function execute() {
} }
// copy all static files from docusaurus // copy all static files from docusaurus
files = glob.sync(join(__dirname, '..', 'static', '**')); const libStaticDir = join(__dirname, '..', 'static');
files = glob.sync(join(libStaticDir, '**'));
files.forEach(file => { files.forEach(file => {
// Why normalize? In case we are on Windows. // Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows // Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
let targetFile = path.normalize(file); const targetFile = path.normalize(file).replace(libStaticDir, buildDir);
targetFile = join(
buildDir,
targetFile.split(`${sep}static${sep}`)[1] || ''
);
// parse css files to replace colors according to siteConfig // parse css files to replace colors according to siteConfig
if (file.match(/\.css$/)) { if (file.match(/\.css$/)) {
let cssContent = fs.readFileSync(file, 'utf8'); let cssContent = fs.readFileSync(file, 'utf8');
@ -210,7 +207,8 @@ async function execute() {
}); });
// Copy all static files from user. // Copy all static files from user.
files = glob.sync(join(CWD, 'static', '**'), {dot: true}); const userStaticDir = join(CWD, 'static');
files = glob.sync(join(userStaticDir, '**'), {dot: true});
files.forEach(file => { files.forEach(file => {
// Why normalize? In case we are on Windows. // Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows // Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
@ -265,8 +263,7 @@ async function execute() {
fs.copySync(normalizedFile, targetFile); fs.copySync(normalizedFile, targetFile);
}); });
} else if (!fs.lstatSync(normalizedFile).isDirectory()) { } else if (!fs.lstatSync(normalizedFile).isDirectory()) {
const parts = normalizedFile.split(`${sep}static${sep}`); const targetFile = normalizedFile.replace(userStaticDir, buildDir);
const targetFile = join(buildDir, parts[1]);
mkdirp.sync(path.dirname(targetFile)); mkdirp.sync(path.dirname(targetFile));
fs.copySync(normalizedFile, targetFile); fs.copySync(normalizedFile, targetFile);
} }
@ -284,18 +281,19 @@ async function execute() {
const enabledLanguages = env.translation const enabledLanguages = env.translation
.enabledLanguages() .enabledLanguages()
.map(lang => lang.tag); .map(lang => lang.tag);
files = glob.sync(join(CWD, 'pages', '**')); const userPagesDir = join(CWD, 'pages');
files = glob.sync(join(userPagesDir, '**'));
files.forEach(file => { files.forEach(file => {
// Why normalize? In case we are on Windows. // Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows // Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
const normalizedFile = path.normalize(file); const normalizedFile = path.normalize(file);
const relativeFile = normalizedFile.replace(userPagesDir, '');
// render .js files to strings // render .js files to strings
if (normalizedFile.match(/\.js$/)) { if (normalizedFile.match(/\.js$/)) {
const pageID = path.basename(normalizedFile, '.js'); const pageID = path.basename(normalizedFile, '.js');
// make temp file for sake of require paths // make temp file for sake of require paths
const parts = normalizedFile.split('pages'); let tempFile = join(__dirname, '..', 'pages', relativeFile);
let tempFile = join(__dirname, '..', 'pages', parts[1]);
tempFile = tempFile.replace( tempFile = tempFile.replace(
path.basename(normalizedFile), path.basename(normalizedFile),
`temp${path.basename(normalizedFile)}` `temp${path.basename(normalizedFile)}`
@ -305,11 +303,11 @@ async function execute() {
const ReactComp = require(tempFile); const ReactComp = require(tempFile);
let targetFile = join(buildDir, parts[1]); let targetFile = join(buildDir, relativeFile);
targetFile = targetFile.replace(/\.js$/, '.html'); targetFile = targetFile.replace(/\.js$/, '.html');
const regexLang = new RegExp( const regexLang = new RegExp(
`${escapeStringRegexp(`${sep}pages${sep}`)}(.*)${escapeStringRegexp( `${escapeStringRegexp(`${userPagesDir}${sep}`)}(.*)${escapeStringRegexp(
sep sep
)}` )}`
); );
@ -384,8 +382,7 @@ async function execute() {
fs.removeSync(tempFile); fs.removeSync(tempFile);
} else if (siteConfig.wrapPagesHTML && normalizedFile.match(/\.html$/)) { } else if (siteConfig.wrapPagesHTML && normalizedFile.match(/\.html$/)) {
const pageID = path.basename(normalizedFile, '.html'); const pageID = path.basename(normalizedFile, '.html');
const parts = normalizedFile.split('pages'); const targetFile = join(buildDir, relativeFile);
const targetFile = join(buildDir, parts[1]);
const str = renderToStaticMarkupWithDoctype( const str = renderToStaticMarkupWithDoctype(
<Site language="en" config={siteConfig} metadata={{id: pageID}}> <Site language="en" config={siteConfig} metadata={{id: pageID}}>
<div <div
@ -399,8 +396,7 @@ async function execute() {
writeFileAndCreateFolder(targetFile, str); writeFileAndCreateFolder(targetFile, str);
} else if (!fs.lstatSync(normalizedFile).isDirectory()) { } else if (!fs.lstatSync(normalizedFile).isDirectory()) {
// copy other non .js files // copy other non .js files
const parts = normalizedFile.split('pages'); const targetFile = join(buildDir, relativeFile);
const targetFile = join(buildDir, parts[1]);
mkdirp.sync(path.dirname(targetFile)); mkdirp.sync(path.dirname(targetFile));
fs.copySync(normalizedFile, targetFile); fs.copySync(normalizedFile, targetFile);
} }