Skip to content

Commit

Permalink
Experimenting with using rollup to better package BrowserFS.
Browse files Browse the repository at this point in the history
Nonfunctional due to rollup-plugin-typescript's bug: rollup/rollup-plugin-typescript#28
  • Loading branch information
John Vilk committed Jun 3, 2016
1 parent dd7e665 commit 74f5640
Show file tree
Hide file tree
Showing 65 changed files with 405 additions and 4,142 deletions.
87 changes: 77 additions & 10 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
var fs = require('fs'),
babel = require('rollup-plugin-babel'),
uglify = require('rollup-plugin-uglify'),
typescript = require('rollup-plugin-typescript'),
nodeResolve = require('rollup-plugin-node-resolve'),
commonjs = require('rollup-plugin-commonjs'),
inject = require('rollup-plugin-inject'),
alias = require('rollup-plugin-alias'),
path = require('path'),
_ = require('underscore'),
mold = require('mold-source-map'),
Expand All @@ -22,7 +29,6 @@ var fs = require('fs'),
'tsify', 'browserify-derequire'
]
},
nodeTSConfig = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'tsconfig.json')).toString()),
karmaFiles = [
// Main module and fixtures loader
'test/harness/test.js',
Expand All @@ -35,14 +41,74 @@ var fs = require('fs'),
{ pattern: 'src/**/*.ts*', included: false, watched: false },
{ pattern: 'typings/**/*.d.ts', included: false },
{ pattern: 'node_modules/pako/dist/*.js*', included: false }
],
];
karmaBrowsers = require('detect-browsers').getInstalledBrowsers().map(function(item) {
return item.name;
}).filter(function(name, index, arr) {
// Remove duplicates, and items with a space in them.
return arr.indexOf(name) === index && arr.indexOf(' ') === -1;
});

function getRollupConfig(release) {
// Globals:
// - RELEASE (TODO)
// - Buffer
// - process
// UglifyJS if release (TODO)
// TypeScript: Use ES6 output.
// Source maps w/ inlined sources
// Alias modules:
// - fs, path, wrapped-assert

return {
sourceMap: true,
format: 'es6',
exports: 'named',
useStrict: true,
plugins: [
typescript({
declaration: false,
typescript: require('typescript')
}),
alias({
resolve: ['.js', '.ts'],
buffer: require.resolve('bfs-buffer'),
fs: './src/core/node_fs',
path: require.resolve('bfs-path'),
'wrapped-assert': './test/harness/wrapped-assert'
}),
nodeResolve({
jsnext: true,
main: true,
browser: true,
preferBuiltins: false
}),
commonjs({
include: ['node_modules/**'],
sourceMap: true,
extensions: ['.js', '.ts'],
ignoreGlobal: true
}),
inject({
process: require.resolve('bfs-process'),
Buffer: [require.resolve('bfs-buffer'), 'Buffer']
})/*,
babel({
exclude: "node_modules/**",
presets: ["es2015-rollup"]
})/*,
uglify({
min: {
options: {
sourceMap: true,
sourceMapIncludeSources: true
}
}
})*/
]
};
}

if (karmaBrowsers.indexOf('IE') !== -1) {
karmaBrowsers.push('IE9', 'IE8');
}
Expand Down Expand Up @@ -91,14 +157,6 @@ var karmaConfig = {
}
};

// Filter out test/ files.
nodeTSConfig.files = nodeTSConfig.files.filter(function(file) {
return file.slice(0, 4) !== 'test';
});

// Ugh, need to write this to a file for grunt-ts.
fs.writeFileSync(path.resolve(__dirname, "generated_node_tsconfig.json"), JSON.stringify(nodeTSConfig));

