2016-07-10 29 views
2

我創建這需要一個第三方庫中的打字稿模塊:TypeScript:如何用窗口對象替換導入?當使用</p> <pre><code>"use strict"; var dexie_1 = require("dexie"); var storage; (function (storage) { ... })(storage || (storage = {})); </code></pre> <p>我還好這個:

import Dexie from "dexie"; 

namespace storage { 
    ... 
} 

當我編譯我的打字稿文件,我得到下面的JavaScript輸出Node.js環境中的輸出。但爲了在瀏覽器中的使用,我想用window中的對象替換var dexie_1 = require("dexie");,例如:var dexie_1 = window.Dexie;

我可以將我編譯的JS中的require語句替換爲window(全局命名空間)中的對象嗎?是否有一個Gulp插件或某事。周圍相似?

tsconfig.json是這樣的:

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "target": "es5" 
    }, 
    "exclude": [ 
    "node_modules", 
    "typings/browser", 
    "typings/browser.d.ts" 
    ] 
} 
+0

你可以評估使用requirejs? – InferOn

回答

3

Webpack可以將require("dexie");映射到window.Dexie

所有你需要做的就是聲明你webpack.config.js如下:

module.exports = { 
    externals: { 
    'dexie': 'Dexie' 
    } 
}; 

這裏是一個最小工作實例爲完整起見:

目錄佈局:

  • bower_components (目錄從bower install
  • DIST(目錄從gulp default
  • node_modules(目錄從npm install
  • SRC(對於打字稿源代碼目錄)
  • 分型(目錄從typings install
  • bower.json(前端依賴)
  • gulpfile。JS(構建配置)
  • 的index.html(演示頁來測試webpacked代碼)
  • index.js(用於分發主入口點)
  • 的package.json(工作流程和建立依賴關係)
  • tsconfig.json (打字稿編譯器配置)
  • webpack.config.json(的WebPack配置)

的src/storage.ts

/// <reference path="../typings/index.d.ts" /> 
import Dexie from "dexie"; 

namespace storage { 
    export function setupDatabase():void { 
    let db = new Dexie('MyDatabase'); 

    db.version(1).stores({ 
     friends: 'name, age' 
    }); 

    db.open().then(function() { 
     console.log('Initialized database: ' + db.name); 
    }); 
    } 
} 

module.exports = storage; 

bower.json

{ 
    "name": "storage", 
    "main": "dist/webpacked.js", 
    "private": true, 
    "dependencies": { 
    "dexie": "^1.4.1" 
    } 
} 

gulpfile.js

var gulp = require('gulp'); 
var rename = require('gulp-rename'); 
var runSequence = require('run-sequence'); 
var ts = require('gulp-typescript'); 
var tsProject = ts.createProject('tsconfig.json'); 
var webpack = require('webpack-stream'); 

gulp.task('build', function() { 
    return gulp.src('src/**/*.ts') 
    .pipe(ts(tsProject)) 
    .pipe(gulp.dest('dist')); 
}); 

gulp.task('webpack', function() { 
    return gulp.src('dist/index.js') 
    .pipe(webpack(require('./webpack.config.js'))) 
    .pipe(rename('webpacked.js')) 
    .pipe(gulp.dest('dist')); 
}); 

gulp.task('default', function (done) { 
    runSequence('build', 'webpack', done); 
}); 

的index.html

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="UTF-8" /> 
    <title>Demo</title> 
    <script src="bower_components/dexie/dist/dexie.js"></script> 
    <script src="dist/webpacked.js"></script> 
    </head> 
    <body> 
    <script> 
     document.addEventListener("DOMContentLoaded", function() { 
     storage.setupDatabase(); 
     }, false); 
    </script> 
    </body> 
</html> 

個index.js

window.storage = require('./dist/storage'); 

的package.json

{ 
    "name": "storage", 
    "private": true, 
    "devDependencies": { 
    "dexie": "^1.4.1", 
    "gulp": "^3.9.1", 
    "gulp-rename": "^1.2.2", 
    "gulp-typescript": "^2.13.6", 
    "run-sequence": "^1.2.2", 
    "webpack-stream": "^3.2.0" 
    } 
} 

tsconfig.json

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "target": "es5" 
    }, 
    "exclude": [ 
    "node_modules", 
    "typings/browser", 
    "typings/browser.d.ts" 
    ] 
} 

typings.json

{ 
    "globalDependencies": { 
    "node": "registry:dt/node#6.0.0+20160709114037" 
    } 
} 

注:node項來自typings install dt~node --global --save,並且需要打字稿在module.exports語句來解決module

webpack.config.js

module.exports = { 
    externals: { 
    'dexie': 'Dexie' 
    } 
}; 

方法:

打字稿代碼進口Dexie和使用命名空間storage聲明本身。要遵循commonjs依賴關係管理方法(在tsconfig.json中聲明),TypeScript代碼需要將storage命名空間作爲模塊導出:module.exports = storage

由於TypeScript不知道module對象,我們需要獲取它的定義。 module定義是node的類型定義的一部分,我們從DefinitelyTyped存儲庫獲得typings工具使用typings install dt~node --global --save。要在TypeScript中鏈接節點檢索到的類型定義,我們需要聲明/// <reference path="../typings/index.d.ts" />

在我們編譯我們的TypeScript代碼(使用gulp build)之後,我們需要聲明一個入口點來使我們的代碼可訪問。這在index.js中完成。成功構建輸出我們的dist/storage.js文件,該文件在index.js中引用。

當構建到位時,我們可以對我們的代碼進行webpack(將其捆綁爲HTML5瀏覽器)。我們的webpack.config.js將我們依賴關係的「require」名稱(dexie)映射到全局名稱空間(window.Dexie)中的對象。這保證我們,Dexie不是我們編譯代碼的一部分(dist/webpacked.js)。

一旦我們有webpacked.js,我們可以在瀏覽器中使用它。但是我們必須確保所有的外部依賴都在我們的HTML頁面被引用(這就是爲什麼Dexie也被聲明爲使用Bower的前端依賴)。

3

如何使用窗口對象替代進口?

這是不可能的。有兩個選項:

使用全局對象

在瀏覽器中,當一個對象被一個<script>加載,你的代碼使用它作爲一個全局變量。在TypeScript中,你必須使用全局聲明文件。這裏不使用模塊,所以沒有import這樣做。

使用捆綁或裝載

您可以使用捆綁,for example Webpack。或者a loader like SystemJS

相關問題