Skip to content

Commit

Permalink
refactored internals for reusability
Browse files Browse the repository at this point in the history
this will be used for upcoming virtual bundles
  • Loading branch information
FND committed Nov 7, 2018
1 parent 1ae4efa commit d6c7653
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
29 changes: 29 additions & 0 deletions lib/bundle/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

let generateBundle = require("./bundler");
let generateConfig = require("./config");
let { generateError } = require("../util");

let DEFAULTS = {
format: "iife"
};

module.exports = class BasicBundle {
constructor(config, { browsers, plugins }) {
config = Object.assign({}, DEFAULTS, config);
this._config = generateConfig(config, { browsers });
}

compile(entryPoint) {
return generateBundle(entryPoint, this._config, this._cache).
then(({ code, modules, cache }) => {
this._modules = modules; // XXX: only required for non-basic bundles
this._cache = cache;
return { code };
}, err => ({
// also report error from within bundle, to avoid it being overlooked
code: generateError(err),
error: err
}));
}
};
2 changes: 1 addition & 1 deletion lib/bundle/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent
esnext, jsx, typescript, // eslint-disable-next-line indent
sourcemap, compact }, { browsers }) {
let cfg = { sourcemap };

let plugins = [];

if(esnext || jsx) {
let transpiler = Object.assign({}, esnext, jsx);
if(esnext) {
Expand Down
37 changes: 13 additions & 24 deletions lib/bundle/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
"use strict";

let generateBundle = require("./bundler");
let generateConfig = require("./config");
let { generateError } = require("../util");
let BasicBundle = require("./basic");

let DEFAULTS = {
format: "iife"
};

module.exports = class Bundle {
module.exports = class Bundle extends BasicBundle {
constructor(entryPoint, target, config, { browsers }) {
this.entryPoint = entryPoint;
this.target = target;

config = Object.assign({}, DEFAULTS, config);
// extract bundle-specific fingerprinting, if any
config = Object.assign({}, config);
if(config.fingerprint !== undefined) {
this.fingerprint = config.fingerprint;
var fingerprint = config.fingerprint; // eslint-disable-line no-var
delete config.fingerprint;
}
this._config = generateConfig(config, { browsers });
super(config, { browsers });

this.entryPoint = entryPoint;
this.target = target;
if(fingerprint !== undefined) {
this.fingerprint = fingerprint;
}
}

// compiles the bundle - if a list of file paths is provided, compilation
Expand All @@ -32,15 +30,6 @@ module.exports = class Bundle {
return false;
}

return generateBundle(this.entryPoint, this._config, this._cache).
then(({ code, modules, cache }) => {
this._modules = modules;
this._cache = cache;
return { code };
}, err => ({
// also report error from within bundle, to avoid it being overlooked
code: generateError(err),
error: err
}));
return super.compile(this.entryPoint);
}
};

0 comments on commit d6c7653

Please sign in to comment.