fwouts / bazel-javascript

JavaScript and TypeScript rules for Bazel (React friendly)
MIT License
65 stars 17 forks source link

Enhance import resolving #49

Open schoren opened 5 years ago

schoren commented 5 years ago

I have encountered 2 issues with import resolving:

  1. If I have to import a file from a js_library that lives in a parent path, I have to use a lot of '../..' in the path. That makes it hard to move files around without breaking anything.

Example:

.
|-- app
|---- src
|------ index.js (import '../../lib/common/net.js';)
|-- lib
|---- commonn
|------ net.js
  1. I create some json files using jsonnet. I then import them as a js_library, but the path is something like this: bazel-out/k8-fastbuild/bin/app/config.json.

Exampe:

import '../../bazel-out/k8-fastbuild/bin/app/config.json';

The problem is that the fastbuild dir is prefixed with the target architecture (k8/darwin). Then, I cannot resolve the import in a cross platform way. If I use the k8 prefix, it won't work on MacOS, and vice versa.

I have created #48 to solve both issues. I think the implementatios is pretty generic, so it should work for anyone.

Thanks

josefermin commented 5 years ago

@schoren, thanks for your change. My imports looks way nicer now.

I also have a some generated files and I was dealing with the same cross platform problem. I tried #48 but it didn't work for me because the generated files are under 'bazel-out/darwin-fastbuild/genfiles' not under 'bazel-out/darwin-fastbuild/bin' . Adding GENDIR path without modifications solves the problem.

  const gendir = path.join(base, process.env["GENDIR"], '..', 'bin')
  const gendir2 = path.join(base, process.env["GENDIR"])

See #53 which is currently working for me.