src/index.ts
export class Simple{
public static print(){
console.log( "print" )
}
}
tsconfig-base.json
{
"exclude": ["./test", "./tap-snapshots"],
"include": ["src/**/*.ts"],
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"inlineSources": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es2022"
}
}
tsconfig.json
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs",
"moduleResolution": "Node"
}
}
tsconfig-esm.json
{
"extends": "./tsconfig-base.json",
"exclude": ["./test", "./tap-snapshots", "src/bin.ts"],
"compilerOptions": {
"module": "esnext",
"outDir": "dist/mjs"
}
}
webpack.cjm.js
{
"loader":'ts-loader',
options: {
configFile: path.resolve(__dirname, './tsconfig.json')
}
}
webpack.mjs.js
{
"loader":'ts-loader',
options: {
configFile: path.resolve(__dirname, './tsconfig-esm.json')
}
then run to compile:
webpack -c webpack.cjm.js
webpack -c webpack.mjm.js
package.json:
"name": "@fakename/webpack_lib",
"main": "./dist/cjs/index.js",
"types": "./dist/mjs/index.d.ts",
"module": "./dist/mjs/index.js",
then pack them to npm
then create a new project
package.json:
"dependencies": {
"@fakename/webpack_lib": "0.0.1"
}
src/index.ts
import * as lib from "@gogoqiu/webpack_lib"
console.log( lib )
lib.Simple.print()
compile and run:
report:
/tmp/webpack/webpack-import/dist/cjs/index.js:27
lib.Simple.print();
^
TypeError: Cannot read properties of undefined (reading ‘print’)
If using tsc is ok, lib can be imported in subproject and run corrected, but use webpack is not ok?
who can give me a total example is ok
Han Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.