简体   繁体   中英

es6 not working for my custom npm module

I've just extracted part of my javascript app (written in Vue2 and nuxt.js but that's not important) to an external npm package.

My main app is using babel for es6 code and all the files written in that package are using es6 syntax. The problem is - after extracting es6 code to an external npm module, uglifyjs started to complain about their syntax. Errors suggests that uglify treats them as regular javascript files, not es6.

How can i tell node to treat my package files as es6?


Edit:

Sorry for not providing a code sample but there is really not much to be shown. I have a regular webpack app generated by nuxt.js/vue-cli generator. It uses babelify and uglify to make it able to use es6 sytnax. All the main app is written using es6.

Let's say i moved two files to external module - A and B (both in es6). Module A requires module B and that's when uglify complains - that the B file is not written in es5. It seems that module required internally is not getting babelified first for some reason. Is that an expected behaviour?

There are few solutions,

one is to mention your extracted module as part of the vendors, so in webpack you will have :

entry: {
    ...
    vendor: [
        'jquery', 
        'my-extracted-module'
    ]
},

And use the CommonsChunkPlugin :

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        filename: 'vendor.js'
    })
]

And compile my-extracted-module as a standalone (you could use es:next feature in it's package.json)

Another solution is just tell webpack to include that package when building, see

https://github.com/webpack/webpack/issues/2031

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM