Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

66
Vistas
How to create Javascript library OOP and build bundle with Webpack?

I try to create a Javascript library with OOP style and make it can run with the browser. I am a newbie to modern javascript.

My Example Code: mylib.js

function MyLib() {
if (!(this instanceof MyLib)) {
        return new MyLib(options)
    }

    this.init(options);

    return this
}

MyLib.prototype.init = function (options) {
    this.debug = options.debug
    console.log(options)
}

.... ///

export default MyLib

index.js

import MyLib from './mylib.js';

index.html

<script src="dist/mylib.js"></secript>
<script>
     let mylib = new({
         debug: true 
     });
</script>

webpack.config.js

const webpack = require('webpack')
const path = require('path')

module.exports = {
    entry: {
        mylib: './lib/index.js'
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
    },
    mode: process.env.MINIFY_BUILD ? 'production' : 'development',
    plugins: [
        new webpack.DefinePlugin({
            'procrss.env.NODE_ENV': 'production'
        })
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules\/(?!(lit-element|lit-html)\/).*/,
                loader: 'babel-loader'
            }
        ]
    }
}

So after all, I got an error message like below

Uncaught ReferenceError: MyLib is not defined

Please help or suggest to me.

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos