Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Webpack】TypeScript 打包 #49

Open
Tracked by #6
swiftwind0405 opened this issue Apr 17, 2020 · 0 comments
Open
Tracked by #6

【Webpack】TypeScript 打包 #49

swiftwind0405 opened this issue Apr 17, 2020 · 0 comments
Labels

Comments

@swiftwind0405
Copy link
Owner

可参考https://www.webpackjs.com/guides/typescript/或https://webpack.js.org/guides/typescript/

  • 安装 ts 依赖
npm install --save-dev typescript ts-loader
  • 增加 tsconfig.json 配置文件

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "jsx": "react",
    "allowJs": true
  }
}
  • webpack.config.js 添加对 ts/tsx 语法支持(ts-loader)

webpack.config.js

const path = require("path");

module.exports = {
  entry: "./src/index.ts",
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"]
  },
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist")
  }
};

当从 npm 安装第三方库时,一定要牢记同时安装这个库的类型声明文件。可以从 TypeSearch 中找到并安装这些第三方库的类型声明文件。如:

npm install --save-dev @types/lodash
@swiftwind0405 swiftwind0405 changed the title 【Webpack】【进阶】TypeScript 打包 【Webpack】TypeScript 打包 Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant