【问题标题】:How to use tsify with watchify on Ubuntu?如何在 Ubuntu 上使用 tsify 和 watchify?
【发布时间】:2020-04-03 16:44:45
【问题描述】:

输入目录包含:

  1. 一个JavaScript文件(一个不在DefinitelyTyped repo中的jQuery插件)和
  2. 2 个打字稿文件
    • declarations.d.ts
    • main.ts

tsconfig.json 文件是这样的(正在进行中):

{
    "compilerOptions": {
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "outDir": "wp-content/themes/custom-theme/assets/js",
        "watch": true,
        "allowJs": true,
        "lib": ["ES2016", "DOM"]
    },
    "include": [
        "wp-content/themes/custom-theme/assets/ts"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

目前我有这个运行良好的 watch.sh 脚本:

tmux \
    new-session  'cd html && tsc' \; \
    split-window 'cd html/wp-content/themes && scss --watch custom-theme/assets/scss:custom-theme/assets/css' \; \
    split-window 'cd html/wp-content/themes && watchify custom-theme/assets/js/main.js -o custom-theme/assets/js/bundle.js'

我想将此脚本替换为 Browserify build.js 文件(如果可能,我更喜欢 build.ts),并且我想将 Tsify 与 Watchify 一起使用(我知道 Watchify build.js 文件类似于 Browserify文件)。

我见过this example,但我不确定我是否走上了好路。

我有这个不起作用的 build.js 文件:

const browserify = require("browserify");
const tsify = require("tsify");

browserify()
    .plugin(tsify, { allowsJs: true })
    .add("wp-content/themes/custom-theme/assets/ts/main.ts")
    .bundle()
    .on('error', function (error) { console.error(error.toString()) })
    .pipe(process.stdout);

它甚至没有开始运行:它说在( 附近的第 1 行有语法错误。

非常感谢任何建议。

谢谢。

更新 1

新的 build.js 文件:

const watchify = require("watchify");
const tsify = require("tsify");

watchify()
    .plugin(tsify, { allowsJs: true })
    .add("wp-content/themes/custom-theme/assets/ts/main.ts")
    .bundle()
    .on('error', function (error) { console.error(error.toString()) })
    .pipe(process.stdout);

运行但抛出这个:

$ node build.js 
/.../node_modules/watchify/index.js:14
    var cache = b._options.cache;
                  ^

TypeError: Cannot read property '_options' of undefined
    at watchify (/.../node_modules/watchify/index.js:14:19)
    at Object.<anonymous> (/.../build.js:4:1)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

更新 2

我最终使用了这个watch.sh shell 脚本文件:

tmux \
    new-session  'cd html && tsc' \; \
    split-window 'cd html/wp-content/themes; scss --watch custom-theme/assets/scss:custom-theme/assets/css' \; \
    split-window 'cd html/wp-content/themes; watchify custom-theme/assets/ts/main.ts -p [ tsify ] -o custom-theme/assets/js/bundle.js -v'

来自here 我了解它尊重tsconfig.json 文件。唯一的问题是 main.ts 中的 require 调用没有返回 VS Code 可以很好理解的内容,所以我没有自动完成支持。这是我仍然需要帮助的地方。将来我还想使用build.js 脚本,如果有人可以帮助我的话。

【问题讨论】:

    标签: javascript node.js typescript browserify


    【解决方案1】:

    现在我使用 ES6 语法来导入模块,无论我在哪里导入一些东西。当我从 npm 包导入时,我还使用 node_modules 目录中相关 npm 包中文件的相对路径。

    tsconfig.json 中,除了其他之外,我还有以下几行:

    "target": "ES3",
    "lib": ["ES2020", "DOM"],
    "module": "CommonJS"
    

    最终的工作测试项目是here

    我仍然有一些没有为 ES6 导入准备好的模块的问题。

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 1970-01-01
      • 2015-04-20
      • 2013-05-12
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2015-11-19
      相关资源
      最近更新 更多