Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Changes to babel plugin and re-using AST generated by babel #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan.Orlov committed Dec 19, 2016
1 parent 91a813f commit 3862f7b
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 118 deletions.
29 changes: 29 additions & 0 deletions _playground/babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fsbx = require("./../build/commonjs/index.js");
const watch = require("watch");

//global.Promise = require('bluebird')
const FuseBox = fsbx.FuseBox;
const fs = require("fs");
//npm install babel-core babel-preset-latest
let fuseBox = FuseBox.init({
homeDir: "_playground/babel",
cache: true,
outFile: "_playground/_build/out.js",


plugins: [
fsbx.SVGPlugin(),
fsbx.CSSPlugin(),
fsbx.BabelPlugin({
config: {
sourceMaps: true,
presets: ["latest", "stage-0"],
// plugins: [
// ["transform-react-jsx"]
// ]
}
})
]
});

fuseBox.bundle(">index.js", true);
2 changes: 2 additions & 0 deletions _playground/babel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import lib from "./lib/foo";
console.log("my lib", lib);
3 changes: 3 additions & 0 deletions _playground/babel/lib/foo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Foo from "./foo";

export default Foo;
127 changes: 64 additions & 63 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
{
"name": "fuse-box",
"version": "1.3.12",
"description": "Fuse-Box a bundler just does it right",
"typings": "./dist/typings/index.d.ts",
"main": "dist/commonjs/index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "TRAVIS=true ./node_modules/mocha/bin/mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fuse-box/fuse-box.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/fuse-box/fuse-box/issues"
},
"homepage": "https://github.com/fuse-box/fuse-box#readme",
"devDependencies": {
"@types/node": "^6.0.41",
"cheerio": "0.22.0",
"fuse-box-test-using-old-version": "^1.0.0",
"fuse-box-testcase1": "^1.0.0",
"fuse-box-testcase3": "^1.0.3",
"gulp": "^3.9.1",
"gulp-bump": "^2.4.0",
"gulp-concat": "^2.6.0",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-sourcemaps": "^1.6.0",
"gulp-typescript": "^3.0.1",
"gulp-uglify": "^2.0.0",
"gulp-wrap": "^0.13.0",
"mocha": "^3.1.0",
"reflect-metadata": "^0.1.8",
"rollup": "^0.36.3",
"run-sequence": "^1.2.2",
"should": "^11.1.0",
"typescript": "^2.1.1",
"watch": "^1.0.1",
"wires-reactive": "^1.1.1"
},
"dependencies": {
"acorn": "^4.0.3",
"acorn-es7": "^0.1.0",
"acorn-jsx": "^3.0.1",
"ansi": "^0.3.1",
"app-root-path": "^2.0.1",
"ast-traverse": "^0.1.1",
"buffer": "^5.0.0",
"concat-with-sourcemaps": "^1.0.4",
"escodegen": "^1.8.1",
"glob": "^7.1.1",
"mkdirp": "^0.5.1",
"pretty-time": "^0.2.0",
"prettysize": "0.0.3",
"realm-utils": "^1.0.6",
"request": "^2.79.0"
}
}
"name": "fuse-box",
"version": "1.3.12",
"description": "Fuse-Box a bundler just does it right",
"typings": "./dist/typings/index.d.ts",
"main": "dist/commonjs/index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "TRAVIS=true ./node_modules/mocha/bin/mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fuse-box/fuse-box.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/fuse-box/fuse-box/issues"
},
"homepage": "https://github.com/fuse-box/fuse-box#readme",
"devDependencies": {
"@types/node": "^6.0.41",
"cheerio": "0.22.0",
"fuse-box-test-using-old-version": "^1.0.0",
"fuse-box-testcase1": "^1.0.0",
"fuse-box-testcase3": "^1.0.3",
"gulp": "^3.9.1",
"gulp-bump": "^2.4.0",
"gulp-concat": "^2.6.0",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-sourcemaps": "^1.6.0",
"gulp-typescript": "^3.0.1",
"gulp-uglify": "^2.0.0",
"gulp-wrap": "^0.13.0",
"mocha": "^3.1.0",
"reflect-metadata": "^0.1.8",
"rollup": "^0.36.3",
"run-sequence": "^1.2.2",
"should": "^11.1.0",
"typescript": "^2.1.1",
"watch": "^1.0.1",
"wires-reactive": "^1.1.1"
},
"dependencies": {
"acorn": "^4.0.3",
"acorn-es7": "^0.1.0",
"acorn-jsx": "^3.0.1",
"ansi": "^0.3.1",
"app-root-path": "^2.0.1",
"ast-traverse": "^0.1.1",
"buffer": "^5.0.0",
"concat-with-sourcemaps": "^1.0.4",
"escodegen": "^1.8.1",
"glob": "^7.1.1",
"mkdirp": "^0.5.1",
"pretty-time": "^0.2.0",
"prettysize": "0.0.3",
"realm-utils": "^1.0.6",
"request": "^2.79.0",
"watch": "^1.0.1"
}
}
12 changes: 9 additions & 3 deletions src/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ export class File {
this.isLoaded = true;
}

public makeAnalysis() {
if (!this.analysis.astIsLoaded()) {
this.analysis.parseUsingAcorn();
}
this.analysis.analyze();
}


