jmm / pathmodify

In a nutshell, rewrite (AKA alias, map) `require()` IDs / paths / directories to different values in browserify.
MIT License
27 stars 1 forks source link

'import' and 'export' may appear only with 'sourceType: module' #11

Open mnoble01 opened 8 years ago

mnoble01 commented 8 years ago

I'm getting this error when using pathmodify.

What I'm trying to do is alias an underscore-extend for all calls to require('underscore') (or import 'underscore' in ES6). The reason for this is I'm mixing in some of my own functions to underscore and I'd like them to be available anywhere underscore is required in my codebase.

Here's my underscore-extend.js:

import _ from 'vendor/underscore'

_.mixin({
...
})

export default _

And my gulpfile:

 let b = browserify({
    entries: `./app/${app}/index.js`, 
    debug: true,
    detectGlobal: false,
    paths: [
      './app/shared/',
      './app/shared/lib/',
      './app/'
    ]
  }).transform(browserifyShim) // config must be in package.json
    .transform('babelify', {presets: ['es2015'], compact: false})
    .transform('brfs')
    .plugin(pathmodify, {mods: [
      pathmodify.mod.id('underscore', 'shared/lib/underscore-extend')
    ]})

I'm wondering if it's just the order of transforms/plugins or if the pathmodify plugin is wrongly parsing the erroring file in ES6 instead of from the stream. Or something else I'm not even thinking of.

jmm commented 8 years ago

Hello @mnoble01. The error goes away if you just remove Pathmodify and leave everything else as-is? If that's the case, it suggests that Browserify thinks your module is in node_modules/ when using Pathmodify (not sure how that would happen though).

I'm wondering if it's just the order of transforms/plugins

That could be the issue in some cases, because a plugin can add a transform, but Pathmodify doesn't currently.

or if the pathmodify plugin is wrongly parsing the erroring file in ES6 instead of from the stream

It's definitely not that: Pathmodify doesn't parse files it all, it just affects Browserify's module path resolution behavior.

I'm not exactly sure off the top of my head how Pathmodify would interact with the paths config. For an experiment you could try setting your target like this:

var path = require('path');
pathmodify.mod.id(
  'underscore',
  path.join(__dirname, 'app/shared/lib/underscore-extend')
)

FWIW, the way the Node module system works, you should be able to just invoke your script that extends Underscore from your entry point, not have it export anything, and just import Underscore from your other modules since it should be the same object (although it looks like you have Underscore in vendor/underscore and would need to rewrite imports of underscore somehow anyway?).

Can you create a repo with a minimal reproduction?

By the way, I'd like to ask you a question about your usage of Pathmodify off GitHub and I'd appreciate if you would get in touch with me here (please make sure to include contact information).