Launch Browserify to convert dependencies?

I work on two Node packages at once, I will call them the Library and the Consumer. The library is responsible for providing a bunch of things in the browser. All users are import Library from 'library' and call Library(someConfigHere) - this is basically just a test to make sure the library does what I expect in the browser.

I have npm link ed Library in Consumer and I try to launch Browserify on Consumer, but I get this error: ParseError: 'import' and 'export' may appear only with 'sourceType: module' . The library does indeed contain the ES6 export statement, so I assume that Browserify only works with Consumer, not the library.

So my question is: is there a way to get Browserify to also convert dependencies ?

This is my package.json :

 { "name": "consumer", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "budo index.js --port $PORT", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "devDependencies": { "babel-preset-es2015": "^6.13.2", "babel-preset-react": "^6.11.1", "babelify": "^7.3.0", "browserify-shim": "^3.8.12" }, "browserify": { "transform": [ "babelify" ] }, "babel": { "presets": [ "es2015", "react" ] } } 

This is consumer index.js :

 import Library from 'library' // <= this is what isn't getting babelified console.log(Library); 

This is the index.js library:

 export default (config) => { console.log('Testing testing') } 
+6
source share
1 answer

Browserify transforms can be configured as global, which means that they will apply to files inside node_modules too.

Configuration is performed for each conversion. With babelify, you should configure it like this:

 browserify().transform("babelify", { global: true }) 

Or, if you use the command line, for example:

 browserify ... -t [ babelify --global ] ... 

<y> Or, to configure it in package.json , it should be something like this (note the added square brackets):

 "browserify": { "transform": [ ["babelify", { "global": true }] ] } 

Babelify also implements the ignore parameter, so it can be configured to convert only the files to node_modules that you want. There is more details here .

Another solution would be to include a similar browserify / babelify in your library module package.json . When processing dependencies, Browserify checks the specified pacakge.json dependency pacakge.json for transformations and will apply any configured ones.

+4
source

Source: https://habr.com/ru/post/1013106/


All Articles