首页 > 解决方案 > 将 Javascript 转换为 Typescript。我构建的文件没有在我预期的地方结束

问题描述

我正在学习 Cypress 和 Typescript。我根据以下文章设置了我的文件:

将 JavaScript 项目转换为 Typescript,一次一个文件

webpack.config.js:

var path = require('path');
var webpack = require('webpack');


module.exports = {  
  mode: "development",
  entry: { 
    index: "./cypress/integration/alloy_test.ts"
  },
target: 'node',
  : {
    rules: [
      { test: /\.ts(x?)$/, loader: 'ts-loader' },      
      { test: /\.json$/, loader: 'json-loader' }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({'process.env.NODE_ENV': '"production"'})
    ],
  resolve: {
    extensions: ['.ts', '.js', '.json']
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, 'lib'), 
    filename: '[name].js'
  },
};

这里指定了目录 /lib,这是我希望所有生成的 Typescript 文件所在的位置。

tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "noImplicitAny": true,
        "lib": [
        "es5","es2015", "es6", "dom"
        ],
        "types": ["cypress"]
    }
}

包.json:

{
  "name": "playground",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "cypress:open": "cypress open",
    "build": "node_modules/.bin/webpack --config webpack.config.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/prompt-sync": "^4.1.0",
    "cypress": "^3.1.0",
    "json-loader": "^0.5.7",
    "ts-loader": "^5.2.2",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
  },
  "dependencies": {
    "ajv": "^6.0.0",
    "ajv-keywords": "^3.2.0",
    "typescript": "^3.1.3"
  }
}

我正在运行 npm run build 来进行转译。

最后,我的一个 .ts 文件 /cypress/integration/alloy-test.ts - 已被转译到同一目录:/cypress/integration/alloy-test.js。

我期待它去/lib。

标签: javascripttypescriptnpmcypress

解决方案


推荐阅读