8000 feat: Quantum Source Maps · fuse-box/fuse-box@134292c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 134292c

Browse files
committed
feat: Quantum Source Maps
1 parent 7b7a76b commit 134292c

26 files changed

+465
-210
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ npm-debug.log*
1818
playground
1919
sourcemaps.js.map
2020
yarn.lock
21+
some.sh
2122

2223
# FuseBox Loader API built type definition
2324
# Which is then exported as a module from `src`

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "8"
3+
- "10"
44
install:
55
- npm install
66
- gulp dist

docs/getting-started/setup.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Before we proceed, let's make sure of that we are using NodeJS 8.2+ installed
1313

1414
## Hop on board
1515

16+
If you are interested in a more step by step guide here is a great
17+
[article](https://auth0.com/blog/introducing-fusebox-an-alternative-to-webpack)
18+
1619
- Create `fuse.js`
1720
- Define your producer
1821
- Dev options

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"shorthash": "0.0.2",
5858
"source-map": "^0.7.1",
5959
"stream-browserify": "^2.0.1",
60+
"sourcemap-blender": "1.0.5",
6061
"tslib": "^1.8.0",
6162
"watch": "^1.0.1",
6263
"ws": "^1.1.1"

src/analysis/FileAnalysis.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const plugins: TraversalPlugin[] = [
2626
];
2727

2828
export function acornParse(contents, options?: any): any {
29+
options = options || {};
2930
return acorn.parse(contents, {
3031
...(options || {}),
3132
...{

src/core/Bundle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ExtensionOverrides } from "./ExtensionOverrides";
1616
import { QuantumBit } from "../quantum/plugin/QuantumBit";
1717
import { CombinedTargetAndLanguageLevel } from './CombinedTargetAndLanguageLevel';
1818

19-
type WatchFilterFn = (path: string) => boolean
19+
type WatchFilterFn = (path: string) => boolean;
2020
export interface HMROptions {
2121
port?: number;
2222
socketURI?: string;
@@ -31,6 +31,8 @@ export class Bundle {
3131
public onDoneCallback: any;
3232
public webIndexPriority = 0;
3333
public generatedCode: Buffer;
34+
public generatedSourceMaps: string;
35+
public generatedSourceMapsPath: string;
3436
public bundleAbstraction: BundleAbstraction;
3537
public packageAbstraction: PackageAbstraction;
3638
public lastChangedFile: string;
@@ -50,7 +52,7 @@ export class Bundle {
5052
}
5153

5254
public watch(ruleOrFilterFn?: string | WatchFilterFn, filterFn?: WatchFilterFn): Bundle {
53-
if (typeof ruleOrFilterFn === 'function') {
55+
if (typeof ruleOrFilterFn === "function") {
5456
this.watchRule = "**";
5557
this.watchFilterFn = ruleOrFilterFn;
5658
} else {

src/core/BundleProducer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import * as chokidar from "chokidar";
1010
import { utils, each } from "realm-utils";
1111
import { ProducerAbstraction, ProducerAbtractionOptions } from "../quantum/core/ProducerAbstraction";
1212
import { BundleAbstraction } from "../quantum/core/BundleAbstraction";
13+
import { ModuleCollection } from "./ModuleCollection";
1314

1415
export class BundleProducer {
15-
public bundles = new Map<string, Bundle>();
16+
public bundles: Map<string, Bundle>;
1617
public hmrInjected = false;
1718
public hmrAllowed = true;
1819
public allowSyntheticDefaultImports = false;
@@ -22,6 +23,7 @@ export class BundleProducer {
2223
public sharedEvents = new EventEmitter();
2324
public writeBundles = true;
2425
public sharedCustomPackages: Map<string, SharedCustomPackage>;
26+
public defaultCollection: ModuleCollection;
2527
public runner: BundleRunner;
2628
public userEnvVariables: any = Object.assign({ NODE_ENV: "production" }, process.env);
2729
public devServerOptions: ServerOptions;
@@ -33,10 +35,15 @@ export class BundleProducer {
3335
private chokidarPaths: any;
3436
private warnings = new Map<string, string[]>();
3537
constructor(public fuse: FuseBox) {
38+
this.bundles = new Map<string, Bundle>();
3639
this.runner = new BundleRunner(this.fuse);
3740
}
3841

39-
public run(opts: { chokidar?: any; chokidarPaths?: any; runType?: string }): Promise<BundleProducer> {
42+
public reset() {
43+
this.bundles = new Map<string, Bundle>();
44+
}
45+
46+
public run(opts: { chokidar?: any; runType?: string }): Promise<BundleProducer> {
4047
if (opts) {
4148
this.chokidarOptions = opts.chokidar;
4249
this.chokidarPaths = opts.chokidarPaths;

src/core/File.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export class File {
146146

147147
public hasExtensionOverride = false;
148148

149+
private dynamicImportsReplaced = false;
150+
149151
public addAlternativeContent(str: string) {
150152
this.alternativeContent = this.alternativeContent || "";
151153
this.alternativeContent += "\n" + str;
@@ -234,6 +236,18 @@ export class File {
234236
let collection = this.collection ? this.collection.name : "default";
235237
return `${collection}/${this.info.fuseBoxPath}`;
236238
}
239+
240+
public getSourceMapPath() {
241+
let collection = this.collection ? this.collection.name : "default";
242+
if (collection === this.context.defaultPackageName) {
243+
collection = "src";
244+
}
245+
let filePath = this.info.fuseBoxPath;
246+
if (/.ts(x?)$/.test(this.info.absPath)) {
247+
filePath = this.info.fuseBoxPath.replace(/\.js(x?)$/, ".ts$1");
248+
}
249+
return `${collection}/${filePath}`;
250+
}
237251
/**
238252
*
239253
*
@@ -393,6 +407,8 @@ export class File {
393407

394408
public makeAnalysis(parserOptions?: any, traversalOptions?: { plugins: TraversalPlugin[] }) {
395409
if (!this.analysis.astIsLoaded()) {
410+
parserOptions = parserOptions || {};
411+
parserOptions.sourceMap = this.sourceMap;
396412
this.analysis.parseUsingAcorn(parserOptions);
397413
}
398414
this.analysis.analyze(traversalOptions);
@@ -412,6 +428,7 @@ export class File {
412428
const expression = /(\s+|^|\(|:)(import\()/g;
413429
if (expression.test(targetContent)) {
414430
targetContent = targetContent.replace(expression, "$1$fsmp$(");
431+
this.dynamicImportsReplaced = true;
415432
if (this.context.fuse && this.context.fuse.producer) {
416433
this.devLibsRequired = ["fuse-imports"];
417434
if (!this.context.fuse.producer.devCodeHasBeenInjected("fuse-imports")) {
@@ -677,12 +694,10 @@ export class File {
677694
this.context.debug("TypeScript", `Transpile ${this.info.fuseBoxPath}`);
678695
let result = this.transpileUsingTypescript();
679696
if (result.sourceMapText && this.context.useSourceMaps) {
680-
const correctSourceMapPath = this.getCorrectSourceMapPath();
697+
//const correctSourceMapPath = this.getCorrectSourceMapPath();
681698
let jsonSourceMaps = JSON.parse(result.sourceMapText);
682699
jsonSourceMaps.file = this.info.fuseBoxPath;
683-
jsonSourceMaps.sources = [
684-
this.context.useTypescriptCompiler ? correctSourceMapPath : correctSourceMapPath.replace(/\.js(x?)$/, ".ts$1"),
685-
];
700+
jsonSourceMaps.sources = [this.getSourceMapPath()];
686701
if (!this.context.inlineSourceMaps) {
687702
delete jsonSourceMaps.sourcesContent;
688703
}
@@ -692,8 +707,17 @@ export class File {
692707
`//# sourceMappingURL=${this.context.bundle.name}.js.map?tm=${this.context.cacheBustPreffix}`,
693708
)
694709
.replace("//# sourceMappingURL=module.js.map", "");
710+
if (this.dynamicImportsReplaced) {
711+
// fixing the replaces import statements
712+
// so the sourceContent will look the same
713+
let sourcesContent = jsonSourceMaps.sourcesContent;
714+
if (sourcesContent && sourcesContent[0]) {
715+
sourcesContent[0] = sourcesContent[0].replace(/\$fsmp\$/g, "import");
716+
}
717+
}
695718
this.sourceMap = JSON.stringify(jsonSourceMaps);
696719
}
720+
697721
this.contents = result.outputText;
698722

699723
// consuming transpiled javascript

src/core/FuseBox.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export class FuseBox {
9393

9494
public producer = new BundleProducer(this);
9595

96+
public projectCollection: ModuleCollection;
97+
9698
/**
9799
* Creates an instance of FuseBox.
98100
*
@@ -398,6 +400,9 @@ export class FuseBox {
398400
}
399401

400402
public addDefaultContents() {
403+
if (self.producer) {
404+
self.producer.defaultCollection = this.defaultCollection;
405+
}
401406
return self.collectionSource.get(this.defaultCollection).then((cnt: string) => {
402407
self.context.log.echoDefaultCollection(this.defaultCollection, cnt);
403408
});

src/quantum/core/BundleAbstraction.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { RequireStatement } from "./nodes/RequireStatement";
77

88
export class BundleAbstraction {
99
public splitAbstraction = false;
10-
public packageAbstractions = new Map<string, PackageAbstraction>();
10+
public packageAbstractions: Map<string, PackageAbstraction>;
1111
public producerAbstraction: ProducerAbstraction;
1212
public globalVariableRequired = false;
1313
/**
@@ -22,6 +22,7 @@ export class BundleAbstraction {
2222

2323
constructor(public name: string) {
2424
//producerAbstraction.registerBundleAbstraction(this);
25+
this.packageAbstractions = new Map<string, PackageAbstraction>();
2526
}
2627

2728
public registerHoistedIdentifiers(identifier: string, statement: RequireStatement, file: FileAbstraction) {
@@ -39,7 +40,7 @@ export class BundleAbstraction {
3940
this.packageAbstractions.set(packageAbstraction.name, packageAbstraction);
4041
}
4142

42-
public parse(contents: string) {
43+
public parse(contents: string, sourceMap?) {
4344
const ast = acornParse(contents);
4445
ASTTraverse.traverse(ast, {
4546
pre: (node, parent, prop, idx) => {

0 commit comments

Comments
 (0)
0