Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mnoble01 opened this issue Jul 29, 2016 · 1 comment
Open

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

mnoble01 opened this issue Jul 29, 2016 · 1 comment

Comments

@mnoble01
Copy link

mnoble01 commented Jul 29, 2016

##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
Copy link
Owner

jmm commented Jul 29, 2016

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).

@mnoble01 mnoble01 reopened this Jul 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants