Importing a custom library into a storybook - node.js

I am writing a TypeScript library. I have gotten to the point where I would like to test said library. Because it's a very visual thing, I chose to go with storybook so I can show off the different functionalities of my library.
My package has an index.ts with the following in it:
export { Container } from "./Container";
My folder structure looks like this:
library/
dist/
src/
index.ts
Container.ts
package.json
storybook/
stories/
package.json
This is the package.json of my library:
{
"name": "#wesp/customcontainer",
"main": "dist/index.js",
"files": [
"dist"
],
}
This is the dependencies for the package.json of the storybook folder:
"dependencies": {
"#wesptest/customcontainer": "file: ../",
},
Now when I try to use the custom library in for example storybook/stories/test.stories.ts:
import {Container} from "#wesp/customcontainer";
but then the story will throw this error:
_wesp_customcontainer__WEBPACK_IMPORTED_MODULE_1__.Container is undefined
What do I have to change so I can successfully import this class?
thanks.
-- edit --
My tsconfig.json:
{
"compilerOptions": {
"target": "es2019",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"jsx": "react",
"esModuleInterop": true,
"outDir": "./dist",
"declaration": true,
},
"include": ["./src"]
}

main of your lib is dist/index.js.
Storybook file will try to import from dist dir, so first check if js file exists in that dir.

Related

Package.json dependencies and reducing time for step Generating es5 bundles for differential loading

I need some clarification here: If your application's package.json has dependencyA and another commonDependency, and commonDependency's package.json already has dependencyA, does your application need dependencyA in its package.json? My guess is no, but I need to confirm.
I am trying to reduce the time (currently ~60 mins on our Jenkins server) it takes for this step: Generating es5 bundles for differential loading...
I believe by removing some of these "peer/nested" dependencies from our application, the build times will improve.
Application Package JSON
"dependencies": {
"#dependencyA": "1.2.3", <-- is this required here?
"#commonDependency": "2.3.4"
}
CommonDependency Package JSON
"dependencies": {
"#dependencyA": "1.2.3",
"#dependencyB": "4.5.6",
"#dependencyC": "7.8.9",
}
Application TSConfig JSON
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"paths": {
"#angular/*": [
"./node_modules/#angular/*"
]
},
"module": "es2020",
"moduleResolution": "node",
"importHelpers": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"target": "es2015",
"preserveSymlinks": true,
"downlevelIteration": true,
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2018",
"dom",
"esnext.asynciterable"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
UPDATE:
My issue now is this:
In the Application, there are .ts files that have
import ... from #dependencyA.
So when I remove the dependencyA from Application package.json, the ng build fails.
It will not resolve
import ... from #commonDependency, which has dependencyA in its package.json. Any suggestions?

Why isn't VS Code respecting TypeScript paths?

In my TypeScript project that's running tests via ts-node and jasmine I have defined some paths in tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"module": "commonjs",
"lib": [
"es5",
"es2015",
"DOM"
],
"moduleResolution": "Node",
"outDir": "./dist/cjs",
"declaration": true,
"sourceMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"removeComments": true,
"strict": true,
"paths": {
"#common": ["common/index"],
"#crashes": ["crashes/index"]
}
},
"files": [
"./src/index.ts"
]
}
I am able to import via paths #common and #crashes in my production code, but for some reason VS Code gives me the error Cannot find module '#common' or its corresponding type declarations. in my spec.ts files.
How can I get VS Code to recognize TypeScript paths in my tests?
This is because VS Code's intellisense will follow the rules in tsconfig.json. In this instance, I was not including the spec.ts files in the TypeScript compilation. Additionally, I did not want to include the TypeScript files in my project's build output.
The solution was as follows:
Remove the files property from tsconfig.json
Introduce a new tsconfig.dist.json that extends tsconfig.json and add the files property here
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist/cjs",
},
"files": [
"./src/index.ts"
]
}
Use the new tsconfig.dist.json in the publish script
Additionally, once I did all this, I got the following error when I tried to run jasmine tests with ts-node:
> ts-node node_modules/jasmine/bin/jasmine --config=spec/support/jasmine.spec.json
Error: Cannot find module '#common'
The solution to this error was to install tsconfig-paths and update my test script in package.json
{
"scripts": {
"test": "ts-node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine --config=spec/support/jasmine.spec.json"
}
}

