Typescript 模块导入返回空对象
我尝试使用Webpack和TypeScript创建npm模块。在构建我的包并尝试导入它之后,我有一个空对象,而不是默认的导出函数 您可以看到我的配置: Webpack.js:Typescript 模块导入返回空对象,typescript,npm,webpack,Typescript,Npm,Webpack,我尝试使用Webpack和TypeScript创建npm模块。在构建我的包并尝试导入它之后,我有一个空对象,而不是默认的导出函数 您可以看到我的配置: Webpack.js: const webpack = require('webpack'); const path = require('path'); const config = { entry: './src/index.ts', output: { path: path.resolve(__dirname, 'dist
const webpack = require('webpack');
const path = require('path');
const config = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.ts(x)?$/,
use: ['awesome-typescript-loader'],
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
'postcss-loader'
]
},
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
}
};
module.exports = config;
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"allowJs": true
},
"include": ["./src/**/*"]
}
我的模块是build,但默认导入返回空对象。我已尝试使用示例函数:
const test = () => console.log('test');
export default test;
从“我的模块名”导入测试;test()//test不是函数
有人已经使用webpack和typescript创建了npm模块吗
谢谢社区 基于,您必须将libraryTarget:“commonjs2”
添加到您的网页配置中
下面是一个例子:
module.exports={
...
输出:{
path:path.resolve(uu dirname,'dist'),
文件名:'[name].js',
libraryTarget:'commonjs2'//添加此行
},
...
}
我以前实现过这个解决方案,它也可以在TypeScript+Webpack
上运行,但有类似的问题