diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49103d2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +.DS_Store +package-lock.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..282f6b0 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "tabWidth": 4, + "semi": false, + "singleQuote": true +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..a33fc40 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# Next.js + Webpack Aliases + +Use [Webpack Aliases](https://webpack.js.org/configuration/resolve/#resolve-alias) in your Next.js project + +## Installation + +``` +npm install --save @blunck/next-alias +``` + +## Usage + +Create a `next.config.js` in your project + +```js +// next.config.js +const withAlias = require('@blunck/next-alias')({ + '@components': __dirname + '/components' +}) +module.exports = withAlias() +``` + +You can now use aliases in your imports + +```js +import foo from '@components/Blog' + +export default () => +``` + +### Configuring Next.js + +Optionally you can add your custom Next.js configuration as parameter + +```js +// next.config.js +const withAlias = require('@blunck/next-alias')() +module.exports = withAlias({ + webpack(config, options) { + return config + } +}) +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..cb8b0a8 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +module.exports = (aliases = {}) => (nextConfig = {}) => { + return Object.assign({}, nextConfig, { + webpack(config, options) { + Object.assign(config.resolve.alias, aliases) + + if (typeof nextConfig.webpack === 'function') { + return nextConfig.webpack(config, options) + } + + return config + } + }) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..fe42c6e --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "@blunck/next-alias", + "version": "1.0.0", + "description": "Use Webpack Aliases in your Next.js project", + "main": "index.js", + "repository": "alexblunck/next-alias", + "keywords": ["nextjs", "alias-loader", "alias"], + "author": "Alexander Blunck", + "license": "MIT", + "bugs": { + "url": "https://github.com/alexblunck/next-alias/issues" + }, + "homepage": "https://github.com/alexblunck/next-alias#readme" +}