Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e5a4d74
Dev: Add "framework" option
RaeesBhatti Apr 13, 2020
0d5075e
settings.type rename to settings.framework
RaeesBhatti Apr 13, 2020
f860bf5
Dev: Remove flag "framework"
RaeesBhatti Apr 14, 2020
2e752f7
Detect Server: Provide process env as default
RaeesBhatti Apr 14, 2020
feef41b
Merge branch 'master' into raees/framework-option
RaeesBhatti Apr 14, 2020
f78e94d
Dev: Rename a prop
RaeesBhatti Apr 14, 2020
8908318
Merge branch 'master' into raees/framework-option
RaeesBhatti Apr 14, 2020
ec0d518
Rename cra detector to create-react-app
RaeesBhatti Apr 15, 2020
74081cd
Return settings.framework when configured
RaeesBhatti Apr 15, 2020
c721446
Detect Server: Change chooseDefaultSettings to chooseDefaultArgs
RaeesBhatti Apr 15, 2020
b38dbc6
Detect Server: Add some tests
RaeesBhatti Apr 15, 2020
9e650c7
Add docs for "framework" option
RaeesBhatti Apr 15, 2020
64dcc42
grammer
RaeesBhatti Apr 15, 2020
c8fa056
Detect Server test: Update valid detector
RaeesBhatti Apr 15, 2020
2557912
Detect Server: Test command override
RaeesBhatti Apr 15, 2020
8752f99
Dev Docs: Remove "framework" from default config
RaeesBhatti Apr 15, 2020
9782105
Update an instruction log
RaeesBhatti Apr 15, 2020
8492f05
Detect Server: Fix settings when single detector passed
RaeesBhatti Apr 15, 2020
af4e01f
Detect Server: Dont slice out run from npm command
RaeesBhatti Apr 15, 2020
b6e0d7b
Docs: Add docs for '#auto' and `#static` "framework" options
RaeesBhatti Apr 15, 2020
fdc281f
Dev: Fix a condition check
RaeesBhatti Apr 17, 2020
3c592eb
Update a URL in log
RaeesBhatti Apr 17, 2020
923b372
Improve a log
RaeesBhatti Apr 17, 2020
4488512
Dev: Use dist from settings
RaeesBhatti Apr 17, 2020
446a199
Dev: Don't use static if settings.proxyPort is present
RaeesBhatti Apr 17, 2020
481a2bb
Dev: Improve a log
RaeesBhatti Apr 17, 2020
e3b37fe
Move all server settings logic to serverSettings function
RaeesBhatti Apr 17, 2020
9cc4f6e
DetectServer: Add new tests and update previous
RaeesBhatti Apr 17, 2020
92ec534
Merge branch 'master' into raees/framework-option
RaeesBhatti Apr 17, 2020
14f7bd9
Detect Server test: use t.is instead
RaeesBhatti Apr 17, 2020
7d3891f
Detect Server test: Change port to reduce conflict
RaeesBhatti Apr 17, 2020
0569d0e
Remove unused require
RaeesBhatti Apr 17, 2020
aeeb0ec
Dev: Revamp functionDir config
RaeesBhatti Apr 17, 2020
a4f7464
Add test for "functions" config
RaeesBhatti Apr 17, 2020
54bb9ce
Merge branch 'master' into raees/framework-option
RaeesBhatti Apr 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/netlify-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Netlify Dev is meant to work with zero config for the majority of users, by usin

## Project detection

Netlify Dev will attempt to detect the SSG or build command that you are using, and run these on your behalf, while adding other development utilities. If you have a JavaScript project, it looks for the best `package.json` script to run for you, using simple heuristics, so you can use the full flexibility of npm scripts. We may add more intelligence to this in future.
Netlify Dev will attempt to detect the SSG or build command that you are using, and run these on your behalf, while adding other development utilities. If you have a JavaScript project, it looks for the best `package.json` script to run for you, using simple heuristics, so you can use the full flexibility of npm scripts. We may add more intelligence to this in the future.

