[英]Next.js - webpack aliasing component folders doesn't work
I am extending next.js webpack configuration as suggested in docs. 我正在按照文档中的建议扩展next.js webpack配置。
const path = require('path')
module.exports = {
webpack: (config, { dev }) => {
config.resolve = {
alias: {
Templates: path.resolve(__dirname, 'templates/'),
Components: path.resolve(__dirname, 'components/')
}
}
return config
}
}
I want to make my imports to work like this: 我想让我的导入像这样工作:
import Template from 'Templates/Base'
import Input from 'Components/Input'
What have I done wrong in configuration because I am getting errors such as: 我在配置中做错了什么,因为出现以下错误:
Cannot find module 'Components/Header' 找不到模块“组件/标题”
I am structuring my directory like this: 我正在像这样构建目录:
.next
.storybook
components
|_ Header
|__ index.js
|_ Input
|__ index.js
templates
|_ Base
|__ index.js
pages
|_ index.js
node_modules
containers
stories
...
Instead of editing webpack file, it is required to create .babelrc
file with these contents: 无需编辑Webpack文件,而是需要使用以下内容创建.babelrc
文件:
{
"plugins": [
["module-alias", [
{ "src": "./components", "expose": "Components" },
{ "src": "./containers", "expose": "Containers" },
{ "src": "./templates", "expose": "Templates" }
]]
],
"presets": [
"es2015",
"next/babel"
]
}
This way we extended Next.js .babelrc configuration and it works well on serverside too. 这样,我们扩展了Next.js .babelrc配置,它在服务器端也很好用。
const path = require("path");
module.exports = {
webpack: function(config, { dev }) {
config.resolve.alias = {
Templates: path.resolve(__dirname, "templates/"),
Components: path.resolve(__dirname, "components/")
};
return config;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.