Skip to content
Permalink
Browse files

Make resolution cross-env in filesystem plugin and base package (#1128)

* Make resolution cross-env

* Stop using hardcoded path separators. These are not the same across environments.
* Use path.relative for relative resolution instead of replace. This normalizes paths first
* Cutoff the extension using path.basename and path.extname, which works across environments
* Prefix with a slash if necessary

* Remove linebreak-style rule

This is covered by git and other svn; it will automatically correct the linebreak style.

https://eslint.org/docs/rules/linebreak-style#using-this-rule-with-version-control-systems

> For example, the default behavior of git on Windows systems is to convert LF linebreaks to CRLF when checking out files, but to store the linebreaks as LF when committing a change

This rule is therefore redundant.

* Add cross-env

This correctly passes environment variables to the process across environments.

* Fix non-bourne shell quotes

Single quotes won't work across shells. In this case the argument never needs quotes.

* Make chunkbuilder work cross-environments

* Format πŸ’…

* Always use path.join

* Test for throwing behaviour

* Format test

* Add changes to CHANGELOG.md

* Ensure stable path.join

* Fix config path with resolve on windows

* Format chunkBuilder.js

* Use cross-env path resolution

* Format 🐟

* πŸ’… format edited files

* Add production build fix to CHANGELOG.md
  • Loading branch information...
SleeplessByte authored and tannerlinsley committed Apr 16, 2019
1 parent 932796b commit cb090813ad62b449f3c26e772e5c6738e4f06938
Showing with 143 additions and 69 deletions.
  1. +1 βˆ’0 .eslintrc.js
  2. +3 βˆ’0 CHANGELOG.md
  3. +1 βˆ’1 packages/react-static-plugin-css-modules/package.json
  4. +1 βˆ’1 packages/react-static-plugin-emotion/package.json
  5. +1 βˆ’1 packages/react-static-plugin-jss/package.json
  6. +1 βˆ’1 packages/react-static-plugin-less/package.json
  7. +1 βˆ’1 packages/react-static-plugin-mdx/package.json
  8. +1 βˆ’1 packages/react-static-plugin-preact/package.json
  9. +1 βˆ’1 packages/react-static-plugin-reach-router/package.json
  10. +1 βˆ’1 packages/react-static-plugin-react-location/package.json
  11. +1 βˆ’1 packages/react-static-plugin-react-router/package.json
  12. +1 βˆ’1 packages/react-static-plugin-sass/package.json
  13. +1 βˆ’1 packages/react-static-plugin-sitemap/package.json
  14. +1 βˆ’1 packages/react-static-plugin-source-filesystem/package.json
  15. +23 βˆ’6 packages/react-static-plugin-source-filesystem/src/node.api.js
  16. +1 βˆ’1 packages/react-static-plugin-styled-components/package.json
  17. +1 βˆ’1 packages/react-static-plugin-typescript/package.json
  18. +5 βˆ’4 packages/react-static/package.json
  19. +8 βˆ’0 packages/react-static/src/static/__tests__/getRoutes.test.js
  20. +14 βˆ’9 packages/react-static/src/static/exportRoute.js
  21. +2 βˆ’1 packages/react-static/src/static/exportRoutes.js
  22. +14 βˆ’2 packages/react-static/src/static/getRoutes.js
  23. +2 βˆ’2 packages/react-static/src/static/webpack/webpack.config.prod.js
  24. +25 βˆ’0 packages/react-static/src/utils/__tests__/chunkBuilder.test.js
  25. +23 βˆ’30 packages/react-static/src/utils/chunkBuilder.js
  26. +9 βˆ’1 yarn.lock
@@ -4,6 +4,7 @@ module.exports = {
jest: true,
},
rules: {
'linebreak-style': 0,
'import/no-unresolved': 0,
'import/extensions': 0,
},
@@ -3,6 +3,9 @@
### Improved

- Fixed issues with custom config specified using `-c` or `-config` is not loaded
- `react-static-plugin-source-filesystem`: Fixed resolution on Windows
- `react-static`: Fixed building chunks on Windows
- `react-static`: Fixed production build on Windows

# 7.0.7

@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"peerDependencies": {
@@ -7,7 +7,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"peerDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"peerDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"peerDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -12,7 +12,7 @@
"watch": "yarn build --watch",
"start": "yarn watch",
"test": "yarn format && jest",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"devDependencies": {
@@ -14,14 +14,23 @@ export default ({
const { config, stage, debug } = state
if (!location) {
throw new Error(
'react-static-plugin-source-filesystem: A valid `location` directory is required to use this plugin'
'react-static-plugin-source-filesystem: A valid `location` directory ' +
'is required to use this plugin'
)
}
// Make a glob extension to get all pages with the set extensions from the pages directory
// It should be a directory, not index.js inside that directory. This will
// happen when using .resolve in some instances
if (/index\.js$/.test(location)) {
location = nodePath.dirname(location)
}

// Make a glob extension to get all pages with the set extensions from the
// pages directory
const globExtensions = [...config.extensions, ...extensions]
.map(ext => `${ext.slice(1)}`) // cut off the period of the extension
.join(',') // join them for the glob string
const pagesGlob = `${location}/**/*.{${globExtensions}}`
const pagesGlob = nodePath.join(location, '**', `*.{${globExtensions}}`)
// Get the pages

if (debug) {
@@ -37,12 +46,20 @@ export default ({
const originalPath = page
// Glob path will always have unix style path, convert to windows if necessary
const template = nodePath.resolve(page)
// Make sure the path is relative to the root of the site
let path = page.replace(`${location}`, '').replace(/\..*/, '')
// turn windows paths back to unix
path = path.split('\\').join('/')
// Make sure the path is relative to the location root
let path = nodePath.relative(location, template)
// Cutoff the extension
path = nodePath.join(
nodePath.dirname(path),
nodePath.basename(path, nodePath.extname(path))
)
// Ensure paths are unix
path = path.split(nodePath.sep).join(nodePath.posix.sep)
// Make sure it starts with a slash
path = path[0] === '/' ? path : `/${path}`
// Turn `/index` paths into roots`
path = path.replace(/\/index$/, '/')
// Add the path prefix
path = pathPrefix ? pathJoin(pathPrefix, path) : path
// Return the route
return createRoute({
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"peerDependencies": {
@@ -11,7 +11,7 @@
"build": "babel src --out-dir .",
"watch": "yarn build --watch",
"test": "yarn format",
"format": "prettier 'src/**/*.js' --write",
"format": "prettier src/**/*.js --write",
"preversion": "yarn build"
},
"dependencies": {
@@ -14,15 +14,15 @@
},
"scripts": {
"prebuild": "rimraf lib",
"build": "REACT_STATIC_INTERNAL=true babel src --out-dir lib --ignore '**/__tests__/*' --source-maps inline",
"build": "cross-env REACT_STATIC_INTERNAL=true babel src --out-dir lib --ignore '**/__tests__/*' --source-maps inline",
"watch": "yarn build --watch",
"watch:link": "onchange './lib/**/*' -- yalc push --changed",
"start": "yarn watch",
"test": "yarn format && yarn lint && yarn unitTest",
"lint": "eslint src",
"format": "prettier 'README.md' '{src,templates/*/src}/**/*.js' --write",
"unitTest": "NODE_ENV=test yarn jest ./src",
"unitTestWatch": "NODE_ENV=test yarn jest src --watch",
"format": "prettier README.md {src,templates/*/src}/**/*.js --write",
"unitTest": "cross-env NODE_ENV=test yarn jest ./src",
"unitTestWatch": "cross-env NODE_ENV=test yarn jest src --watch",
"preversion": "yarn build && yarn test",
"publishLink": "echo '{{event}} to {{changed}}' && yalc publish"
},
@@ -107,6 +107,7 @@
"@types/react-helmet": "^5.0.7",
"babel-jest": "^24.5.0",
"cors": "^2.8.5",
"cross-env": "^5.2.0",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.11.2",
"enzyme-to-json": "^3.3.4",
@@ -92,5 +92,13 @@ describe('normalizeRoute', () => {
})
})
})

describe('when path is not normalised', () => {
it('should throw an error', () => {
const route = { template: 'windows\\path' }

expect(() => normalizeRoute(route, undefined, d => d)).toThrow(Error)
})
})
})
})
@@ -103,11 +103,9 @@ export default (async function exportRoute(state) {

let FinalComp

// Get the react component from the Comp and
// pass it the export context. This uses
// reactContext under the hood to pass down
// the exportContext, since react's new context
// api doesn't survive across bundling.
// Get the react component from the Comp and pass it the export context. This
// uses reactContext under the hood to pass down the exportContext, since
// react's new context api doesn't survive across bundling.
Comp = config.disableRuntime ? Comp : Comp(embeddedRouteInfo)

if (route.redirect) {
@@ -116,7 +114,9 @@ export default (async function exportRoute(state) {
FinalComp = props => (
<ReportChunks
report={chunkName => {
// if we are building to a absolute path we must make the detected chunkName relative and matching to the one we set in generateTemplates
// if we are building to a absolute path we must make the detected
// chunkName relative and matching to the one we set in
// generateTemplates
if (!config.paths.DIST.startsWith(config.paths.ROOT)) {
chunkName = absoluteToRelativeChunkName(
config.paths.ROOT,
@@ -173,7 +173,8 @@ export default (async function exportRoute(state) {

if (config.renderToElement) {
throw new Error(
`config.renderToElement has been deprecated in favor of the 'beforeRenderToElement' or 'beforeRenderToHtml' hooks instead.`
`config.renderToElement has been deprecated in favor of the ` +
`'beforeRenderToElement' or 'beforeRenderToHtml' hooks instead.`
)
}

@@ -185,17 +186,21 @@ export default (async function exportRoute(state) {

if (config.renderToHtml) {
throw new Error(
`config.renderToHtml has been deprecated in favor of the 'beforeRenderToHtml' or 'beforeHtmlToDocument' hooks instead.`
`config.renderToHtml has been deprecated in favor of the ` +
`'beforeRenderToHtml' or 'beforeHtmlToDocument' hooks instead.`
)
}

console.log('Time to render to string and extract', RenderedComp)

appHtml = renderToStringAndExtract(RenderedComp)

appHtml = await plugins.beforeHtmlToDocument(appHtml, state)
} catch (error) {
if (error.then) {
error.message =
'Components are not allowed to suspend during static export. Please make its data available synchronously and try again!'
'Components are not allowed to suspend during static export. Please ' +
'make its data available synchronously and try again!'
}
error.message = `Failed exporting HTML for URL ${route.path} (${
route.template
@@ -25,7 +25,8 @@ async function buildHTML(state) {

time(chalk.green('[\u2713] HTML Exported'))

// in case of an absolute path for DIST we must tell node to load the modules from our project root
// in case of an absolute path for DIST we must tell node to load the modules
// from our project root
if (!paths.DIST.startsWith(paths.ROOT)) {
process.env.NODE_PATH = paths.NODE_MODULES
require('module').Module._initPaths()
@@ -36,7 +36,9 @@ export default async function getRoutes(state, callback = d => d) {
// If no Index page was found, throw an error. This is required
if (!hasIndex && !incremental) {
throw new Error(
'Could not find a route for the "index" page of your site! This is required. Please create a page or specify a route and template for this page.'
'Could not find a route for the "index" page of your site! This is ' +
'required. Please create a page or specify a route and template ' +
'for this page.'
)
}

@@ -46,7 +48,10 @@ export default async function getRoutes(state, callback = d => d) {
path: '404',
template: path.relative(
state.config.paths.ROOT,
path.resolve(__dirname, '../browser/components/Default404')
path.resolve(
__dirname,
path.join('..', 'browser', 'components', 'Default404')
)
),
})
}
@@ -118,6 +123,13 @@ export function normalizeAllRoutes(routes, state) {
if (normalizedRoute.path === '404') {
has404 = true
}

if (normalizedRoute.path.indexOf('\\') !== -1) {
throw new Error(
'Plugins must return a normalized path for the `path` key of a route,' +
' which is a path with / and not \\.'
)
}
}

routes.forEach(route => recurseRoute(route))
@@ -167,8 +167,8 @@ export default function(state) {
(context, request, callback) => {
const resolved = path.resolve(context, request)
if (
[/react-static\/lib\/browser/, /webpack-flush-chunks/].some(d =>
d.test(resolved)
[/react-static(\\|\/)lib(\\|\/)browser/, /webpack-flush-chunks/].some(
d => d.test(resolved)
)
) {
return callback(null, `commonjs ${resolved}`)

0 comments on commit cb09081

Please sign in to comment.
You can’t perform that action at this time.