if (!fs.existsSync('build')) {
fs.mkdirSync('build');
}
Expand Down Expand Up @@ -261,6 +319,14 @@ module.exports = function(grunt) {
}
}
},
rollup: {
options: getRollupConfig(false),
release: {
files: {
'build/browserfs.js': 'src/browserify_main.ts'
}
}
},
shell: {
gen_cert: {
command: [
Expand Down Expand Up @@ -329,6 +395,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-exorcise');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-rollup');
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-ts');
grunt.loadNpmTasks('grunt-contrib-copy');
Expand Down
40 changes: 25 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
"node": ">= 0.10"
},
"devDependencies": {
"aliasify": "^1.9.0",
"archiver": "^0.21.0",
"aliasify": "^2.0.0",
"archiver": "^1.0.0",
"babel-preset-es2015-rollup": "^1.1.1",
"bfs-buffer": "^0.1.7",
"bfs-path": "^0.1.2",
"bfs-process": "^0.1.6",
Expand All @@ -34,31 +35,40 @@
"coffee-script": "^1.10.0",
"detect-browsers": "^2.0.0",
"dropbox": "^0.10.3",
"grunt": "^0.4.5",
"grunt-browserify": "^4.0.1",
"grunt-cli": "^0.1.13",
"grunt-contrib-connect": "^0.11.2",
"grunt-contrib-copy": "^0.8.2",
"grunt-contrib-uglify": "^0.11.1",
"grunt": "^1.0.0",
"grunt-browserify": "^5.0.0",
"grunt-cli": "^1.2.0",
"grunt-contrib-connect": "^1.0.2",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-uglify": "^1.0.1",
"grunt-contrib-watch": "*",
"grunt-exorcise": "^2.1.1",
"grunt-istanbul": "^0.7.0",
"grunt-karma": "^0.12.1",
"grunt-karma": "^2.0.0",
"grunt-rollup": "^0.7.1",
"grunt-shell": "^1.2.1",
"grunt-ts": "^5.3.2",
"karma": "^0.13.21",
"karma-browserify": "^5.0.2",
"karma-chrome-launcher": "^0.2.2",
"karma-chrome-launcher": "^1.0.0",
"karma-coverage": "^0.5.3",
"karma-firefox-launcher": "^0.1.7",
"karma-ie-launcher": "^0.2.0",
"karma-mocha": "^0.2.2",
"karma-opera-launcher": "^0.3.0",
"karma-safari-launcher": "^0.1.1",
"karma-firefox-launcher": "^1.0.0",
"karma-ie-launcher": "^1.0.0",
"karma-mocha": "^1.0.1",
"karma-opera-launcher": "^1.0.0",
"karma-rollup-preprocessor": "^2.0.2",
"karma-safari-launcher": "^1.0.0",
"mocha": "^2.4.5",
"mold-source-map": "^0.4.0",
"object-wrapper": "^0.1.1",
"remap-istanbul": "^0.5.1",
"rollup-plugin-alias": "^1.2.0",
"rollup-plugin-babel": "^2.4.0",
"rollup-plugin-commonjs": "^2.2.1",
"rollup-plugin-inject": "^1.4.0",
"rollup-plugin-node-resolve": "^1.5.0",
"rollup-plugin-typescript": "^0.7.5",
"rollup-plugin-uglify": "^0.3.1",
"tsify": "^0.13.2",
"typescript": "^1.8.2",
"underscore": "^1.8.3"
Expand Down
32 changes: 16 additions & 16 deletions src/backend/AsyncMirror.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import file_system = require('../core/file_system');
import {SynchronousFileSystem, FileSystem} from '../core/file_system';
import {ApiError, ErrorCode} from '../core/api_error';
import file_flag = require('../core/file_flag');
import file = require('../core/file');
import FileFlag from '../core/file_flag';
import {File} from '../core/file';
import Stats from '../core/node_fs_stats';
import preload_file = require('../generic/preload_file');
import PreloadFile from '../generic/preload_file';

interface IAsyncOperation {
apiMethod: string;
Expand All @@ -13,8 +13,8 @@ interface IAsyncOperation {
/**
* We define our own file to interpose on syncSync() for mirroring purposes.
*/
class MirrorFile extends preload_file.PreloadFile<AsyncMirror> implements file.File {
constructor(fs: AsyncMirror, path: string, flag: file_flag.FileFlag, stat: Stats, data: Buffer) {
class MirrorFile extends PreloadFile<AsyncMirror> implements File {
constructor(fs: AsyncMirror, path: string, flag: FileFlag, stat: Stats, data: Buffer) {
super(fs, path, flag, stat, data);
}

Expand All @@ -40,17 +40,17 @@ class MirrorFile extends preload_file.PreloadFile<AsyncMirror> implements file.F
* The two stores will be kept in sync. The most common use-case is to pair a synchronous
* in-memory filesystem with an asynchronous backing store.
*/
export default class AsyncMirror extends file_system.SynchronousFileSystem implements file_system.FileSystem {
export default class AsyncMirror extends SynchronousFileSystem implements FileSystem {
/**
* Queue of pending asynchronous operations.
*/
private _queue: IAsyncOperation[] = [];
private _queueRunning: boolean = false;
private _sync: file_system.FileSystem;
private _async: file_system.FileSystem;
private _sync: FileSystem;
private _async: FileSystem;
private _isInitialized: boolean = false;
private _initializeCallbacks: ((e?: ApiError) => void)[] = [];
constructor(sync: file_system.FileSystem, async: file_system.FileSystem) {
constructor(sync: FileSystem, async: FileSystem) {
super();
this._sync = sync;
this._async = async;
Expand All @@ -70,8 +70,8 @@ export default class AsyncMirror extends file_system.SynchronousFileSystem imple
return true;
}

public _syncSync(fd: preload_file.PreloadFile<any>) {
this._sync.writeFileSync(fd.getPath(), fd.getBuffer(), null, file_flag.FileFlag.getFileFlag('w'), fd.getStats().mode);
public _syncSync(fd: PreloadFile<any>) {
this._sync.writeFileSync(fd.getPath(), fd.getBuffer(), null, FileFlag.getFileFlag('w'), fd.getStats().mode);
this.enqueueOp({
apiMethod: 'writeFile',
arguments: [fd.getPath(), fd.getBuffer(), null, fd.getFlag(), fd.getStats().mode]
Expand Down Expand Up @@ -116,12 +116,12 @@ export default class AsyncMirror extends file_system.SynchronousFileSystem imple
}
});
}, copyFile = (p: string, mode: number, cb: (err?: ApiError) => void) => {
this._async.readFile(p, null, file_flag.FileFlag.getFileFlag('r'), (err, data) => {
this._async.readFile(p, null, FileFlag.getFileFlag('r'), (err, data) => {
if (err) {
cb(err);
} else {
try {
this._sync.writeFileSync(p, data, null, file_flag.FileFlag.getFileFlag('w'), mode);
this._sync.writeFileSync(p, data, null, FileFlag.getFileFlag('w'), mode);
} catch (e) {
err = e;
} finally {
Expand Down Expand Up @@ -191,12 +191,12 @@ export default class AsyncMirror extends file_system.SynchronousFileSystem imple
this.checkInitialized();
return this._sync.statSync(p, isLstat);
}
public openSync(p: string, flag: file_flag.FileFlag, mode: number): file.File {
public openSync(p: string, flag: FileFlag, mode: number): File {
this.checkInitialized();
// Sanity check: Is this open/close permitted?
var fd = this._sync.openSync(p, flag, mode);
fd.closeSync();
return new MirrorFile(this, p, flag, this._sync.statSync(p, false), this._sync.readFileSync(p, null, file_flag.FileFlag.getFileFlag('r')));
return new MirrorFile(this, p, flag, this._sync.statSync(p, false), this._sync.readFileSync(p, null, FileFlag.getFileFlag('r')));
}
public unlinkSync(p: string): void {
this.checkInitialized();
Expand Down
22 changes: 11 additions & 11 deletions src/backend/Dropbox.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import preload_file = require('../generic/preload_file');
import file_system = require('../core/file_system');
import file_flag = require('../core/file_flag');
import PreloadFile from '../generic/preload_file';
import {FileSystem, BaseFileSystem} from '../core/file_system';
import FileFlag from '../core/file_flag';
import {default as Stats, FileType} from '../core/node_fs_stats';
import {ApiError, ErrorCode} from '../core/api_error';
import file = require('../core/file');
import async = require('async');
import path = require('path');
import {File} from '../core/file';
import * as async from 'async';
import * as path from 'path';
import {arrayBuffer2Buffer, buffer2ArrayBuffer} from '../core/util';

var errorCodeLookup: {[dropboxErrorCode: number]: ErrorCode} = null;
Expand Down Expand Up @@ -286,8 +286,8 @@ class CachedDropboxClient {
}
}

export class DropboxFile extends preload_file.PreloadFile<DropboxFileSystem> implements file.File {
constructor(_fs: DropboxFileSystem, _path: string, _flag: file_flag.FileFlag, _stat: Stats, contents?: NodeBuffer) {
export class DropboxFile extends PreloadFile<DropboxFileSystem> implements File {
constructor(_fs: DropboxFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: NodeBuffer) {
super(_fs, _path, _flag, _stat, contents)
}

Expand All @@ -311,7 +311,7 @@ export class DropboxFile extends preload_file.PreloadFile<DropboxFileSystem> imp
}
}

export default class DropboxFileSystem extends file_system.BaseFileSystem implements file_system.FileSystem {
export default class DropboxFileSystem extends BaseFileSystem implements FileSystem {
// The Dropbox client.
private _client: CachedDropboxClient;

Expand Down Expand Up @@ -418,7 +418,7 @@ export default class DropboxFileSystem extends file_system.BaseFileSystem implem
});
}

public open(path: string, flags: file_flag.FileFlag, mode: number, cb: (err: ApiError, fd?: file.File) => any): void {
public open(path: string, flags: FileFlag, mode: number, cb: (err: ApiError, fd?: File) => any): void {
// Try and get the file's contents
this._client.readFile(path, (error, content, dbStat) => {
if (error) {
Expand Down Expand Up @@ -490,7 +490,7 @@ export default class DropboxFileSystem extends file_system.BaseFileSystem implem
* Returns a BrowserFS object representing a File, created from the data
* returned by calls to the Dropbox API.
*/
public _makeFile(path: string, flag: file_flag.FileFlag, stat: Dropbox.File.Stat, buffer: NodeBuffer): DropboxFile {
public _makeFile(path: string, flag: FileFlag, stat: Dropbox.File.Stat, buffer: NodeBuffer): DropboxFile {
var type = this._statType(stat);
var stats = new Stats(type, stat.size);
return new DropboxFile(this, path, flag, stats, buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/FolderAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BaseFileSystem, FileSystem} from '../core/file_system';
import path = require('path');
import * as path from 'path';
import {ApiError} from '../core/api_error';

/**
Expand Down
20 changes: 10 additions & 10 deletions src/backend/HTML5FS.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import preload_file = require('../generic/preload_file');
import file_system = require('../core/file_system');
import PreloadFile from '../generic/preload_file';
import {FileSystem as BFSFileSystem, BaseFileSystem} from '../core/file_system';
import {ApiError, ErrorCode} from '../core/api_error';
import {FileFlag, ActionType} from '../core/file_flag';
import {default as FileFlag, ActionType} from '../core/file_flag';
import {default as Stats, FileType} from '../core/node_fs_stats';
import file = require('../core/file');
import path = require('path');
import global = require('../core/global');
import async = require('async');
import {File as BFSFile} from '../core/file';
import * as path from 'path';
import global from '../core/global';
import * as async from 'async';
import {buffer2ArrayBuffer, arrayBuffer2Buffer} from '../core/util';

function isDirectoryEntry(entry: Entry): entry is DirectoryEntry {
Expand Down Expand Up @@ -50,7 +50,7 @@ function _toArray(list?: any[]): any[] {
// - exclusive: If true, only create the entry if it doesn't already exist,
// and throw an error if it does.

export class HTML5FSFile extends preload_file.PreloadFile<HTML5FS> implements file.File {
export class HTML5FSFile extends PreloadFile<HTML5FS> implements BFSFile {
constructor(_fs: HTML5FS, _path: string, _flag: FileFlag, _stat: Stats, contents?: NodeBuffer) {
super(_fs, _path, _flag, _stat, contents);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export class HTML5FSFile extends preload_file.PreloadFile<HTML5FS> implements fi
}
}

export default class HTML5FS extends file_system.BaseFileSystem implements file_system.FileSystem {
export default class HTML5FS extends BaseFileSystem implements BFSFileSystem {
private size: number;
private type: number;
// HTML5File reaches into HTML5FS. :/
Expand Down Expand Up @@ -332,7 +332,7 @@ export default class HTML5FS extends file_system.BaseFileSystem implements file_
this.fs.root.getFile(path, opts, loadAsFile, failedToLoadAsFile);
}

public open(p: string, flags: FileFlag, mode: number, cb: (err: ApiError, fd?: file.File) => any): void {
public open(p: string, flags: FileFlag, mode: number, cb: (err: ApiError, fd?: BFSFile) => any): void {
var error = (err: DOMError): void => {
if (err.name === 'InvalidModificationError' && flags.isExclusive()) {
cb(ApiError.EEXIST(p));
Expand Down
Loading

0 comments on commit 74f5640

Please sign in to comment.