Skip to content
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

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -4,6 +4,7 @@ module.exports = {
jest: true,
},
rules: {
'linebreak-style': 0,
'import/no-unresolved': 0,
'import/extensions': 0,
},
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-css-modules/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-emotion/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-jss/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-less/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-mdx/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-preact/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-reach-router/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-react-location/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-react-router/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-sass/package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-sitemap/package.json
Expand Up @@ -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": {
Expand Down
Expand Up @@ -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": {
Expand Down
29 changes: 23 additions & 6 deletions packages/react-static-plugin-source-filesystem/src/node.api.js
Expand Up @@ -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) {
Expand All @@ -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({
Expand Down
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-static-plugin-typescript/package.json
Expand Up @@ -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": {
Expand Down
9 changes: 5 additions & 4 deletions packages/react-static/package.json
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions packages/react-static/src/static/__tests__/getRoutes.test.js
Expand Up @@ -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)
})
})
})
})
23 changes: 14 additions & 9 deletions packages/react-static/src/static/exportRoute.js
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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.`
)
}

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/react-static/src/static/exportRoutes.js
Expand Up @@ -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()
Expand Down
16 changes: 14 additions & 2 deletions packages/react-static/src/static/getRoutes.js
Expand Up @@ -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.'
)
}

Expand All @@ -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')
)
),
})
}
Expand Down Expand Up @@ -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))
Expand Down
Expand Up @@ -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}`)
Expand Down