{"id":1316,"date":"2021-05-13T19:30:00","date_gmt":"2021-05-13T10:30:00","guid":{"rendered":"https:\/\/ito-u-oti.com\/?p=1316"},"modified":"2021-05-13T17:52:43","modified_gmt":"2021-05-13T08:52:43","slug":"react-dotenv","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=1316","title":{"rendered":"[React.js]dotenv\u3092\u5229\u7528\u3057\u3066\u74b0\u5883\u5dee\u5206\u3092\u5438\u53ce\u3059\u308b"},"content":{"rendered":"\n<p>\u958b\u767a\u7528\u3001\u672c\u756a\u7528\u3067ajax\u3092\u4f7f\u3063\u305f\u63a5\u7d9a\u5148URL\u304c\u9055\u3046\u3002\u3068\u3044\u3063\u305f\u74b0\u5883\u3054\u3068\u306e\u5dee\u5206\u3092\u5438\u53ce\u3059\u308b\u305f\u3081\u306b\u3001React.js\u3067\u306fdotenv\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__1\" class=\"wp-block-heading\">dotenv \u306e\u5c0e\u5165<\/h2>\n\n\n\n<h3 id=\"outline__1_1\" class=\"wp-block-heading\"><code>env<\/code>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u3001<code>.env.[\u74b0\u5883\u540d]<\/code>\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u308b<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"202\" height=\"89\" src=\"https:\/\/ito-u-oti.com\/wp-content\/uploads\/2021\/05\/image-28.png\" alt=\"\" class=\"wp-image-1318\"\/><\/figure>\n\n\n\n<p>.env.local<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nREACT_APP_ENV_NAME=hello_local_world\n<\/pre><\/div>\n\n\n<p><code>REACT_APP_[\u74b0\u5883\u5909\u6570\u540d]=\u5024<\/code>\u3068\u3044\u3046\u5f62\u3067\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>src \u4e0a\u3067\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u3001<code>process.env.REACT_APP_[\u74b0\u5883\u5909\u6570\u540d]<\/code>\u3067\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__1_2\" class=\"wp-block-heading\">package.json\u306e\u4fee\u6b63<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  ...\n  'scripts':\n    {\n      'start': 'dotenv -e .\/env\/.env.local react-scripts start',\n      'build': 'dotenv -e .\/env\/.env.local react-scripts build',\n      'build_staging': 'dotenv -e .\/env\/.env.staging react-scripts build',\n      'test': 'dotenv -e .\/env\/.env.test react-scripts test',\n    },\n  ...\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__1_3\" class=\"wp-block-heading\">(option)<code>.gitignore<\/code>\u306e<code>.env.[\u74b0\u5883\u540d]<\/code>\u306e\u884c\u3092\u524a\u9664\u3059\u308b <\/h3>\n\n\n\n<p>create-react-app\u3067\u4f5c\u6210\u3057\u305fproject\u3067\u81ea\u52d5\u7684\u306b\u4f5c\u3089\u308c\u308b.gitignore\u306e\u4e2d\u3067\u306fenv\u30d5\u30a1\u30a4\u30eb\u304cignore\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3053\u308c\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# See https:\/\/help.github.com\/articles\/ignoring-files\/ for more about ignoring files.\n\n# dependencies\nnode_modules\n.pnp\n.pnp.js\n\n# testing\ncoverage\n\n# production\nbuild\n\n# misc\n.DS_Store\n# .env.local\n# .env.development.local\n# .env.test.local\n# .env.production.local\n\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\n\n# sonarQube\n.scannerwork\n<\/pre><\/div>\n\n\n<p>\u3053\u308c\u3067\u5c0e\u5165\u306f\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__2\" class=\"wp-block-heading\">\u4f7f\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<p>App.tsx\u306benv\u3092\u8aad\u307f\u8fbc\u3080\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport React from 'react';\nimport logo from '.\/logo.svg';\nimport '.\/App.css';\n\nconst envName = process.env.REACT_APP_ENV_NAME;\n\nfunction App() {\n  return (\n    &amp;lt;div className=&quot;App&quot;&gt;\n      &amp;lt;header className=&quot;App-header&quot;&gt;\n        &amp;lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; \/&gt;\n        &amp;lt;p&gt;\n          Edit &amp;lt;code&gt;src\/App.tsx&amp;lt;\/code&gt; and save to reload.\n        &amp;lt;\/p&gt;\n        &amp;lt;a\n          className=&quot;App-link&quot;\n          href=&quot;https:\/\/reactjs.org&quot;\n          target=&quot;_blank&quot;\n          rel=&quot;noopener noreferrer&quot;\n        &gt;\n          Learn React\n        &amp;lt;\/a&gt;\n        &amp;lt;div&gt;{envName}&amp;lt;\/div&gt;\n      &amp;lt;\/header&gt;\n    &amp;lt;\/div&gt;\n  );\n}\n\nexport default App;\n\n<\/pre><\/div>\n\n\n<p>\u5b9f\u884c<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nyarn start\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"571\" height=\"535\" src=\"https:\/\/ito-u-oti.com\/wp-content\/uploads\/2021\/05\/image-29.png\" alt=\"\" class=\"wp-image-1319\"\/><\/figure>\n\n\n\n<p>\u4e0b\u6bb5\u306benv\u3067\u8a2d\u5b9a\u3057\u305f\u5024\u304c\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__3\" class=\"wp-block-heading\">dotenv\u3068jest &#8211;coverage\u3092\u4f75\u7528\u3057\u305f\u3044\u5834\u5408<\/h2>\n\n\n\n<p>jest &#8211;coverage\u3068dotenv\u3092\u666e\u901a\u306b\u7d44\u307f\u5408\u308f\u305b\u3066\u4f7f\u3046\u3068\u3001coverage\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u52b9\u304d\u307e\u305b\u3093\u3002\uff08jest\u30b3\u30de\u30f3\u30c9\u3067\u8a8d\u8b58\u3057\u3066\u304f\u308c\u306a\u3044\uff09<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndotenv -e .\/env\/.env.test jest --coverage\n<\/pre><\/div>\n\n\n<p>coverage\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6709\u52b9\u306b\u3059\u308b\u305f\u3081\u306b\u3001dotenv\u30b3\u30de\u30f3\u30c9\u3067\u306e\u5b9f\u884c\u3067\u306f\u306a\u304fjest\u30b3\u30de\u30f3\u30c9\u306e\u5b9f\u884c\u3068\u3057\u3066env\u30d5\u30a1\u30a4\u30eb\u3092\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u8aad\u307f\u8fbc\u307e\u305b\u308b\u5f62\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__1_2\" class=\"wp-block-heading\">package.json\u306e\u4fee\u6b63<\/h3>\n\n\n\n<p>test\u30b3\u30de\u30f3\u30c9\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  &quot;scripts&quot;: {\n    &quot;test&quot;: &quot;jest --setupFiles .\/src\/test\/setup-test.ts --coverage&quot;\n  }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__3_2\" class=\"wp-block-heading\">setup-test.ts\u306e\u4f5c\u6210<\/h3>\n\n\n\n<p>setup-test.ts\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"253\" height=\"268\" src=\"https:\/\/ito-u-oti.com\/wp-content\/uploads\/2021\/05\/image-30.png\" alt=\"\" class=\"wp-image-1320\"\/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport dotenv from 'dotenv';\n\n\/\/test\u7528\u306eenv\u3092\u8aad\u307f\u8fbc\u3080\ndotenv.config({ path: '..\/..\/env\/.env.test' });\n<\/pre><\/div>\n\n\n<h2 id=\"outline__4\" class=\"wp-block-heading\">\u95a2\u9023<\/h2>\n\n\n\n<p><a href=\"https:\/\/ito-u-oti.com\/react-monorepo\/\">[React.js]monorepo\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b+typescript<\/a><\/p>\n\n\n\n<h2 id=\"outline__5\" class=\"wp-block-heading\">\u53c2\u8003<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/50259025\/using-env-files-for-unit-testing-with-jest\">Using .env files for unit testing with jest<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u958b\u767a\u7528\u3001\u672c\u756a\u7528\u3067ajax\u3092\u4f7f\u3063\u305f\u63a5\u7d9a\u5148URL\u304c\u9055\u3046\u3002\u3068\u3044\u3063\u305f\u74b0\u5883\u3054\u3068\u306e\u5dee\u5206\u3092\u5438\u53ce\u3059\u308b\u305f\u3081\u306b\u3001Reac [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1314,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[298],"tags":[299,295,296,283],"class_list":["post-1316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-js","tag-dotenv","tag-react-js","tag-typescript","tag-yarn"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1316"}],"version-history":[{"count":1,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1316\/revisions"}],"predecessor-version":[{"id":1321,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1316\/revisions\/1321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/media\/1314"}],"wp:attachment":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}