Skip to content

Commit

Permalink
Fallback to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Apr 22, 2023
1 parent f6143dd commit 5c11120
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-undef */
import {Environment, napi} from 'napi-wasm';
import fs from 'fs';
import path from 'path';
const {Environment, napi} = require('napi-wasm');
const fs = require('fs');
const path = require('path');

export let Resolver;

export default async function init(input) {
if (Resolver == null) {
module.exports.Resolver = undefined;
module.exports.init = async function init(input) {
if (module.exports.Resolver == null) {
input = input ?? path.join(__dirname, 'parcel_resolver_node.wasm');
if (
typeof input === 'string' ||
Expand All @@ -27,6 +26,6 @@ export default async function init(input) {
}

let env = new Environment(instance);
Resolver = env.exports.Resolver;
module.exports.Resolver = env.exports.Resolver;
}
}
};
5 changes: 5 additions & 0 deletions packages/utils/node-resolver-core/index-with-fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
module.exports = require('./index.js');
} catch (e) {
module.exports = require('./index-wasm.js');
}
44 changes: 44 additions & 0 deletions packages/utils/node-resolver-core/index-with-fallback.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// @flow
import type {FileCreateInvalidation} from '@parcel/types';

export interface JsFileSystemOptions {
canonicalize: string => string;
read: string => Buffer;
isFile: string => boolean;
isDir: string => boolean;
includeNodeModules?: boolean | Array<string> | {|[string]: boolean|};
}
export interface ResolverOptions {
fs?: JsFileSystemOptions;
includeNodeModules?: boolean | Array<string> | {|[string]: boolean|};
conditions?: number;
moduleDirResolver?: (...args: any[]) => any;
mode: number;
entries?: number;
extensions?: Array<string>;
}
export interface ResolveOptions {
filename: string;
specifierType: string;
parent: string;
}
export interface ResolveResult {
resolution: Resolution;
invalidateOnFileChange: Array<string>;
invalidateOnFileCreate: Array<FileCreateInvalidation>;
query?: string;
sideEffects: boolean;
error: mixed;
}
export type Resolution =
| {|type: 'path', value: string|}
| {|type: 'builtin', value: string|}
| {|type: 'external'|}
| {|type: 'empty'|}
| {|type: 'global', value: string|};
declare export class Resolver {
constructor(projectRoot: string, options: ResolverOptions): Resolver;
resolve(options: ResolveOptions): ResolveResult;
}

declare export var init: void | (() => void);
42 changes: 0 additions & 42 deletions packages/utils/node-resolver-core/index.js.flow

This file was deleted.

3 changes: 3 additions & 0 deletions packages/utils/node-resolver-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
"url": "^0.11.0",
"util": "^0.12.3",
"vm-browserify": "^1.1.2"
},
"browser": {
"index-with-fallback.js": "index-wasm.js"
}
}
2 changes: 1 addition & 1 deletion packages/utils/node-resolver-core/src/Wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {FileSystem} from '@parcel/fs';
import type {PackageManager} from '@parcel/package-manager';
import type {Diagnostic} from '@parcel/diagnostic';
import {NodeFS} from '@parcel/fs';
import init, {Resolver} from '../wasm';
import {init, Resolver} from '../index-with-fallback';
import builtins, {empty} from './builtins';
import path from 'path';
import {
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/node-resolver-core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
export {default} from './Wrapper';
// export {Resolver as ResolverBase} from '../index';
export {Resolver as ResolverBase, default as init} from '../wasm';
export {Resolver as ResolverBase, init} from '../index-with-fallback';

0 comments on commit 5c11120

Please sign in to comment.