Executing typescript code using ts-node gives "Cannot use import statement outside a module" error

I am trying to run apollo-server and the code I have written is in TypeScript.
My code folder contains a tsconfig.json which looks like this:
{
"compilerOptions": {
"target": "esnext",
"lib": [
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": [
"server"
]
}
The command I am running is below:
$ npx ts-node ./server/index.ts
If I remove the tsconfig.json (which of course I can't do), the command above works fine. I am not sure which configuration is actually causing the problem.
the setting you are searching is "module": "commonjs". As ts-node is executing your code in the nodejs environment you have to use its module system. If you need your config as default for your project you can create a second tsconfig tsconfig.node.json and target it with ts-node --project <tsconfig.json>
tsconfig.node.json:
{
"extends": "./",
"compilerOptions": {
"module": "commonjs",
},
}

Node Error: Cannot find module - typescript api for custom module where I export mongoose schemas and interfaces

I have a typescript module #tlabs/models where I'm simply exporting in index.ts:
export * from '../models......'
where in each file I have something like:
export const Project = typedModel('projects', ProjectSchema);
and my only dependency is ts-mongoose imported in each file simply as:
import { createSchema, Type, typedModel, ExtractProps } from 'ts-mongoose';
ts-mongoose being a dependency that itself required mongoose + mongoose types.
In my typescript node project I have ts-mongoose, mongoose and #tlabs/models as dependencies and #types/mongoose as dev dependency.
Running tsc is fine, the files get compiled and there's no error being thrown out but then trying to run the actual files throws out:
Error: Cannot find module '#tlabs/models'
I have reinstalled all modules several times and checked package.json as well as the actual files on the disk + through vscode and they're right there.
What am I missing out?
My tsconfig is:
{
"include": ["src/**/*"],
"exclude": ["node_modules", "./node_modules", "./node_modules/*"],
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"allowJs": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"noImplicitAny": false,
"alwaysStrict": true,
"strictNullChecks": true,
"types": [],
"lib": [],
"experimentalDecorators": true
}
}
My final TS config for my exported models:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "lib",
"strict": true,
"esModuleInterop": true
},
"include": ["src"]
}
and relevant package.json configuration:
{
"main": "lib/index.js",
"types": "lib",
"scripts": {
"tsc": "tsc",
"build": "tsc -p ."
},
"files": [
"lib"
],
}

Rollup does not bundle declaration file

I am trying to build a library made with TypeScript. I am using the paths property in tsconfig.json.
My tsconfig.json looks like this:
{
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"target": "es2015",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"experimentalDecorators": true,
"sourceMap": true,
"lib": [
"esnext",
"dom"
],
"strict": true,
"sourceRoot": "./src",
"rootDir": "./src",
"outDir": "build",
"declaration": true,
"declarationDir": "build",
"baseUrl": "./src",
"noUnusedLocals": true,
"paths": {
"modules/*": [
"modules/*"
],
"common/*": [
"common/*"
]
}
},
"exclude": [
"node_modules",
"build"
]
}
And my rollup config looks like this:
import typescript from 'rollup-plugin-typescript2'
export default {
entry: "./src/index.ts",
output: {
file: './build/index.js',
format: 'cjs'
},
plugins: [
typescript({ typescript: require("typescript"), declaration: true, clean: true })
]
}
However, when I build using rollup -c, it creates this in my build folder:
Where the index.js is bundled just fine, but the declaration file here simply imports from the /common folder and that folder basically contains all the other folders from the src directory but only with a single declaration file per folder, furthermore, these files try to import using the paths aliases instead of being built with relative imports, so they do not work.
How do I tell rollup to build a single declaration file instead of this?
rollup-plugin-typescript keeps the folder structure for declaration files. To bundle the declaration too, you should use rollup-plugin-ts as well:
https://github.com/wessberg/rollup-plugin-ts

Resources