1

Let's say I have a file plugin.js, with the following contents:

class MyCompiler {
  processFilesForTarget(files) {
    files.forEach(file => {
      const output = myExternalFunction(file);
      file.addJavaScript({
        data: output,
        path: `${file.getDisplayPath()}.js`
      });
    })
  }
}

Plugin.registerCompiler({
  extensions: ['rb', 'js.rb'],
  filenames: []
}, () => new MyCompiler());

I also have a file myExternalFile.js, which is located beside plugin.js. I want to import myExternalFunction from myExternalFile to use in plugin.js.

I tried using import { myExternalFunction } from 'myExternalFile'; but that presents the error 'import' and 'export' may only appear at the top level (17:0).

Using require presents this error: require is not defined.

And finally, using Npm.require won't work because it's not an NPM dependency.

So how do I actually accomplish this?

Cereal
  • 3,699
  • 2
  • 23
  • 36
  • what's the issue with just doing the `import` at the top level in the file (on top)? Because that *should* work. Are you trying to optimize for the case where the project doesn't have any `rb` files and hence you could save importing something that you don't need? – Christian Fritz Feb 07 '20 at 16:14
  • That error using import is what happens when you use import at the top of the file. – Cereal Feb 07 '20 at 16:24
  • It looks like adding any files to your `sources` in Meteor's `package.js` implicitly loads the file globally, so that looks like it solves my problem. It's just strange. – Cereal Feb 07 '20 at 16:25
  • 1
    That's really odd, because as far as I know `plugin.js` is not a special file. So this file should be processed like any other, and of course you can `import` in a file. I'm assuming the issue is not just that you didn't say `from './myExternalFile';` (i.e., local), right? – Christian Fritz Feb 07 '20 at 16:46

0 Answers0