์๋
ํ์ธ์, ์ ๋ ๋ธ๋ก๊ทธ๋ฅผ ์์ฑ ์ค์ด๋ฉฐ ๊ฒ์๋ฌผ ์์ธ์ ๋ชจ๋ ๊ฒ์๋ฌผ์ ๋์ดํ๊ณ ์ถ์ต๋๋ค. ๊ฐ ๊ฒ์๋ฌผ์๋ ํ์ง๊ฐ ์์ต๋๋ค.
์๋๋ ๋ด ์ฝ๋์
๋๋ค.
{posts.map(post => {
return <li key={post.node.fields.slug}>
<Link to={post.node.fields.slug}>
<img className="news-img" src={require(`../${post.node.frontmatter.cover.relativePath}`)} />
<h3 className="news-titles">{post.node.frontmatter.title}</h3>
<p className="news-desc"> {post.node.excerpt}</p>
</Link>
</li>
}
)}
ํ์ง๋ง ๊ฒฝ๊ณ ๋ฅผ ๋ฐ์์ต๋๋ค.
warning in ./src/pages/md/new4/new4.md
Module parse failed: /Users/magicly/development/blog/gatsby-starter-blog/src/pages/md/new4/new4.md Assigning to rvalue (1:2)
You may need an appropriate loader to handle this file type.
SyntaxError: Assigning to rvalue (1:2)
at Parser.pp$4.raise (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp$2.checkLVal (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:1509:12)
at Parser.pp$3.parseMaybeUnary (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:1685:24)
at Parser.pp$3.parseExprOps (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseExpression (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:1573:21)
at Parser.pp$1.parseStatement (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:727:47)
at Parser.pp$1.parseTopLevel (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:638:25)
at Parser.parse (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:516:17)
at Object.parse (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/webpack/lib/Parser.js:902:15)
at NormalModule.<anonymous> (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/webpack/lib/NormalModule.js:104:16)
at NormalModule.onModuleBuild (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/Users/magicly/development/blog/gatsby-starter-blog/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /Users/magicly/development/blog/gatsby-starter-blog/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
@ ./src/pages ^\.\/.*$
๊ฒฝ๊ณ ์๋ ๋ถ๊ตฌํ๊ณ ํ์ด์ง๋ ๊ด์ฐฎ์ ๋ณด์ด์ง๋ง ํ์ด์ง๋ฅผ ๋น๋ํ๊ณ ๋ฐฐํฌํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Uncaught TypeError: Cannot read property 'call' of undefined
at t (bootstrap 29073caโฆ:52)
at Object../src/components/Footer/footer.css (footer.css:1)
at t (bootstrap 29073caโฆ:52)
at Object.<anonymous> (index.jsx:5)
at Object../src/components/Footer/index.jsx (layout-component---index-adc8f24โฆ.js:153)
at t (bootstrap 29073caโฆ:52)
at Object.<anonymous> (index.jsx:3)
at Object../node_modules/babel-loader/lib/index.js?{"plugins":["/Users/magicly/development/blog/gatsby-starter-blog/node_modules/gatsby/dist/utils/babel-plugin-extract-graphql.js","add-module-exports","add-module-exports","add-module-exports","transform-object-assign",["transform-react-jsx",{"pragma":"Glamor.createElement"}],"babel-plugin-glamor"],"presets":["/Users/magicly/development/blog/gatsby-starter-blog/node_modules/babel-preset-react/lib/index.js","/Users/magicly/development/blog/gatsby-starter-blog/node_modules/babel-preset-es2015/lib/index.js","/Users/magicly/development/blog/gatsby-starter-blog/node_modules/babel-preset-stage-1/lib/index.js","/Users/magicly/development/blog/gatsby-starter-blog/node_modules/babel-preset-env/lib/index.js","/Users/magicly/development/blog/gatsby-starter-blog/node_modules/babel-preset-stage-0/lib/index.js","/Users/magicly/development/blog/gatsby-starter-blog/node_modules/babel-preset-react/lib/index.js"],"cacheDirectory":true}!./src/layouts/index.jsx (layout-component---index-adc8f24โฆ.js:757)
at t (bootstrap 29073caโฆ:52)
at index.jsx?8a0a:11
๊ทธ๋ฐ ๋ค์ ๋ด ์ฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํฉ๋๋ค.
{posts.map(post => {
const path = `../${post.node.frontmatter.cover.relativePath}`;
return <li key={post.node.fields.slug}>
<Link to={post.node.fields.slug}>
<img className="news-img" src={require(path)} />
<h3 className="news-titles">{post.node.frontmatter.title}</h3>
<p className="news-desc"> {post.node.excerpt}</p>
</Link>
</li>
}
)}
์ฝ์์ ๊ฒฝ๊ณ ๊ฐ ํ์๋์์ต๋๋ค.
``` ๊ฒฝ๊ณ 1๊ฐ์ ๊ฒฝ๊ณ ๋ก ์ปดํ์ผ๋จ 10:31:08
./src/pages/news/index.jsx์ ๊ฒฝ๊ณ
์ค์ํ ์ข
์์ฑ:
103:74-87 ์ข
์์ฑ ์์ฒญ์ ํํ์์
๋๋ค.
@ ./src/pages/news/index.jsx 103:74-87
and the error on web page:
์ค๋ฅ: '../md/new4/cover4.png' ๋ชจ๋์ ์ฐพ์ ์ ์์ต๋๋ค.
http://localhost :8000/commons.js:55706:42
webpackContextResolve(์นํฉ:/src/pages/news
^./. $:11webpackContext(์นํฉ:/src/pages/news^./. $:8
http://localhost :8000/commons.js:7945:98
๋ฐฐ์ด.๋งต
(ํ ์ข
์)
์ธ๋ฑ์ค.๋ ๋๋ง
webpack:///src/pages/news/index.js jsx:21
์ธ๋ฑ์ค.๋ ๋๋ง
webpack:///~/react-proxy/modules/createPrototypeProxy.js:44
http://localhost :8000/commons.js:30838:22
์ธก์ ์๋ช
์ฃผ๊ธฐ ์ฑ๋ฅ
webpack:///~/react-dom/lib/ReactCompositeComponent.js:75
ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext
webpack:///~/react-dom/lib/ReactCompositeComponent.js:794
I am sure the path '../md/new4/cover4.png' is right, because if I change code to:
{posts.map(๊ฒ์๋ฌผ => {
๋ฐํ
{post.node.excerpt}
๊ฐ์ฌํฉ๋๋ค~
๋๋ ๊ตฌ๊ธ
the request of a dependency is an expression
๊ทธ๋ฆฌ๊ณ ๋ง์นจ๋ด https://webpack.github.io/docs/context.html์ ์ฐพ์์ต๋๋ค. ์ด๊ฒ์ webpack dynamic์ด ์๊ตฌํ๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
my๋ฅผ ์ฝ๋๋ก ๋ณ๊ฒฝํ ํ
{posts.map(post => {
let path = `${post.node.frontmatter.cover.relativePath}`;
path = path.slice(0, path.lastIndexOf('.'))
return <li key={post.node.fields.slug}>
<Link to={post.node.fields.slug}>
<img className="news-img" src={require('../' + path + '.png')} />
<h3 className="news-titles">{post.node.frontmatter.title}</h3>
<p className="news-desc"> {post.node.excerpt}</p>
</Link>
</li>
}
)}
์ด์ ๋ชจ๋ ๊ฒ์ด ์ ์์ ๋๋ค.
์ด ์์ ์ฌํญ์ ์์ฑํด ์ฃผ์ @magicly ์๊ฒ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. Gatsbyv 0.x์์ v1.x๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ๋์ ์ด ๋ฌธ์ ๊ฐ ์ ๋ง ๋ฌธ์ ์์ต๋๋ค. ์ด๊ฒ์ ์ฐ์ฐํ ๋ฐ๊ฒฌํ ๋ค๋ฅธ ์ฌ๋์ ์ํด ๋๋ ๊ฐ์ ๊ฒ์ ์ํ์ง๋ง ํ์ผ ํ์ฅ์๊ฐ ๋์ ์ด๊ธฐ๋ฅผ ์ํ์ต๋๋ค.
์ ๊ฒฝ์ฐ์ @magicly์ ๊ฒฝ์ฐ ์ธ๊ด์ Markdown frontmatter์์ ์ค๋ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋ณ ๊ธฐ์ฌ์ ํ์ง ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค๊ณ ์์ต๋๋ค. Gatsby ๊ธฐ๋ณธ ์คํํฐ ๋ธ๋ก๊ทธ์ ๊ฐ์ ํ๋ก์ ํธ ์ปจํ ์คํธ์์ ๋ค์๊ณผ ๊ฐ์ ๋ด์ฉ์ด ์์ ์ ์์ต๋๋ค.
---
title: The title of the post
featured_image: ./teaser.jpg
---
๊ทธ๋ฐ ๋ค์ ๋ชจ๋ ๊ธฐ์ฌ๋ฅผ ๋์ดํ๋ ์์ธ โโํ์ด์ง์ GraphQL ์ฟผ๋ฆฌ์ featured_image
( @magicly ๊ฐ cover
์ด๋ผ๊ณ ํ ๊ฒ)๋ฅผ ํฌํจํ๋ ค๊ณ ํ ๊ฒ์
๋๋ค.
frontmatter {
title
featured_image {
name
ext
relativePath
}
}
์ด์ ๋ชจ๋ ๊ฒ์๋ฌผ์ ๋ฐ๋ณตํ๊ณ ์ธ๋ฑ์ค ํ์ด์ง์ ์ ๋ชฉ๊ณผ ์ถ์ฒ ์ด๋ฏธ์ง๋ฅผ ํ์ํ๊ณ ์ถ์ ๊ฒ์ ๋๋ค. React ๊ตฌ์ฑ ์์ ๋ด์์ ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
// โฆ
// Create a new require context for Webpack
const requireFeatureImage = require.context(
`./../pages/portfolio/`, // Donโt make this a variable
true, // Whether or not to check subdirectories
/^.*\.(svg|png|gif|jpg|jpeg|webp|)$/ // Rough regex for extensions, maybe change this for your use case?
)
return (
<ol>
{pages.map(function(page, i) {
let path = page.node.frontmatter.featured_image.relativePath
let imgPath = `./${path.split('portfolio/')[1]}`
return (
<li key={`Item_${page.node.fields.slug}`}>
<Link to={page.node.fields.slug}>
<img src={requireFeatureImage(imgPath)} />
<h2>
{page.node.frontmatter.title}
</h2>
</Link>
</li>
)
})}
</ol>
)
Webpack 2๊ฐ ํฌํจ๋ require.context
์ ๋ํ ๋ฌธ์ ๋ ์ค์ ์ ๋ ์์ธํ ์ค๋ช
ํ์ง๋ง ์ค์ ๋ก ์ด์ ๊ฐ์ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ๋ณด์ฌ์ฃผ์ง๋ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ Gatsby ๋ฌธ์ ๋ Webpack์ ๋ํ ์ฝ๊ฐ์ ํน์ ์ง์์ด ํ์ํ(์ฃ์กํฉ๋๋ค) ์๋นํ ์ผ๋ฐ์ ์ธ ํจํด์ธ ๊ฒ ๊ฐ์ง ์์ต๋๋ค.
@KyleAMathews ์ด๊ฒ์ด ๋ด๊ฐ ๋ฌธ์์ ๋ํด ํ์ฅํ๋ ๋ฐ ๊ด์ฌ์ด ์์ต๋๊น? ์๋๋ฉด ๋ด๊ฐ ๋์น ์ด๋ฐ ์ข ๋ฅ์ ๊ฒ์ ๋ํ ์/๋ ๋์ ์ ๊ทผ ๋ฐฉ์์ด ์์ต๋๊น? ๊ฐ์ฌ ํด์!
ํํ ๋ฆฌ์ผ์ ํํธ 5์์ ์ด์ ๋ํด ์ค๋ช ํ๊ฒ ์ง๋ง ๊ทธ ๋์ www์ gatsbygram ์ ํ์ธํ์ญ์์ค.
https://www.gatsbyjs.org/packages/gatsby-transformer-sharp/ ๋ฅผ ์ค์นํ๋ฉด frontmatter์ ์ฐ๊ฒฐ๋ ์ด๋ฏธ์ง๊ฐ graphql์ ํตํด ์ฟผ๋ฆฌํ ์ ์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๊ตฌ๊ธ
๊ทธ๋ฆฌ๊ณ ๋ง์นจ๋ด https://webpack.github.io/docs/context.html์ ์ฐพ์์ต๋๋ค. ์ด๊ฒ์ webpack dynamic์ด ์๊ตฌํ๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
my๋ฅผ ์ฝ๋๋ก ๋ณ๊ฒฝํ ํ
์ด์ ๋ชจ๋ ๊ฒ์ด ์ ์์ ๋๋ค.