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

Exploration of different config + expose webpack config #222

Merged
merged 16 commits into from
Dec 17, 2016

Conversation

CompuIves
Copy link
Contributor

@CompuIves CompuIves commented Nov 7, 2016

We currently use package.json for our configuration. I think we should move away from using package.json and use something like next.config.js since it gives us more power than JSON.

An example is the issue for custom Webpack support: #40.
With a config like this you could do this for example:

// next.config.js

export default {
  webpack: (webpackConfig) => {
    const newConfig = { ...webpackConfig };
    newConfig.module.preloaders.push({ test: /\.js$/, loader: 'eslint-loader' });
    return newConfig;
  },
  cdn: false
}

Which is in my opinion better than creating a new file for webpack since this is more centralised. This does however still give users the option to use different files, for the webpack example:

// next.config.js

export default {
  webpack: require('./webpack').default,
  cdn: false
}

These are just my thoughts, I found out that I needed a sort of config file in .js when starting a PR for this project. Very curious what others think of this 😄.

@CompuIves CompuIves mentioned this pull request Nov 7, 2016
@frol
Copy link
Contributor

frol commented Nov 7, 2016

Disclaimer: I am not a JS developer, so I am not familiar with the traditions in JS world.

Config files in JS enable great flexibility, but at a cost of greater complexity and potential misuse and errors. In Python (which is my preferred language), it is common to use config files written in Python.

Overall, I like this idea.

} else {
throw err
}
}

// no try-cache, it must be a valid json
const config = JSON.parse(data).next || {}
const config = module.default || module || {}
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, the above comment is irrelevant for the updated implementation.

@rauchg
Copy link
Member

rauchg commented Nov 7, 2016

This is a pretty great idea I think

@Florian-R
Copy link

If this one get merged (and I hope it will), should this fall back to the old behaviour, so this is not a breaking change?

@CompuIves
Copy link
Contributor Author

@Florian-R You mean also read package.json for CND settings? We could do this, although it does add an extra bit of complexity since then we're reading two files for configuration.

@frol
Copy link
Contributor

frol commented Nov 8, 2016

@Florian-R There existed only one config option: cdn=true. I don't think it is so much of a trouble to move one line from package.json to next.config.js.

@Florian-R
Copy link

@frol You're right I missed that, don't know why I was sure there was others options. This indeed shouldn't break any code, dismiss my previous comments.

@CompuIves CompuIves changed the title Exploration of different config Exploration of different config + expose webpack config Nov 10, 2016
@CompuIves
Copy link
Contributor Author

This PR now also includes #174

@@ -171,4 +172,7 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
return interpolateNames.get(this.resourcePath) || url
}
})
const config = await getConfig(dir)

Choose a reason for hiding this comment

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

why is this async? i don't see any way for it to be async.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh right! That need was removed since we now use require. Good catch, I'll update the PR.

@dbo dbo mentioned this pull request Nov 16, 2016
@rauchg
Copy link
Member

rauchg commented Nov 16, 2016

Important: since this effectively deprecates package.next (which I'm 100% ok with), we should probably WARN if we find it

@arunoda arunoda self-assigned this Nov 16, 2016
This was referenced Nov 16, 2016
Copy link
Contributor

@arunoda arunoda left a comment

Choose a reason for hiding this comment

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

@CompuIves could you check these suggestion.
After that, we could get ship this PR :)


let data
let module
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to do a sync FS check rather doing this.
That's cleaner.

const defaultConfig = { cdn: true }
const defaultConfig = {
cdn: true,
webpack: (cfg, hotReload) => cfg
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a pretty good idea if we could log something, when we are using this user supplied webpack config decorator.
So, I suggest to not to define this.
And when we are using this, we can check for webpack decorator and log it.

@@ -171,4 +172,7 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
return interpolateNames.get(this.resourcePath) || url
}
})
const config = getConfig(dir)
const userWebpackConfig = config.webpack(webpackConfig, hotReload)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do this only if user provides a webpack decorator.
If there's a one, we should log it so the user know this is happening.

} else {
throw err
}
}

// no try-cache, it must be a valid json
const config = JSON.parse(data).next || {}
const config = module.default || module || {}
Copy link
Contributor

Choose a reason for hiding this comment

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

We we should also read the package.json and check for the next config. If it's there, we should get it. (Only the cdn field).
And if we found it, we need log a message as it's deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

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

@nkzawa @rauchg do we have any specific format to log these messages.

Copy link
Contributor

Choose a reason for hiding this comment

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

no, I think

@jonaswindey
Copy link
Contributor

Could we have a variable className on https://github.com/zeit/next.js/blob/master/lib/document.js#L11 in this config too?

That would cover #251

@CompuIves
Copy link
Contributor Author

@arunoda Processed your feedback!
Maybe we could make a helper function in the future to show warnings/notices/errors with the use of chalk to make them stand out more.