**Overriding the detectors**: The number of [project types which Netlify Dev can detect](https://github.com/netlify/cli/tree/master/src/detectors) is growing, but if yours is not yet supported (contributions welcome!), you can instruct Netlify Dev to run the project on your behalf by declaring it in a `[dev]` block of your `netlify.toml` file.

Expand All @@ -134,6 +134,19 @@ Netlify Dev will attempt to detect the SSG or build command that you are using,
publish = "dist" # If you use a _redirect file, provide the path to your static content folder
```


Or you if your project is being detected incorrectly or positive by multiple
detectors you can specify `framework` option to test only one detector
against your project.
```toml
[dev]
framework = "create-react-app" # or "#static" to force a static server
```
The `framework` option should be one of the available
[project types which Netlify Dev can detect](https://github.com/netlify/cli/tree/master/src/detectors)
or `#auto` (default) to test all available detectors or `#staic` for a static
file server.

## Explanation of ports in Netlify Dev

There will be a number of ports that you will encounter when using Netlify Dev, especially when running a static site generator like Gatsby which has its own dev server. All the port numbers can be a bit confusing, so here is a brief explainer.
Expand Down
77 changes: 14 additions & 63 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const child_process = require('child_process')
const http = require('http')
const httpProxy = require('http-proxy')
const waitPort = require('wait-port')
const getPort = require('get-port')
const stripAnsiCc = require('strip-ansi-control-characters')
const which = require('which')
const chokidar = require('chokidar')
Expand Down Expand Up @@ -202,7 +201,7 @@ async function startProxy(settings, addonUrls, configPath, projectDir, functions
functionsServer,
functionsPort: settings.functionsPort,
jwtRolePath: settings.jwtRolePath,
serverType: settings.type,
framework: settings.framework,
}

if (match) return serveRedirect(req, res, proxy, match, options)
Expand Down Expand Up @@ -288,7 +287,7 @@ async function serveRedirect(req, res, proxy, match, options) {
return render404(options.publicFolder)
}

if (match.force || ((!(await isStatic(reqUrl.pathname, options.publicFolder)) || options.serverType) && match.status !== 404)) {
if (match.force || ((!(await isStatic(reqUrl.pathname, options.publicFolder)) || options.framework) && match.status !== 404)) {
const dest = new url.URL(match.to, `${reqUrl.protocol}//${reqUrl.host}`)
if (isRedirect(match)) {
res.writeHead(match.status, {
Expand All @@ -314,7 +313,7 @@ async function serveRedirect(req, res, proxy, match, options) {
const destURL = dest.pathname + (urlParams.toString() && '?' + urlParams.toString())

let status
if (isInternal(destURL) || !options.serverType) {
if (isInternal(destURL) || !options.framework) {
req.url = destURL
status = match.status
console.log(`${NETLIFYDEVLOG} Rewrote URL to `, req.url)
Expand Down Expand Up @@ -354,15 +353,14 @@ async function startDevServer(settings, log) {
return
}

log(`${NETLIFYDEVLOG} Starting Netlify Dev with ${settings.type}`)
const args = settings.command === 'npm' ? ['run', ...settings.args] : settings.args
log(`${NETLIFYDEVLOG} Starting Netlify Dev with ${settings.framework || 'custom config'}`)
const commandBin = await which(settings.command).catch(err => {
if (err.code === 'ENOENT') {
throw new Error(`"${settings.command}" could not be found in your PATH. Please make sure that "${settings.command}" is installed and available in your PATH`)
}
throw err
})
const ps = child_process.spawn(commandBin, args, {
const ps = child_process.spawn(commandBin, settings.args, {
env: { ...settings.env, FORCE_COLOR: 'true' },
stdio: 'pipe',
})
Expand Down Expand Up @@ -392,9 +390,9 @@ class DevCommand extends Command {
this.log(`${NETLIFYDEV}`)
let { flags } = this.parse(DevCommand)
const { api, site, config } = this.netlify
config.dev = config.dev || {}
const devConfig = { ...config.dev, ...flags }
const functionsDir = devConfig.functions || (config.build && config.build.functions)
config.dev = { ...config.dev }
config.build = { ...config.build }
const devConfig = { framework: '#auto', ...(config.build.functions && { functions: config.build.functions }), ...config.dev, ...flags }
let addonUrls = {}

let accessToken = api.accessToken
Expand All @@ -412,57 +410,13 @@ class DevCommand extends Command {
Object.entries(vars).forEach(([key, val]) => process.env[key] = val)
}

let settings = await serverSettings(devConfig)

if (flags.dir || !(settings && settings.command)) {
let dist
if (flags.dir) {
this.log(`${NETLIFYDEVWARN} Using simple static server because --dir flag was specified`)
dist = flags.dir
} else {
this.log(`${NETLIFYDEVWARN} No dev server detected, using simple static server`)
}
if (!dist) {
this.log(`${NETLIFYDEVLOG} Using current working directory`)
this.log(`${NETLIFYDEVWARN} Unable to determine public folder to serve files from.`)
this.log(
`${NETLIFYDEVWARN} Setup a netlify.toml file with a [dev] section to specify your dev server settings.`
)
this.log(
`${NETLIFYDEVWARN} See docs at: https://github.com/netlify/cli/blob/master/docs/netlify-dev.md#project-detection`
)
this.log(`${NETLIFYDEVWARN} Using current working directory for now...`)
dist = process.cwd()
}
settings = {
env: { ...process.env },
noCmd: true,
port: 8888,
proxyPort: await getPort({ port: 3999 }),
dist,
}
}

// Flags have highest priority, then configuration file (netlify.toml etc.) then detectors
settings = {
...settings,
jwtRolePath: devConfig.jwtRolePath || settings.jwtRolePath || 'app_metadata.authorization.roles',
port: devConfig.port || settings.port,
proxyPort: devConfig.targetPort || settings.proxyPort,
functionsPort: await getPort({ port: settings.functionsPort || 34567 }),
}

const port = await getPort({ port: settings.port })
if (port !== settings.port && devConfig.port) {
throw new Error(`Could not acquire required "port": ${settings.port}`)
}
settings.port = port
let settings = await serverSettings(devConfig, flags, this.log)

await startDevServer(settings, this.log)

// serve functions from zip-it-and-ship-it
// env variables relies on `url`, careful moving this code
if (functionsDir) {
if (settings.functions) {
const functionBuilder = await detectFunctionsBuilder(settings)
if (functionBuilder) {
this.log(
Expand All @@ -480,10 +434,7 @@ class DevCommand extends Command {
functionWatcher.on('unlink', functionBuilder.build)
}

const functionsServer = await serveFunctions({
...settings,
functionsDir
})
const functionsServer = await serveFunctions(settings.functions)
functionsServer.listen(settings.functionsPort, function(err) {
if (err) {
console.error(`${NETLIFYDEVERR} Unable to start lambda server: `, err) // eslint-disable-line no-console
Expand All @@ -495,7 +446,7 @@ class DevCommand extends Command {
})
}

let { url } = await startProxy(settings, addonUrls, site.configPath, site.root, functionsDir)
let { url } = await startProxy(settings, addonUrls, site.configPath, site.root, settings.functions)
if (!url) {
throw new Error('Unable to start proxy server')
}
Expand All @@ -513,7 +464,7 @@ class DevCommand extends Command {
eventName: 'command',
payload: {
command: 'dev',
projectType: settings.type || 'custom',
projectType: settings.framework || 'custom',
live: flags.live || false
}
})
Expand Down Expand Up @@ -577,7 +528,7 @@ DevCommand.flags = {
live: flags.boolean({
char: 'l',
description: 'Start a public live session'
})
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't Prettier pick up those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have prettier setup for oncommit hook, it messes up the staged hunks

}

module.exports = DevCommand
4 changes: 2 additions & 2 deletions src/detectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

```ts
{
type: String, // e.g. gatsby, vue-cli
framework: String, // e.g. gatsby, vue-cli
command: String, // e.g. yarn, npm
port: Number, // e.g. 8888
proxyPort: Number, // e.g. 3000
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = function() {
// });

return {
type: 'gitbook',
framework: 'gitbook',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 4000,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {
}

return {
type: '@angular/cli',
framework: '@angular/cli',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 4200,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/brunch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function() {
})

return {
type: 'brunch',
framework: 'brunch',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 3333,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/cra.js → src/detectors/create-react-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function() {
}

return {
type: 'create-react-app',
framework: 'create-react-app',
command: getYarnOrNPMCommand(),
port: 8888, // the port that the Netlify Dev User will use
proxyPort: 3000, // the port that create-react-app normally outputs
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function() {
})

return {
type: 'docusaurus',
framework: 'docusaurus',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 3000,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function() {
) return false

return {
type: 'eleventy',
framework: 'eleventy',
port: 8888,
proxyPort: 8080,
env: { ...process.env },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {
}

return {
type: 'ember-cli',
framework: 'ember-cli',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 4200,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/expo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function() {
possibleArgsArrs.push(['expo', 'start', '--web'])
}
return {
type: 'expo',
framework: 'expo',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 19006,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/gatsby.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function() {
possibleArgsArrs.push(['gatsby', 'develop'])
}
return {
type: 'gatsby',
framework: 'gatsby',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 8000,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/gridsome.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function() {
})

return {
type: 'gridsome',
framework: 'gridsome',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 8080,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function() {
possibleArgsArrs.push(['hexo', 'server'])
}
return {
type: 'hexo',
framework: 'hexo',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 4000,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function() {
}

return {
type: 'hugo',
framework: 'hugo',
port: 8888,
proxyPort: 1313,
env: { ...process.env },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function() {
}

return {
type: 'jekyll',
framework: 'jekyll',
port: 8888,
proxyPort: 4000,
env: { ...process.env },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/middleman.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function() {
}

return {
type: 'middleman',
framework: 'middleman',
port: 8888,
proxyPort: 4567,
env: { ...process.env },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function() {
possibleArgsArrs.push(['next'])
}
return {
type: 'next.js',
framework: 'next.js',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 3000,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/nuxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function() {
}

return {
type: 'nuxt',
framework: 'nuxt',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 3000,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/parcel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {
}

return {
type: 'parcel',
framework: 'parcel',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 1234,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/phenomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function() {
})

return {
type: 'phenomic',
framework: 'phenomic',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 3333,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/quasar-v0.17.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {
}

return {
type: 'quasar-cli-v0.17',
framework: 'quasar-cli-v0.17',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 8080,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/quasar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {
}

return {
type: 'quasar-cli',
framework: 'quasar-cli',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 8081,
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/react-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function() {
possibleArgsArrs.push(['react-static', 'start'])
}
return {
type: 'react-static',
framework: 'react-static',
command: getYarnOrNPMCommand(),
port: 8888,
proxyPort: 3000,
Expand Down
Loading