Skip to content

Commit

Permalink
fix: nested static in static breaks build (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey authored and yangshun committed Sep 11, 2018
1 parent 11faa43 commit 4a2b7cf
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,12 @@ async function execute() {
}

// 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 => {
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
let targetFile = path.normalize(file);
targetFile = join(
buildDir,
targetFile.split(`${sep}static${sep}`)[1] || ''
);
const targetFile = path.normalize(file).replace(libStaticDir, buildDir);
// parse css files to replace colors according to siteConfig
if (file.match(/\.css$/)) {
let cssContent = fs.readFileSync(file, 'utf8');
Expand Down Expand Up @@ -210,7 +207,8 @@ async function execute() {
});

// 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 => {
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
Expand Down Expand Up @@ -265,8 +263,7 @@ async function execute() {
fs.copySync(normalizedFile, targetFile);
});
} else if (!fs.lstatSync(normalizedFile).isDirectory()) {
const parts = normalizedFile.split(`${sep}static${sep}`);
const targetFile = join(buildDir, parts[1]);
const targetFile = normalizedFile.replace(userStaticDir, buildDir);
mkdirp.sync(path.dirname(targetFile));
fs.copySync(normalizedFile, targetFile);
}
Expand All @@ -284,18 +281,19 @@ async function execute() {
const enabledLanguages = env.translation
.enabledLanguages()
.map(lang => lang.tag);
files = glob.sync(join(CWD, 'pages', '**'));
const userPagesDir = join(CWD, 'pages');
files = glob.sync(join(userPagesDir, '**'));
files.forEach(file => {
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
const normalizedFile = path.normalize(file);
const relativeFile = normalizedFile.replace(userPagesDir, '');
// render .js files to strings
if (normalizedFile.match(/\.js$/)) {
const pageID = path.basename(normalizedFile, '.js');

// make temp file for sake of require paths
const parts = normalizedFile.split('pages');
let tempFile = join(__dirname, '..', 'pages', parts[1]);
let tempFile = join(__dirname, '..', 'pages', relativeFile);
tempFile = tempFile.replace(
path.basename(normalizedFile),
`temp${path.basename(normalizedFile)}`
Expand All @@ -305,11 +303,11 @@ async function execute() {

const ReactComp = require(tempFile);

let targetFile = join(buildDir, parts[1]);
let targetFile = join(buildDir, relativeFile);
targetFile = targetFile.replace(/\.js$/, '.html');

const regexLang = new RegExp(
`${escapeStringRegexp(`${sep}pages${sep}`)}(.*)${escapeStringRegexp(
`${escapeStringRegexp(`${userPagesDir}${sep}`)}(.*)${escapeStringRegexp(
sep
)}`
);
Expand Down Expand Up @@ -384,8 +382,7 @@ async function execute() {
fs.removeSync(tempFile);
} else if (siteConfig.wrapPagesHTML && normalizedFile.match(/\.html$/)) {
const pageID = path.basename(normalizedFile, '.html');
const parts = normalizedFile.split('pages');
const targetFile = join(buildDir, parts[1]);
const targetFile = join(buildDir, relativeFile);
const str = renderToStaticMarkupWithDoctype(
<Site language="en" config={siteConfig} metadata={{id: pageID}}>
<div
Expand All @@ -399,8 +396,7 @@ async function execute() {
writeFileAndCreateFolder(targetFile, str);
} else if (!fs.lstatSync(normalizedFile).isDirectory()) {
// copy other non .js files
const parts = normalizedFile.split('pages');
const targetFile = join(buildDir, parts[1]);
const targetFile = join(buildDir, relativeFile);
mkdirp.sync(path.dirname(targetFile));
fs.copySync(normalizedFile, targetFile);
}
Expand Down

0 comments on commit 4a2b7cf

Please sign in to comment.