6

Context : I am building a small library (let's call it myLibrary here) using TypeScript and Webpack. I built it, imported it in a react application but the react application crash.

Library side

My main entry point (index.ts) has a default export as such :

import wrapper from "./wrapper";

export default wrapper;

And my wrapper file exposes a default export which is a function (wrapper.ts) :

const create = () => {
  // Some functions
  return {
    init,
    close,
    getBase,
    setBase
  }
}
export default create;

The library pass all the unit tests easily.

React application side

After building and when importing my library in a React application, I have no Typescript error but my React app crashes with the following message :

TypeError: myLibrary__WEBPACK_IMPORTED_MODULE_13___default(...) is not a function

After calling my library like that :

import createAPI from "myLibrary";
const api = createAPI(); // Crash here even though I should have a nice object containing my functions

It's really strange as TS really compiled nicely to JS without any warnings.

My library wepback config (4.43.0) which I use to build with command webpack --config webpack.config.js:

const path = require('path');

module.exports = {
  mode: "production",
  entry: "./src/index.ts",
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: [".ts", ".js"]
  },
  module: {
    rules: [
      { test: /\.tsx?$/, loader: "ts-loader" }
    ]
  }
}

My library TS config (3.7.3) :

{
  "compilerOptions": {
    "outDir": "dist",
    "target": "es5",
    "module": "CommonJS",
    "lib": ["dom", "dom.iterable", "esnext"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "preserve",
    "declaration": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true
  },
  "include": ["src"]
}

Any help would be greatly appreciated :)

EDIT : After updating default export to named export :

import { createAPI } from "myLibrary";
const api = createAPI();

I got a new error

TypeError: Object(...) is not a function

And when I try to console.log(typeof createAPI); I got an undefined, which should not be possible as my tests are passing and TS doesn't complain.

Hugo Laplace
  • 609
  • 1
  • 7
  • 16
  • What is your `myLibrary` anyway? Does it export a default function as well? – tmhao2005 Jul 28 '20 at 15:41
  • `myLibrary` is the library I built with webpack :) – Hugo Laplace Jul 28 '20 at 15:49
  • Instead of default exports, what happens if you use named exports instead? IMO default exports suck anyway. See https://basarat.gitbook.io/typescript/main-1/defaultisbad – spender Jul 28 '20 at 15:55
  • I'm quite unsure to get your context correctly. Did you mean you built a simple library which export the wrapper as default then use it in a different project? The issue is happening in the current that project. Is that correct? And what project do above webpack.config & tsconfig belong to anyway? – tmhao2005 Jul 28 '20 at 15:58
  • I just updated my question :) – Hugo Laplace Jul 28 '20 at 16:20
  • If you can provide the build file of your library, it would be great since I'm keen to check which module that is built – tmhao2005 Jul 28 '20 at 16:27
  • You can get the module at https://www.npmjs.com/package/@upandgo/scorm-wrapper – Hugo Laplace Jul 28 '20 at 16:37
  • I've just tried for you but it seems you didn't export anything at all so you might have to check the way you export things & how to build your library such as which module type do you target to (cjs/esm/umd/...)? since webpack just supports a number of module types. – tmhao2005 Jul 28 '20 at 16:45
  • By the way your code has been built with production mode so I couldn't help you to find anything helpful though – tmhao2005 Jul 28 '20 at 16:46
  • If I didn't export anything my tests (which use my library main entry point) should not run AND Typescript should actually let me know when I import my library on another project that I have an undefined whereas the autocomplete and type inference work great :/ I'll investigate further as it was working when I didn't use Webpack – Hugo Laplace Jul 28 '20 at 16:51
  • Hold on. I found something interesting. It looks like you bundle your library with webpack which means you might have to set some library props as following snippet. I'll comment them as answer. – tmhao2005 Jul 28 '20 at 17:20

2 Answers2

2

In your webpack config of the library to point out library name & its module type:

output: {
  path: './dist',
  filename: 'index.js',
  library: 'scorm-wrapper',
  libraryTarget: 'umd'
},
tmhao2005
  • 14,776
  • 2
  • 37
  • 44
0

If you updated webpack to version 5.x.x, then the issue might be related to output.library setting issue

so if you have library setting in webpack output try to remove it:

output: {
  path: path.resolve(__dirname, 'build'),
  filename: 'painterro.commonjs2.js',
  library: 'Painterro',  // ⏪ this is the issue
  libraryTarget: 'commonjs2',
 },

Read more in issue Fix _webpack_imported_module is not a function for webpack5

Ivan Borshchov
  • 3,036
  • 5
  • 40
  • 62