-
Notifications
You must be signed in to change notification settings - Fork 184
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 file path issues #833
Merged
Merged
Fix file path issues #833
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2fece0a
Create tests for compileClient file writing.
drewpc f58dd8a
Apply fixes for various path issues in compileClient. Remove console…
drewpc 9070f45
Remove out of date comments regarding tests not being present.
drewpc c749a76
Restore callerDependency to it's original state. Create a mock calle…
drewpc da1a89d
Removed unnecessary getSpecGlob() function.
drewpc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/react-server-cli/src/__tests__/compileClient/compileClientSpec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import fs from "fs"; | ||
const MemoryStream = require('memory-stream'); | ||
import path from "path"; | ||
import mockery from "mockery"; | ||
|
||
describe("compileClient", () => { | ||
let mockFs, | ||
writeWebpackCompatibleRoutesFile; | ||
|
||
beforeAll(() => { | ||
mockery.registerSubstitute('./callerDependency', './callerDependency-Mock'); | ||
mockery.enable({ | ||
useCleanCache: true, | ||
warnOnUnregistered: false, | ||
}); | ||
|
||
writeWebpackCompatibleRoutesFile = require("../../compileClient").writeWebpackCompatibleRoutesFile; | ||
}); | ||
|
||
afterAll(() => { | ||
mockery.disable(); | ||
}); | ||
|
||
describe("writes client routes file for Webpack", () => { | ||
const pathStringTests = [ | ||
{ | ||
title: "apostrophes", | ||
path: "PathWith'InIt.js", | ||
}, | ||
{ | ||
title: "double quotes", | ||
path: 'PathWith"InIt.js', | ||
}, | ||
{ | ||
title: "windows style", | ||
path: 'c:\\Path\\With\\InIt.js', | ||
}, | ||
{ | ||
title: "spaces", | ||
path: 'Path With Spaces.js', | ||
}, | ||
]; | ||
|
||
beforeEach(() => { | ||
mockFs = new MemoryStream(); | ||
}); | ||
|
||
afterEach(() => { | ||
mockFs = null; | ||
}); | ||
|
||
pathStringTests.map((test) => { | ||
it("handles file paths with " + test.title, (finishTest) => { | ||
spyOn(fs, 'writeFileSync').and.callFake((path, data) => { | ||
mockFs.write(data); | ||
}); | ||
|
||
const filePath = test.path; | ||
const routes = { | ||
"routes": { | ||
"Homepage": { | ||
"path": "/", | ||
"page": filePath, | ||
}, | ||
}, | ||
}; | ||
|
||
writeWebpackCompatibleRoutesFile(routes, ".", path.normalize("."), null, true, null); | ||
|
||
const coreMiddlewareStringified = JSON.stringify(require.resolve("react-server-core-middleware")); | ||
const filePathStringified = JSON.stringify(filePath); | ||
|
||
// These four strings are important when using multiple platforms or strings with weird characters in them. | ||
// If we're going to output something to a file and then import that file later, we'd better be darn sure | ||
// it's all correctly formatted! Apostrophes, quotes, and windows-style file path characters \ vs / are the worst! | ||
const filePathRegexStrings = [ | ||
"var coreJsMiddleware = require(" + coreMiddlewareStringified + ").coreJsMiddleware;", | ||
"var coreCssMiddleware = require(" + coreMiddlewareStringified + ").coreCssMiddleware;", | ||
"require.ensure(" + filePathStringified + ", function() {", | ||
"cb(unwrapEs6Module(require(" + filePathStringified + ")));", | ||
]; | ||
|
||
const fileData = mockFs.toString(); | ||
filePathRegexStrings.map((regex) => { | ||
expect(fileData).toMatch(regex.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")); | ||
}); | ||
|
||
finishTest(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import lookup from "look-up"; | ||
import path from "path"; | ||
|
||
export default function callerDependency(dep) { | ||
// TODO: We should grab stuff based on what the routes file would get out | ||
// of `require.resolve(dep)`. Using `process.cwd()` instead for now. | ||
return lookup("packages/" + dep, {cwd: path.resolve(process.cwd() + '/..')}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -268,18 +268,18 @@ function packageCodeForBrowser(entrypoints, outputDir, outputUrl, hot, minify, l | |
} | ||
|
||
// writes out a routes file that can be used at runtime. | ||
function writeWebpackCompatibleRoutesFile(routes, routesDir, workingDirAbsolute, staticUrl, isClient, manifest) { | ||
export function writeWebpackCompatibleRoutesFile(routes, routesDir, workingDirAbsolute, staticUrl, isClient, manifest) { | ||
let routesOutput = []; | ||
|
||
const coreMiddleware = require.resolve("react-server-core-middleware"); | ||
const coreMiddleware = JSON.stringify(require.resolve("react-server-core-middleware")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, yeah, good call! |
||
const existingMiddleware = routes.middleware ? routes.middleware.map((middlewareRelativePath) => { | ||
return `unwrapEs6Module(require("${path.relative(workingDirAbsolute, path.resolve(routesDir, middlewareRelativePath))}"))` | ||
return `unwrapEs6Module(require(${JSON.stringify(path.relative(workingDirAbsolute, path.resolve(routesDir, middlewareRelativePath)))}))` | ||
}) : []; | ||
routesOutput.push(` | ||
var manifest = ${manifest ? JSON.stringify(manifest) : "undefined"}; | ||
function unwrapEs6Module(module) { return module.__esModule ? module.default : module } | ||
var coreJsMiddleware = require('${coreMiddleware}').coreJsMiddleware; | ||
var coreCssMiddleware = require('${coreMiddleware}').coreCssMiddleware; | ||
var coreJsMiddleware = require(${coreMiddleware}).coreJsMiddleware; | ||
var coreCssMiddleware = require(${coreMiddleware}).coreCssMiddleware; | ||
module.exports = { | ||
middleware:[ | ||
coreJsMiddleware(${JSON.stringify(staticUrl)}, manifest), | ||
|
@@ -311,22 +311,22 @@ module.exports = { | |
page: {`); | ||
for (let format of Object.keys(formats)) { | ||
const formatModule = formats[format]; | ||
var relativePathToPage = path.relative(workingDirAbsolute, path.resolve(routesDir, formatModule)); | ||
var relativePathToPage = JSON.stringify(path.relative(workingDirAbsolute, path.resolve(routesDir, formatModule))); | ||
routesOutput.push(` | ||
${format}: function() { | ||
return { | ||
done: function(cb) {`); | ||
if (isClient) { | ||
routesOutput.push(` | ||
require.ensure("${relativePathToPage}", function() { | ||
cb(unwrapEs6Module(require("${relativePathToPage}"))); | ||
require.ensure(${relativePathToPage}, function() { | ||
cb(unwrapEs6Module(require(${relativePathToPage}))); | ||
});`); | ||
} else { | ||
routesOutput.push(` | ||
try { | ||
cb(unwrapEs6Module(require("${relativePathToPage}"))); | ||
cb(unwrapEs6Module(require(${relativePathToPage}))); | ||
} catch (e) { | ||
console.error('Failed to load page at "${relativePathToPage}"', e.stack); | ||
console.error('Failed to load page at ${relativePathToPage}', e.stack); | ||
}`); | ||
} | ||
routesOutput.push(` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's... quite a regex! 😳