@jonaswindey I think we should create a separate PR for that issue so we can put this PR live sooner.

@rauchg
Copy link
Member

rauchg commented Nov 17, 2016

@jonaswindey @CompuIves the right way to address that is #251

@rauchg
Copy link
Member

rauchg commented Nov 18, 2016

It just occurred to me that it might be smart to restart the next process (with the same flags) when this file changes? 🤔

@rauchg
Copy link
Member

rauchg commented Nov 18, 2016

Unless, of course, it's tractable to reload everything without a full restart

Copy link
Contributor

@arunoda arunoda left a comment

Choose a reason for hiding this comment

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

This is beautiful. We just need some few messages changes. I've added comments for them.

You may also need to rebase again since there are some conflicts.

Also we need to add a section to doc about configuring webpack config. Here's a suggestion for the layout:

  • Add sub section about Customizing webpack config.
  • Tell about the main goal of this.
  • Show a simple example (like importing images directly)
  • Then, ask to use this with care. (Since user could broke our default config)
  • So show safe operations like adding plugins, aliases, loaders etc.

Also try to keep the doc for this minimal and we can have a full detailed section on the Wiki.

@@ -171,4 +172,10 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
return interpolateNames.get(this.resourcePath) || url
}
})
const config = getConfig(dir)
if (config.webpack) {
console.log('Using Webpack config function defined in next.config.js.')
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a "> " before the message. Like this:

console.log('> Using Webpack config function defined in next.config.js.')

}

if (packageConfig) {
console.warn("WARNING: You're using package.json as source of config for next.js. Please use next.config.js instead.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Change this to:

console.log("> [warn] You are using package.json for next.js config. Use next.config.js instead.");

Just FYI: Try not to use "Please" in front of messages. It's sounds like commanding.
I tend to use that and one of my UK friend asked to drop that. Messages sounds nice after that :)

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 see, thanks for the tip!

@CompuIves
Copy link
Contributor Author

CompuIves commented Nov 19, 2016

@arunoda Added docs, let me know what you think! I'll rebase tomorrow, out of time now 😅

@rauchg Hmm, this is a valid point. I'll check if there is a way to restart the server when the config file changes.

@arunoda
Copy link
Contributor

arunoda commented Nov 19, 2016

@rauchg That's a good point, but it seems like it's hard to do that via webpack itself.

We have to do a hard process restart, may be we could do it using the grunt config easily.

@arunoda
Copy link
Contributor

arunoda commented Nov 19, 2016

@CompuIves yep. Changes are great. Shall we try @rauchg's suggestion too.

@CompuIves
Copy link
Contributor Author

CompuIves commented Nov 20, 2016

@arunoda Added config watching, it kills the old child process and start a new one when it sees a change in next.config.js (only for dev mode).

Also rebased with master!

@nkzawa
Copy link
Contributor

nkzawa commented Nov 20, 2016

I wonder if we should:

  • transpile the next.config.js (support import export, async/await etc)
  • support asynchronous operations in each config
export default {
  async webpack (webpackConfig, hotReload) {
    const newConfig = { ...webpackConfig }
    newConfig.something = await getSomething()
    return newConfig
  }
}

@coveralls
Copy link

coveralls commented Dec 17, 2016

Coverage Status

Coverage increased (+2.9%) to 60.036% when pulling 2316e55 on CompuIves:new_config into b62a0e8 on zeit:master.

@CompuIves
Copy link
Contributor Author

CompuIves commented Dec 17, 2016

Rebased master and did the required changes.

I stumbled upon the item of CDN in the FAQ of the README. Do we still need this?

```javascript
// next.config.js
module.exports = {
cdn: true
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have a cdn option. May be we need to re-phrase this.

@@ -423,7 +483,7 @@ For this reason we want to promote a situation where users can share the cache f

We are committed to providing a great uptime and levels of security for our CDN. Even so, we also **automatically fall back** if the CDN script fails to load [with a simple trick](http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx).

To turn the CDN off, just set `{ “next”: { cdn: false } }` in `package.json`.
To turn the CDN off, just set `module.exports = { cdn: false }` in `next.config.js`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't worry about this one. We'll remove it anyway.

@arunoda
Copy link
Contributor

arunoda commented Dec 17, 2016

@CompuIves I added some comments.

@arunoda arunoda mentioned this pull request Dec 17, 2016
@rauchg rauchg merged commit 5ab7463 into vercel:master Dec 17, 2016
@rauchg
Copy link
Member

rauchg commented Dec 17, 2016

I'll remove the cdn notes myself

@rauchg
Copy link
Member

rauchg commented Dec 17, 2016

Thanks @CompuIves

@arunoda
Copy link
Contributor

arunoda commented Dec 17, 2016

Awesome!

@CompuIves
Copy link
Contributor Author

Nice! Was great to help! 🎉

@lukeed lukeed mentioned this pull request Dec 27, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.