不导出时出现打字稿“未定义导出”错误

时间:2018-11-29 15:46:18

标签: javascript typescript electron commonjs

我正在使用打字稿构建电子应用程序。我已经按照以下步骤设置了tsconfig

{
    "compilerOptions": {
        /* Basic Options */
        "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
        "module": "commonjs",                  /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */

        "outDir": "./build",                      /* Redirect output structure to the directory. */

        "strict": true,                           /* Enable all strict type-checking options. */

         "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */

        "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    },
    "include": [
        "./src"
    ]
}

我不得不提到我没有在使用webpack或browify。

因此,我现在必须打字稿文件,一个负责创建主窗口,另一个负责处理事件(基本上一个负责主线程,另一个负责渲染器)。我两个都只使用进口,没有出口。问题在于,在渲染器脚本(以javascript形式)中,我在运行tscObject.defineProperty(exports, "__esModule", { value: true });之后得到了这一行,这会在运行应用程序时导致“未定义导出”错误。

我已经尝试按照其他帖子中的建议从tsconfig删除模块属性,但这没有帮助。如果我删除了javascript文件中的那行代码,则一切正常。但是我不知道应该怎么做才能阻止tsc添加该行代码,因为在另一个javascript文件中它没有出现。预先感谢您的帮助!

这是我的打字稿文件,带有已编译的javascript等效文件

app.ts

import {app,BrowserWindow,desktopCapturer, DesktopCapturerSource} from 'electron';
import * as path from 'path';

function createWindow(){
const mainWindow:BrowserWindow = new BrowserWindow({
    width:800,
    height:600
});

mainWindow.loadFile(path.join(__dirname,"../src/assets/index.html"));
mainWindow.webContents.openDevTools()

}
app.on('ready',createWindow);

app.js

"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
var path = __importStar(require("path"));
function createWindow() {
    var mainWindow = new electron_1.BrowserWindow({
        width: 800,
        height: 600
    });
    mainWindow.loadFile(path.join(__dirname, "../src/assets/index.html"));
    mainWindow.webContents.openDevTools();
}
electron_1.app.on('ready', createWindow);

renderer.ts

import { desktopCapturer, DesktopCapturerSource } from "electron";

function onLoad(){
    desktopCapturer.getSources({
        thumbnailSize: {
            width: 256,
            height: 256,
        },
        types: ["screen", "window"]
    }, (error: Error, srcs: DesktopCapturerSource[]) => {
        if (error)
            throw error;
        let p: HTMLElement | null = document.querySelector("#tag");
        for (let src of srcs)
            if (p){
                let temp = src.name + ' ';
                p.textContent += src.name;
            }
    })
}

document.addEventListener("DOMContentLoaded", onLoad);

renderer.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
function onLoad() {
    electron_1.desktopCapturer.getSources({
        thumbnailSize: {
            width: 256,
            height: 256,
        },
        types: ["screen", "window"]
    }, function (error, srcs) {
        if (error)
            throw error;
        var p = document.querySelector("#tag");
        for (var _i = 0, srcs_1 = srcs; _i < srcs_1.length; _i++) {
            var src = srcs_1[_i];
            if (p) {
                var temp = src.name + ' ';
                p.textContent += src.name;
            }
        }
    });
}
document.addEventListener("DOMContentLoaded", onLoad);

2 个答案:

答案 0 :(得分:1)

我不是很积极,但是我认为这应该有所帮助:

// TypeScript considers non-module files to be files without `export`s.
// https://stackoverflow.com/a/41975448/5051090
export {};

尝试将其固定在模块底部。

答案 1 :(得分:1)

我知道了!问题是编译目标。从es5切换到es6可以解决此问题

相关问题