LoginSignup
1
0

More than 5 years have passed since last update.

babel の import で指定するパスが間違っていても動いてしまう。

Last updated at Posted at 2018-05-01

import に指定するパスは case insensitive??

webpackとかReactとかExpressで遊んでいた時のことでした。
以下のようなコードを書いていたのですが、ブラウザで動作させた時は確かに動作していました。
(Helloコンポーネントが正しく表示されていた)

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Hello from './components/Hello';

ReactDOM.render(
  <Hello compiler="TypeScript" framework="React" />,
  document.getElementById('app')
);

しかし、Reactなどとは関係ないwebpackに適用したプラグインが正しく動作していませんでした。
かなり悩んだのですが、import Hello from './components/Hello';componentsのところをComponentsにしたら正しく動くようになりました。
確かに正しいパスはComponentsの方なんですが、逆にReactはなぜ動作していたのか謎でした。

原因

調べてみても情報は出てこなかったので、以下完全に推測です。

babelも使用しているのでimportrequireに変換されるのですが、これはnode.jsのシステムなようです。
で、requireはOSのファイルシステムに依存しているんじゃないかな〜と思います。
ちらほらそんなIssueを見かけたので・・・
https://github.com/webpack-contrib/css-loader/issues/53

で、私の使用しているOSはMacなので case insensitiveで動作してしまった・・・・と推測しています。

ESlintも導入しているのにエラーを表示してくれない・・・
何か良い手はないかな:eye::eye:

追記 ( 2018/05/04 )
npmにパスをチェックしてくれる良い感じのを見つけました。
case-sensitive-paths-webpack-pluginです。

This Webpack plugin enforces the entire path of all required modules match the exact case of the actual path on disk. Using this plugin helps alleviate cases where developers working on OSX, which does not follow strict path case sensitivity, will cause conflicts with other developers or build boxes running other operating systems which require correctly cased paths.

上記説明にある通り、パスが正確に記述されているかをチェックしてくれます。
これにより、OS間の差異を埋めることができます。
実際に試して正確に動作しました!!

1
0
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
0