1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Parcel は dotenv をサポートしています

Posted at

Parcel: Environment Variables にあるように、Parcel は dotenv をサポートしています。

.env.development
API_URL=http://localhost:3000
.env.production
API_URL=http://example.com/
index.js
console.log(process.env.API_URL)

parcel index.html のように development モードで起動すると、.env.development で指定した値がコンソールに表示されます。
parcel build index.html のように production モードでビルドすると、.env.production で指定した値がコンソールに表示されます。

production ビルドで出力された JS ファイルでは、以下のように process.env.API_URL が値に置換されています。

index.js
parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(parcelRequire=f,i)throw i;return f}({"Focm":[function(require,module,exports) {
console.log("http://example.com/");
},{}]},{},["Focm"], null)
//# sourceMappingURL=/fuga.59ec2613.js.map

webpack の EnvironmentPlugin みたいなものですね。最初 dotenv だからブラウザでは使えないと勝手に思い込んでいましたが、そんなことはなかった。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?