如何将一些文件导入到 webpack 的入口文件中?

How to import some file into entry file for webpack?

我使用 Node.js API 作为 webpack.config.ts:

import { resolve } from 'path';
import { Configuration } from 'webpack';

export const config: Configuration = {
  // ...
  context: resolve(__dirname),
  entry: {
    swaggerUi: './index.ts'
  },
  // ...
};

当我尝试将任何文件导入入口文件时出现问题 - index.ts:

import SwaggerUI from 'swagger-ui';
import 'swagger-ui/dist/swagger-ui.css';
import { urlConfig } from './swagger.config';

const ui = SwaggerUI({
  url: urlConfig.url,
  dom_id: '#swagger',
});

ui.initOAuth({
  appName: 'Swagger UI Webpack Demo',
  clientId: 'implicit'
});

这会引发错误:

"Module not found: Error: Can't resolve './swagger.config' in '/srv/git/ditsmod/ditsmod/packages/openapi/src/swagger-ui'"

swagger.config.ts 文件实际上与 index.ts.

存在于同一目录中

我做错了什么以及如何将一些文件导入到 webpack 的入口文件中?

您似乎缺少 resolve webpack 选项。您需要指定它,以便 webpack 知道您要解析 .ts 个文件:

  resolve: {
    extensions: [".ts", ".js", ".tsx"], // remove .tsx if you don't need the jsx syntax
  },