/**
*
Expand All @@ -206,8 +213,8 @@ export class File {

if (/\.js(x)?$/.test(this.absPath)) {
this.loadContents();
this.analysis.process();
this.tryPlugins();
this.makeAnalysis();
return;
}
this.tryPlugins();
Expand Down Expand Up @@ -245,11 +252,10 @@ export class File {
result.outputText = result.outputText.replace("//# sourceMappingURL=module.js.map", "")
this.sourceMap = JSON.stringify(jsonSourceMaps);
}
//console.log(result.outputText);
this.contents = result.outputText;

// consuming transpiled javascript
this.analysis.process();
this.makeAnalysis();
if (this.context.useCache) {
this.context.cache.writeStaticCache(this, this.sourceMap);
}
Expand Down
44 changes: 33 additions & 11 deletions src/FileAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class FileAnalysis {
*/
public ast: any;

private wasAnalysed = false;
private skipAnalysis = false;



/**
Expand All @@ -43,19 +46,25 @@ export class FileAnalysis {
*/
constructor(public file: File) { }

public astIsLoaded(): boolean {
return this.ast !== undefined;
}


/**
* Consuming contents with analysis
* Loads an AST
*
* @param {*} ast
*
* @memberOf FileAST
* @memberOf FileAnalysis
*/
public process() {
this.parse();
this.analyze();
public loadAst(ast: any) {
this.ast = ast;
}


public skip() {
this.skipAnalysis = true;
}

/**
*
Expand All @@ -64,7 +73,7 @@ export class FileAnalysis {
*
* @memberOf FileAST
*/
private parse() {
public parseUsingAcorn() {
this.ast = acorn.parse(this.file.contents, {
sourceType: "module",
tolerant: true,
Expand All @@ -74,14 +83,25 @@ export class FileAnalysis {
});
}

private analyze() {
public analyze() {
// We don't want to make analysis 2 times
if (this.wasAnalysed || this.skipAnalysis) {
return;
}


let out = {
requires: [],
processDeclared: false,
processRequired: false,
fuseBoxBundle: false,
fuseBoxMain: undefined
};

let isString = (node) => {
return node.type === "Literal" || node.type === "StringLiteral";
}

traverse(this.ast, {
pre: (node, parent, prop, idx) => {

Expand All @@ -99,7 +119,7 @@ export class FileAnalysis {
if (node.property.name === "main") {
if (parent.arguments) {
let f = parent.arguments[0];
if (f && f.type === "Literal") {
if (f && isString(f)) {
out.fuseBoxMain = f.value;
out.fuseBoxBundle = true;
}
Expand All @@ -115,14 +135,15 @@ export class FileAnalysis {
}
}
if (node.type === "ImportDeclaration") {
if (node.source && node.source.type === "Literal") {
if (node.source && isString(node.source)) {
out.requires.push(node.source.value);
}
}
if (node.type === "CallExpression") {

if (node.callee.type === "Identifier" && node.callee.name === "require") {
let arg1 = node.arguments[0];
if (arg1.type === "Literal") {
if (isString(arg1)) {
out.requires.push(arg1.value);
}
}
Expand Down Expand Up @@ -150,6 +171,7 @@ export class FileAnalysis {
this.file.alternativeContent = `module.exports = require("${out.fuseBoxMain}")`
}
}
this.wasAnalysed = true;
}

/**
Expand Down
50 changes: 33 additions & 17 deletions src/FuseBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ModuleCollection } from "./ModuleCollection";
import * as path from "path";
import { each, utils, chain, Chainable } from "realm-utils";
const appRoot = require("app-root-path");

const watch = require("watch");
/**
*
*
Expand Down Expand Up @@ -112,22 +112,22 @@ export class FuseBox {
}


public bundle(str: string, standalone?: boolean) {
this.context.reset();
this.triggerStart();
let parser = Arithmetic.parse(str);
let bundle: BundleData;
return Arithmetic.getFiles(parser, this.virtualFiles, this.context.homeDir).then(data => {

bundle = data;
return this.process(data, standalone);
}).then((contents) => {
bundle.finalize(); // Clean up temp folder if required

return contents;
}).catch(e => {
console.log(e.stack || e);
});
/**
* Make a Bundle
*
* @param {string} str
* @param {boolean} [daemon] string to a daemon (watching)
*
* @memberOf FuseBox
*/
public bundle(str: string, daemon?: boolean) {
if (daemon) {
watch.watchTree(this.context.homeDir, { interval: 0.2 }, () => {
this.initiateBundle(str);
});
} else {
return this.initiateBundle(str);
}
}

public process(bundleData: BundleData, standalone?: boolean) {
Expand Down Expand Up @@ -188,5 +188,21 @@ export class FuseBox {
});
}

private initiateBundle(str: string) {
this.context.reset();
this.triggerStart();
let parser = Arithmetic.parse(str);
let bundle: BundleData;
return Arithmetic.getFiles(parser, this.virtualFiles, this.context.homeDir).then(data => {

bundle = data;
return this.process(data);
}).then((contents) => {
bundle.finalize(); // Clean up temp folder if required

return contents;
}).catch(e => {
console.log(e.stack || e);
});
}
}

0 comments on commit 3862f7b

Please sign in to comment.