【发布时间】:2020-11-23 12:28:08
【问题描述】:
我的代码是这样的:
Foo.ts:
export module Foo {
export function load() { }
}
Bar.ts:
export module Bar {
export function load() { }
}
webpack.config.js:
const path = require('path');
module.exports = {
entry: {
"FooBar": ['./Foo.ts', './Bar.ts']
},
mode: 'production',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
devtool: 'source-map',
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
library: ['[name]'],
libraryTarget: "window"
}
};
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true
},
"exclude": [
"node_modules"
]
}
如果我切换 FooBar 入口点的两条路径,则只有“Foo”可见。如果我将 Foo 和 Bar 两个模块都放入 Bar.ts,同时将 ./Bar.ts 作为入口点的最后一个路径,那么这两个模块在窗口对象上都变得可见。 这里发生了一些意外的覆盖或“最后一个获胜”的行为。 如何在不将它们放入同一个 .ts 文件的情况下实现两个模块在窗口对象上可见?
我最初将 glob 包含在我的 webpack.config.js 中,以使其尽可能动态:
var glob = require('glob');
...
module.exports = {
entry: {
"FooBar": glob.sync('./*.ts*')
...
我希望将我所有的 .ts 文件捆绑到一个 .js 输出文件中,当然也包括我的所有模块。
【问题讨论】:
标签: node.js typescript webpack bundle