I use webpack4 + babel7. In my JS script I need to import NPM module. This module uses ES6 classes and modules. And after building bundle this class not transpiling to standart functions. How to make the NPM module I need was completely transpiled?
package.json
{ "scripts": { "dev": "webpack --config webpack.config.js --mode=development", "prod": "webpack --config webpack.config.js --mode=production" }, "dependencies": { "@betakiller/wamp-wrapper": "^0.1.0", "babel-polyfill": "^6.26.0", "chalk": "^2.4.1", "whatwg-fetch": "^3.0.0" }, "devDependencies": { "@babel/cli": "^7.1.2", "@babel/core": "^7.1.2", "@babel/preset-env": "^7.1.0", "babel-loader": "^8.0.4", "webpack": "^4.20.2", "webpack-cli": "^3.1.2" } } webpack.config.js
const path = require('path'); const paths = { 'src': __dirname, 'dist': path.resolve(__dirname, 'dist') }; module.exports = { entry: { app: paths.src + '/app.js' }, output: { filename: '[name].js', path: paths.dist }, module: { rules: [ { test: /\.js$/, use: 'babel-loader' } ] } }; .babelrc
{ "presets": [ [ "@babel/preset-env", { "useBuiltIns": "usage", "forceAllTransforms":true } ] ] } app.js
console.log('app.js'); import BetakillerWampFacade from '@betakiller/wamp-wrapper'; import ImportedClass from './ImportedClass'; const WampFacade = new BetakillerWampFacade(); console.log('WampFacade', WampFacade); const Imported = new ImportedClass(); console.log('Imported', Imported); ImportedClass.js
'use strict'; export default class ImportedClass { action(){ console.log('ImportedClass'); } } Failed test in webpack.config.js
module: { rules: [ { test: /\.js$/, use: 'babel-loader', exclude: /node_modules\/(?!@betakiller\/wamp-wrappe).*/ } ] } My solution
- move babel config to the webpack config
- remove
.babelrc
new webpack.config.js
const path = require('path'); const paths = { 'src': __dirname, 'dist': path.resolve(__dirname, 'dist') }; module.exports = { entry: { app: paths.src + '/app.js' }, output: { filename: '[name].js', path: paths.dist }, module: { rules: [ { test: /\.js$/, loader: 'babel-loader', options: { ignore: [], presets: [ [ "@babel/preset-env", { forceAllTransforms: true } ] ] }, } ] } };
exclude: /node_modules\/(?!@betakiller).*/? because the.*means any character zero times or more times, you don't need the restpackage.jsonof@betakiller/wamp-wrapper, if the file mentioned in themainproperty ends with.js? Because that rule you have ONLY runs on.jsfilespackage.jsonof module@betakiller/wamp-wrapper:"main": "./src/BetakillerWampFacade.js",