【问题标题】:Server & client-side TypeScript project organization, compilation服务器和客户端 TypeScript 项目组织、编译
【发布时间】:2017-04-23 12:22:35
【问题描述】:

如何编译我的 TypeScript 项目

  • 在服务器和客户端之间共享代码
  • 使用 TypeScript

我无法让 webpack 工作,该网站只显示了一个非常基本的入门。我试过 gulp,但它太复杂了,增量编译需要很长时间(远远超过它应该需要的时间)。

src/
    common/
        data.ts
        [other files that are needed on both client and server-side]
    server/
        server.ts
        search.ts
    client/
        sw.ts
        resources/
             [other static resources like index.html]
[configuration files like package.json]

我该怎么做?我应该使用什么?

编辑:

在 gulp 中,我使用了 gulp-typescript 和 tsify,但是增量编译需要 5 秒以上,这实在是太多了。

【问题讨论】:

  • 请您发布您的 tsconfig 吗?
  • 你有什么理由不使用tsc
  • 因为客户端需要类似tsify的东西

标签: typescript webpack


【解决方案1】:

我使用 npm 脚本启动 3 个“监视”进程:

  • 监视客户端文件 (webpack) 并编译到名为 public 的文件夹中。

  • 监视服务器文件 (tsc) 并编译到名为 private 的文件夹中

  • 监视输出服务器代码 (nodemon) 并在其更改时运行 node.js 应用程序。

两个应用程序都可以从common 导入文件,并且应该可以正常工作。

package.json 中的 scripts 配置如下所示:

  "scripts": {
    "watch-all": "npm run watch-server & npm run watch-client & nodemon --inspect private/server.js",
    "watch-server": "tsc -p tsconfig.json -inlineSourceMap -outDir private --watch",
    "watch-client": "webpack --watch",
  }

我们在“客户端”中有多个独立应用程序,每个应用程序都使用一个文件夹。所以我们使用 webpack 为每个应用创建一个 bundle。

我的 webpack 配置如下(注意:此配置中可能不需要一些插件):

const { CheckerPlugin, TsConfigPathsPlugin } = require("awesome-typescript-loader");
const Visualizer = require("webpack-visualizer-plugin");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const TypedocWebpackPlugin = require("typedoc-webpack-plugin");

const corePlugins = [
    new CheckerPlugin(),
    new webpack.DefinePlugin({
        "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development")
    }),
    new Visualizer({
        filename: "./debug/statistics.html"
    }),
    new CopyWebpackPlugin([
        // ...
    ])
];

const devPlugins = [
    new TypedocWebpackPlugin(
        {
            name: "ORG",
            out: "../private/docs",
            mode: "file",
            tsconfig: "./tsconfig.json"
        },
        "./src"
    )
];

const prodPlugins = [
    new webpack.optimize.UglifyJsPlugin()
];

const plugins = process.env.NODE_ENV === "production" ? corePlugins.concat(prodPlugins) : corePlugins.concat(devPlugins)

module.exports = {
    entry: {
        "app1/": "./src/client/app1/index.ts",
        "app2/": "./src/client/app2/index.ts",
        // ...
    },
    devServer: {
        inline: true
    },
    output: {
        filename: "[name]bundle.js",
        path: __dirname + "/public",
        publicPath: "/public"
    },
    devtool: "source-map",
    resolve: {
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
        plugins: [
            new TsConfigPathsPlugin({ configFileName: "tsconfig.json" })
        ]
    },
    module: {
        rules: [
            {
                enforce: "pre",
                test: /\.js$/,
                loader: "source-map-loader",
                exclude: [ /node_modules/, /experimental/ ]
            },
            {
                enforce: "pre",
                test: /\.(ts|tsx)$/,
                loader: "tslint-loader",
                exclude: [ /node_modules/, /experimental/ ]
            },
            {
                test: /\.(ts|tsx)$/,
                loader: "awesome-typescript-loader",
                exclude: [ /node_modules/, /experimental/ ]
            },
            {
                test: /\.scss$/,
                use: [{
                    loader: "style-loader",
                    options: {
                        sourceMap: true
                    }
                }, {
                    loader: "css-loader",
                    options: {
                        sourceMap: true
                    }
                }, {
                    loader: "sass-loader",
                    options: {
                        sourceMap: true
                    }
                }]
            }
        ]
    },
    plugins: plugins
};

我正在使用以下版本:

  "devDependencies": {
    "awesome-typescript-loader": "^3.0.0-beta.18",
    "chai": "^3.5.0",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "html5-history-api": "^4.2.7",
    "mocha": "^3.2.0",
    "node-sass": "^4.5.2",
    "nodemon": "^1.11.0",
    "nyc": "^10.1.2",
    "phantomjs-prebuilt": "^2.1.14",
    "sass-loader": "^6.0.3",
    "sequelize-auto": "^0.4.25",
    "sinon": "^2.0.0-pre.5",
    "source-map-loader": "^0.2.1",
    "sourcemap-istanbul-instrumenter-loader": "^0.2.0",
    "style-loader": "^0.16.1",
    "supertest": "^3.0.0",
    "tmp": "0.0.31",
    "ts-node": "^3.0.2",
    "tslib": "^1.6.0",
    "tslint": "^5.1.0",
    "tslint-loader": "^3.5.2",
    "typedoc": "^0.5.10",
    "typedoc-webpack-plugin": "^1.1.4",
    "typescript": "^2.2.2",
    "webpack": "^2.3.3",
    "webpack-dev-server": "^2.4.2",
    "webpack-visualizer-plugin": "^0.1.10",
    "xlsx": "^0.9.10",
    "yargs": "^7.0.2"
  }

更新 1(添加 tsconfig.json)

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6", "dom", "dom.iterable"],
        "downlevelIteration" : true,
        "module": "commonjs",
        "moduleResolution": "node",
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noImplicitAny": true,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "strictNullChecks": true,
        "noImplicitReturns": false,
        "noImplicitThis": true,
        "baseUrl": "src",
    },
    "exclude": [
        "./node_modules"
    ]
}

【讨论】:

  • 这太棒了。您能否也分享您的 tsconfig.json 文件?
  • 我也添加了 tsconfig.json 文件
【解决方案2】:

你试过在你的 tsconfig 中设置isolatedModules : true 吗?在这里阅读更多:http://weblogs.thinktecture.com/thomas/2016/05/tired-of-waiting-for-typescript-compilation.html

【讨论】:

  • 这样快多了,但还是2秒。也许我的 gulp 文件有问题……或者那是个好时机?当我去浏览器,刷新它并看到它没有改变时,这很烦人:/
  • 在 ~5k LOC 项目中对我来说也是 2.5 秒左右
  • 浏览器刷新:你可以考虑使用node-livereload作为你的webserver,这样当你的目标js文件改变时你的浏览器会自动重新加载
猜你喜欢
  • 1970-01-01
  • 2016-01-27
  • 2011-09-11
  • 2014-01-19
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
相关资源
最近更新 更多