From dfa6492bf8205b24cf970fba460d90ee1e30cf7c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 5 Oct 2015 10:22:10 -0400 Subject: [PATCH] remove sander dependency --- .babelrc | 3 ++- browser/fs.js | 13 +++++++++++++ browser/promise.js | 1 + browser/sander.js | 17 ----------------- gobblefile.js | 11 +++++++---- package.json | 2 +- src/Bundle.js | 2 +- src/rollup.js | 2 +- src/utils/fs.js | 17 +++++++++++++++++ src/utils/load.js | 4 ++-- src/utils/resolveId.js | 4 ++-- 11 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 browser/fs.js create mode 100644 browser/promise.js delete mode 100644 browser/sander.js create mode 100644 src/utils/fs.js diff --git a/.babelrc b/.babelrc index ed25dc0..27617a4 100644 --- a/.babelrc +++ b/.babelrc @@ -14,5 +14,6 @@ "loose": [ "es6.classes", "es6.destructuring" - ] + ], + "compact": false } diff --git a/browser/fs.js b/browser/fs.js new file mode 100644 index 0000000..09bdf18 --- /dev/null +++ b/browser/fs.js @@ -0,0 +1,13 @@ +export function readdirSync () { + throw new Error( 'Cannot use fs.readdirSync inside browser' ); +} + +export function readFileSync () { + throw new Error( 'Cannot use fs.readFileSync inside browser' ); +} + +export function writeFile () { + throw new Error( 'Cannot use fs.writeFile inside browser' ); +} + +export const Promise = window.Promise; diff --git a/browser/promise.js b/browser/promise.js new file mode 100644 index 0000000..06b8309 --- /dev/null +++ b/browser/promise.js @@ -0,0 +1 @@ +export default window.Promise; diff --git a/browser/sander.js b/browser/sander.js deleted file mode 100644 index 5dbe4fc..0000000 --- a/browser/sander.js +++ /dev/null @@ -1,17 +0,0 @@ -export function readdirSync () { - throw new Error( 'Cannot use sander.readdirSync inside browser' ); -} - -export function readFile () { - throw new Error( 'Cannot use sander.readFile inside browser' ); -} - -export function readFileSync () { - throw new Error( 'Cannot use sander.readFileSync inside browser' ); -} - -export function writeFile () { - throw new Error( 'Cannot use sander.writeFile inside browser' ); -} - -export const Promise = window.Promise; diff --git a/gobblefile.js b/gobblefile.js index 4042dd7..8e5f63f 100644 --- a/gobblefile.js +++ b/gobblefile.js @@ -8,13 +8,14 @@ var node = src entry: 'rollup.js', dest: 'rollup.js', format: 'cjs', - external: [ 'sander', 'acorn' ], + external: [ 'fs', 'acorn' ], sourceMap: true }) .transform( 'babel' ); var browserPlaceholders = { - sander: fs.readFileSync( 'browser/sander.js' ).toString() + fs: fs.readFileSync( 'browser/fs.js' ).toString(), + promise: fs.readFileSync( 'browser/promise.js' ).toString() }; var browser = src @@ -23,7 +24,8 @@ var browser = src dest: 'rollup.browser.js', format: 'cjs', load: function ( id ) { - if ( ~id.indexOf( 'sander.js' ) ) return browserPlaceholders.sander; + if ( ~id.indexOf( 'utils/fs' ) ) return browserPlaceholders.fs; + if ( ~id.indexOf( 'es6-promise' ) ) return browserPlaceholders.promise; return fs.readFileSync( id ).toString(); }, external: [ 'acorn' ] @@ -31,7 +33,8 @@ var browser = src .transform( 'browserify', { entries: [ './rollup.browser' ], dest: 'rollup.browser.js', - standalone: 'rollup' + standalone: 'rollup', + builtins: false }); module.exports = gobble([ node, browser ]); diff --git a/package.json b/package.json index 64a2ac8..d171bf2 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "gobble-cli": "^0.4.2", "gobble-esperanto-bundle": "^0.2.0", "gobble-rollup": "^0.8.0", - "gobble-rollup-babel": "^0.1.0", + "gobble-rollup-babel": "^0.3.0", "istanbul": "^0.3.20", "mocha": "^2.2.4", "remap-istanbul": "^0.2.0", diff --git a/src/Bundle.js b/src/Bundle.js index 65a381b..5a4b522 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -1,4 +1,4 @@ -import { Promise } from 'sander'; +import Promise from 'es6-promise/lib/es6-promise/promise'; import MagicString from 'magic-string'; import { blank, keys } from './utils/object'; import Module from './Module'; diff --git a/src/rollup.js b/src/rollup.js index 94722e7..9fd126e 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -1,5 +1,5 @@ import { basename } from './utils/path'; -import { writeFile } from 'sander'; +import { writeFile } from './utils/fs'; import { keys } from './utils/object'; import SOURCEMAPPING_URL from './utils/sourceMappingURL'; import Bundle from './Bundle'; diff --git a/src/utils/fs.js b/src/utils/fs.js new file mode 100644 index 0000000..9a45157 --- /dev/null +++ b/src/utils/fs.js @@ -0,0 +1,17 @@ +import Promise from 'es6-promise/lib/es6-promise/promise'; +import * as fs from 'fs'; + +export function writeFile ( dest, data ) { + return new Promise( ( fulfil, reject ) => { + fs.writeFile( dest, data, err => { + if ( err ) { + reject( err ); + } else { + fulfil(); + } + }); + }); +} + +export const readdirSync = fs.readdirSync; +export const readFileSync = fs.readFileSync; diff --git a/src/utils/load.js b/src/utils/load.js index 60b7566..6cd87bc 100644 --- a/src/utils/load.js +++ b/src/utils/load.js @@ -1,8 +1,8 @@ -import { readFileSync } from 'sander'; +import { readFileSync } from './fs'; export function defaultLoader ( id, options ) { // TODO support plugins e.g. !css and !json? - const source = readFileSync( id, { encoding: 'utf-8' }); + const source = readFileSync( id, 'utf-8' ); return options.transform.reduce( ( source, transformer ) => { return transformer( source, id ); diff --git a/src/utils/resolveId.js b/src/utils/resolveId.js index 7cbed09..2ce7ad0 100644 --- a/src/utils/resolveId.js +++ b/src/utils/resolveId.js @@ -1,5 +1,5 @@ import { absolutePath, dirname, isAbsolute, resolve } from './path'; -import { readdirSync, readFileSync } from 'sander'; +import { readdirSync, readFileSync } from './fs'; function dirExists ( dir ) { try { @@ -58,7 +58,7 @@ export function defaultExternalResolver ( id, importer ) { let pkg; try { - pkg = JSON.parse( readFileSync( pkgPath ).toString() ); + pkg = JSON.parse( readFileSync( pkgPath, 'utf-8' ) ); } catch ( err ) { throw new Error( `Missing or malformed package.json: ${modulePath}` ); }