javascript - Webpack resolve.alias does not work with typescript?
我想大声告诉你
我想大声告诉你 2017-07-05 10:36:58
0
2
1136

environment:

- node v6.9.2
- typescript v2.4.1
- webpack v1.15.0
- awesome-typescript-loader v3.2.1

question:

Report an error in tsx Module not found 'tools'...

import { toFieldValue } from '@utils/tools';

  • Add resolve.alias in webpack configuration

resolve : {
    alias: { 
        // 这里配置没有问题, jsx中可以正常使用
         "@utils": "./src/utils"
    }
}
  • Referenced in jsx (can be used)

import { toFieldValue } from '@utils/tools'

tsx configuration

{
  "compilerOptions": {
    "outDir": "./dist/",
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "jsx": "react",
    "pretty": true,
    "noImplicitAny": true,
    "traceResolution": true,
    "baseUrl": ".",
    "paths": {
      "@utils/*": ["./src/utils/*"]
    },
    "lib": [
      "dom",
      "es2015.promise",
      "es5",
      "es2015.iterable",
      "es2015.generator",
      "es2015.symbol",
      "es7"]
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ],
  "awesomeTypescriptLoaderOptions": {
    "useBabel": true,
    "useCache": true
  }
}
  • Webpack adds configuration (webpackConfig is the configuration of webpack)

      webpackConfig.resolve.plugins = [(new TsConfigPathsPlugin({
        configFileName: "tsconfig.json",
        compiler: "typescript",
      }))];
    
我想大声告诉你
我想大声告诉你

reply all(2)
習慣沉默

It should be possible to use alias. The author can refer to my react-typescript template project, https://github.com/devlee/tpl...

This doesn’t work for the poster. The reason is that the paths in tsconfig should be paths, not source files directly

过去多啦不再A梦

Solved~
tool.js must declare a corresponding tool.d.ts file.
Drunk, drunk~~~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template