Typescript模块以字符串形式而不是模块导入导入路径

时间:2018-10-22 15:15:15

标签: javascript typescript webpack import module

试图从打字稿(.ts)文件中导入方法,而不是通过可访问的方法,而是通过webpack中的ts-loader获取到虚拟文件的路径(使用Error: package or namespace load failed for 'stats' in inDL(x, as.logical(local), as.logical(now), ...): unable to load shared object 'C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll': LoadLibrary failure: The specified module could not be found. During startup - Warning message: package 'stats' in options("defaultPackages") was not found Error: '\P' is an unrecognized escape in character string starting ""C:\P" )。

已使用webpack-dev-server引导了应用程序(从一开始就没有使用Typescript),现在正尝试引入TS。

constants / dates.ts:

react-create-app

somefile.js

export const MINUTE_IN_MILLIS = 60 * 1000;
export const HOUR_IN_MILLIS = 60 * 60 * 1000;
export const DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;
export const WEEK_IN_MILLIS = 7 * DAY_IN_MILLIS;
export const NUMBER_OF_QUARTERS = 24 * 4;
export const WEEK_DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
export const US_TIMEZONES = [
  "America/Adak",
  "America/Anchorage",
  "America/Atka",
  "America/Boise",
   ...
];

在console.log以上输出:

import * as dates from "constants/dates"; export const substractUTCOffset = (date: Date) => new Date() export const addUTCOffset = (date: Date) => new Date() export const isUSTimeZone = (timezone: string) => { =======> console.log(dates); <======= return dates.US_TIMEZONES.indexOf(timezone) !== -1; };

tsconfig.json

“/static/media/dates.92294b02.ts"

webpack.dev.js

相对于主题的配置部分:

{ "compilerOptions": { "baseUrl": "./react/src", // "outDir": "./build", "module": "esnext", "target": "es5", "lib": ["es5", "es6", "dom"], "sourceMap": true, "allowJs": true, "jsx": "react", "moduleResolution": "node", // "rootDir": "./", "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, "noImplicitThis": true, "noImplicitAny": true, "importHelpers": true, "strictNullChecks": true, "suppressImplicitAnyIndexErrors": true, "noUnusedLocals": true, "allowSyntheticDefaultImports": true }, "exclude": [ "node_modules", "build", "scripts", "acceptance-tests", "webpack", "jest", "src/setupTests.ts" ] }

extensions: [".ts", ".tsx", ".web.js", ".js", ".json", ".web.jsx", ".jsx"]

1 个答案:

答案 0 :(得分:0)

找到了解决方案,这要感谢@MattMcCutchen。

ts-loader应该已经在我的webpack配置的oneOf部分中。 我还从file-loader中明确排除了“ .tsx?$”。

那完成了工作:)

可能的常见问题之一:

  • “为什么我会得到字符串路径而不是模块”

  • “因为其他一些加载器正在打包要使用的加载器之前要打包的文件”

感谢您的建议!