javascript - 如何使用 npm 脚本将 typescript 编译成 javascrip

My current entire package.json file.

"scripts": {
    "build": "webpack",
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\"",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings",
    "build:css": "node-sass --output-style compressed --include-path scss scss/lifeleveler.scss app/assets/css/app.css",
    "build:css-expand": "node-sass --include-path scss scss/lifeleveler.scss app/assets/css/app.css",
    "watch:css": "nodemon -e scss -x \"npm run build:css\"",
    "watch:css:expand": "nodemon -e scss -x \"npm run build:css-expand\"",
    "build:js": "browserify dist/main.js > dist/lifeleveler.app.js",
    "watch": "npm run watch:css & npm run watch:js",
    "watch:js": "onchange '/dist/**/*.js' -p -- npm run build:js"
},

我正在尝试使用 1 个单独的 watch 脚本来运行 lite 服务器、构建 .ts .js 和 .css 文件。

现在,当我编辑或制作任何新的 typescript 文件时,我有 systemJStsc 生成 .js 文件。

tsc:w 生成这些 javascript 文件后,它们最终会在 dist 文件夹中。一旦它们在那里更新,我想根据 dist 内部的 main.js 将它们 uglify 并连接到 1 个 lifeleveler.app.js 文件中。

到目前为止,我无法正确组合上面的任务..

我的文件夹目录


我也认为我的方法是错误的,我可以有 2 个终端窗口。 1 看 TSX -> js,另一个看 SASS-> CSS。

最后,当我准备好推送构建时,也许是我的 build:js 任务。

但是我需要替换 index.html 文件中的 block ,你会怎么做?

<html>
<head>
    <meta charset="UTF-8">
    <title>LifeLeveler</title>
    <meta name="description" content="Level up in the journey of life.">
    <link rel="stylesheet" href="app/assets/css/app.css">
    <!-- load the dependecies -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <!-- load our angular app with systemjs -->
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="systemjs.config.js"></script>
    <script>
        System.import('app').catch(function(err) { console.log(err); });
    </script>
    <!-- <script src="dist/lifeleveler.app.js"></script> -->
</head>
<body class="container">

    <my-app></my-app>

</body>
</html>

tsconfig

"compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "rootDir": "app/",
    "outFile": "./dist/main.js",
    "outDir": "dist"
},
"sourceMap": true

最佳答案

你可以修改你的 tsconfig.json :

{
    "compilerOptions": {
        "target": "ES5",
        "rootDir": "app/",
        "outFile": "./dist/main.js"
    }
}

先用tsc:w编译成单个文件,用build:js压缩成dist/lifeleveler.app.js

编辑:请注意outFile不能和"module": "commonjs"一起使用,see this question

关于javascript - 如何使用 npm 脚本将 typescript 编译成 javascript,然后编译成 1 个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42181169/

相关文章:

javascript - 减少 React/MERN Stack Bundle 的大小 - 主要是删

xcode - 如何将构建阶段复制到 Xcode 中的另一个目标?

java - 切换到 Java 7 时构建时间显着增加

build - 为什么 Gradle 不在编译/运行时类路径中包含传递依赖项?

deployment - 什么/为什么 WinRT 随机创建新的干净布局

linux - 如何使用静态链接构建 git?

build - 在另一个项目中调用 gradle "build"任务

build - GulpJS : How to rev images and then update

git - 您是否应该将已编译的 Assets checkin Git?

java - 在 Play 上使用 Clover 执行代码覆盖!使用 Ant 的框架应用程序