0

我想在服务器端运行一些用 ES2020 编写的 TypeScript 代码。目前我正在运行一个 ASP.NET Core 应用程序,所以我的想法是通过 Jering.Javascript.NodeJS 和 nodejs 运行 js。

所以从一个基本的例子开始。

var test = await StaticNodeJSService.InvokeFromFileAsync<string>("Scripts/server/modules/hello.ts", args: new object[] {  });

你好.ts

module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = "test"; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}

工作...添加了一些进口声明...

你好.ts

import someFunction from "./some.func";

module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = "test"; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}

--> "不能在模块外使用 import 语句"

Nodejs 版本 v16.13.2 和 package.json 中已经设置

 "type": "module"

更改为要求

你好.ts

const someFunction = require("./some.func")

module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = "test"; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}

导致“InvocationException:ES Module 的 require() ... from ... 不支持。而是将 some.func.js in ... 的 require 更改为在所有 CommonJS 模块中都可用的动态 import()”

这样的无限循环。

通过看这里似乎是可能的,但我如何让它运行?

4

1 回答 1

1

找到了解决方案

  1. 我们有一些 ES6+ 模块,它本身可能有其他导入语句。

     let nodeStart = () => {
         return "ES6+ module";
     }
    
     export default nodeStart;
    
  2. 使用 webpack 和 babel 将 ES6+ 转换为 commonjs

webpack.config.js

const path = require('path');
    const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

    module.exports = (env, options) => {
        console.log(`Webpack 'mode': ${options.mode}`);
        var isDev = options.mode === 'development';
        return {      
            entry: {            
                'some-module-node': "./Scripts/server-test/some-module.ts",
            },
            output: {
                filename: '[name].js',
                path: path.resolve(__dirname, "Scripts/server-test"),
                libraryTarget: 'commonjs'
            },         
            module: {
                rules: [
                    {
                        test: /\.tsx?$/,
                        loader: 'ts-loader',                    
                        exclude: file => (
                            /node_modules/.test(file
                        ))
                    },
                    {
                        test: /\.js$/,
                        exclude: /node_modules/,
                        use: {
                            loader: 'babel-loader',
                            options: {
                                presets: [
                                    ['@babel/preset-env', {
                                        modules: false,
                                        useBuiltIns: 'usage'
                                    }]
                                ]
                            }
                        }                   
                    } 
                ]
            },
            resolve: {
                extensions: [".ts", ".js"]
            },
            target: 'node',      
            devtool: isDev ? "source-map" : undefined,
            plugins: [
                new ForkTsCheckerWebpackPlugin({
                    typescript: {
                       
                    }
                })
            ]        
        }
    };
  1. 在 packages.json 中为 webpack 创建脚本并运行它。

  2. 将生成“some-module-node.js”。

  3. 使用“start-node.js”作为“Jering.Javascript.NodeJS”的入口点

    const someModule = require('./some-module-node.js')

     function start(callback, x) {  // Module must export a function that takes a callback as its first parameter
         var result = someModule.default()
    
         callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
     }
    
     module.exports = start
    
  4. 从 .net 核心运行

      var test = await StaticNodeJSService.InvokeFromFileAsync<string> 
      ("Scripts/server-test/start-node.js", args: new object[] {  });
    

测试将是“ES6+ 模块”

于 2022-02-24T15:21:21.253 回答