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 (TypeScript ソースコードのディレクトリ)
- タイピング ( のディレクトリ
typings install
)
- bower.json (フロントエンドの依存関係)
- gulpfile.js (ビルド構成)
- index.html (webpack コードをテストするためのデモ ページ)
- index.js (ディストリビューションのプライマリ エントリ ポイント)
- package.json (ワークフローとビルドの依存関係)
- tsconfig.json (TypeScript コンパイラ構成)
- 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');
パッケージ.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"
]
}
型付け.json
{
"globalDependencies": {
"node": "registry:dt/node#6.0.0+20160709114037"
}
}
注:node
エントリは、TypeScript がステートメントでtypings install dt~node --global --save
解決するために必要であり、取得されますmodule
。module.exports
webpack.config.js
module.exports = {
externals: {
'dexie': 'Dexie'
}
};
アプローチ:
TypeScript コードはDexie
、名前空間を使用して自身をインポートおよび宣言しますstorage
。commonjs
依存関係管理 ( で宣言されている)の方法に従うにはtsconfig.json
、TypeScript コードでstorage
名前空間をモジュールとしてエクスポートする必要がありますmodule.exports = storage
。
TypeScript はオブジェクトを認識しないため、そのmodule
定義を取得する必要があります。このmodule
定義は、 の型定義の一部であり、を使用してタイピングツールを使用して、 DefiniteTypedリポジトリnode
から取得します。ノードの取得した型定義を TypeScript でリンクするには、 を宣言する必要があります。typings install dt~node --global --save
/// <reference path="../typings/index.d.ts" />
TypeScript コードを ( を使用してgulp build
) コンパイルした後、エントリ ポイントを宣言して、コードにアクセスできるようにする必要があります。これは で行われindex.js
ます。ビルドが成功するdist/storage.js
と、 で参照されているファイルが出力されindex.js
ます。
ビルドが整ったら、コードを webpack できます (HTML5 ブラウザ用にバンドルするため)。webpack.config.js
依存関係の「必要な」名前 ( dexie
) をグローバル名前空間 ( ) のオブジェクトにマップしますwindow.Dexie
。これにより、Dexieがコンパイル済みコードの一部ではないことが保証されます ( dist/webpacked.js
)。
を取得したらwebpacked.js
、ブラウザで使用できます。ただし、すべての外部依存関係が HTML ページで参照されていることを確認する必要があります (これが、Bowerを使用して Dexie がフロントエンドの依存関係としても宣言されている理由です)。