Resolve __dirname from a package?

I’m building a package with a compile plugin that needs to be aware of it’s execution path, as Node’s __dirname & __filename don’t work in Meteor does anybody know if this can be achieved reliably?

This is the best info I’ve found so far but doen’t help with this case: http://www.tagwith.com/question_247979_how-do-i-obtain-the-path-of-a-file-in-a-meteor-package

1 Like

Presumably they don’t work because they get passed straight through to the combined JS file?
Doesn’t sound like it would be impossible for the JS processor to substitute with literals.

Relying on the execution path is probably not the best idea in the context where files are compiled and moved several times.

If you describe your usecase, we could think of an alternative solution?

1 Like

Thanks @slava, @sdarnell!

This is one of those awkward CSS compilation problems where the compiler needs to access imports that are included via an npm library.

The package defines a hook for a type of .css file, then runs it through rework that includes basscss as a local NPM module dependency. I need to transform all @import ‘xxx’ module paths to point to the basscss NPM package and all relative includes to remain relative to the original input file location.

eg:
– in meteor app
client/my-css-definition.HOOK.css

– in package

  • loads ‘my-css-definition.HOOK.css’
  • finds all relative @import ‘module’ lines
  • rewrites to included basscss npm module path
  • finds all @import ‘./my-additional-module’ lines
  • rewrites to original file location path using compileStep.inputPath || compileStep.fullInputPath
  • processes the result through the transform
  • returns the output using compileStep.addStylesheet()

If this is possible it will allow me to write a nice set of modular extensions to basscss in my meteor app and include exactly what is necessary per application.

But if it sounds crazy feel free to sway me otherwise :slight_smile:

1 Like

I use the assets type to get a predictable path. I haven’t used this inside a source handler plugin. So it may not work.

  1. Add api.addFiles(['.npm/package/node_modules/<NPM PACKAGE NAME>/<FILE NAME>.js', server', {isAsset: true}) to package.js (Example)
  2. Add this file to your app: https://github.com/Sanjo/meteor-karma/blob/master/lib/meteor/files.js. It’s the function to get the absolute path to your app root directory. Taken from Meteor core.
  3. Generate the absolute path to the asset file like this: https://github.com/Sanjo/meteor-karma/blob/master/main.js#L102-L112
1 Like

Cheers @Sanjo, I’ll try this out. It does seem like there should be an easier way to solve this but hopefully I can work with this approach for now.

Any other ideas @slava?

The problem you are facing is something that several people at MDG were thinking about lately.

Right now it is a known problem with the Meteor packaging system, sharing a stylesheet in a format of Less or Sass mixings is not possible w/o hacks. Sadly, I don’t have any good advice on how to achieve this at the moment, all solutions I’ve heard were very hacky.

Good to know, is there an issue open for this or is it just on the radar? Happy to contribute to a solution as I see this becoming more important for building larger apps that share components.