Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

98
Visualizações
Custom react hook library. Webpack externals and peerDependencies doesn't work

Each new try I getting 2 cases:

"Invalid hook call" - because of double React environment.

"Can't resolve "react"" - if I would remove it from devDependencies in hook library.

Goal is : to set up correct build environment of hook library.

webpack.config.js

const path = require("path")

module.exports = {
    mode: "production",
    entry: "./src/index.ts",
    output: {
        filename: "index.js",
        path: path.resolve(__dirname, 'dist'),
        clean: true,
        library: "custom-hook-lib",
        libraryTarget: "umd"
    },
    externals: {
        'react': 'react',
        'react-dom' : 'reactDOM'
    },

    resolve: {
        extensions: [".js", ".jsx", ".json", ".ts", ".tsx"]
    },
    
    module: {
        rules: [
            {
                test: /\.(j|t)sx?$/,
                exclude: /(node_modules)/,
                use: 'ts-loader'
            }
        ]        
    }
    
}

package.json

{
  "name": "custom-hook-lib",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "module": "dist/index.js",
  "files": [
    "dist"
  ],
  "keywords": [],
  "license": "ISC",
  "peerDependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "@types/react": "^18.0.17",
    "typescript": "^4.8.2",
    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0",
    "ts-loader": "^9.3.1"
  }
}


about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Do you really need a webpack to bundle your library? As your app will bundle it anyway.

I saw some hook libs use just 'tsc' command as npm build. In that case you app will supply only source code without bundeled deps. Also there will not be react in inner node_modules in hook folder as your react in devDeps and therefore you hook will supply react of your app node_modules.

Now (according the error) it seems that react is still in output bundle of hook lib.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda