Skip to content

Commit 63a5d83

Browse files
authored
Fix CommonJS bundling (#1012)
* Fix CommonJS bundling * Add bundling unit tests
1 parent ed94f6b commit 63a5d83

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/env.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,20 @@ export const apis = Object.freeze({
7373
});
7474

7575
const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE;
76-
const dirname__ = RUNNING_LOCALLY
77-
? path.dirname(path.dirname(url.fileURLToPath(import.meta.url)))
78-
: './';
76+
77+
let dirname__ = './';
78+
if (RUNNING_LOCALLY) {
79+
// NOTE: We wrap `import.meta` in a call to `Object` to prevent Webpack from trying to bundle it in CommonJS.
80+
// Although we get the warning: "Accessing import.meta directly is unsupported (only property access or destructuring is supported)",
81+
// it is safe to ignore since the bundled value (`{}`) isn't used for CommonJS environments (we use __dirname instead).
82+
const _import_meta_url = Object(import.meta).url;
83+
84+
if (_import_meta_url) {
85+
dirname__ = path.dirname(path.dirname(url.fileURLToPath(_import_meta_url))) // ESM
86+
} else if (typeof __dirname !== 'undefined') {
87+
dirname__ = path.dirname(__dirname) // CommonJS
88+
}
89+
}
7990

8091
// Only used for environments with access to file system
8192
const DEFAULT_CACHE_DIR = RUNNING_LOCALLY

tests/bundles.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { spawnSync } from "child_process";
2+
3+
const MODULE_NAME = "@huggingface/transformers";
4+
5+
const CODE_BODY = `
6+
const model_id = "hf-internal-testing/tiny-random-LlamaForCausalLM";
7+
const generator = await pipeline("text-generation", model_id, { dtype: "fp32" });
8+
const result = await generator("hello", { max_new_tokens: 3, return_full_text: false });
9+
process.stdout.write(result[0].generated_text);
10+
`;
11+
12+
const TARGET_OUTPUT = "erdingsAndroid Load";
13+
14+
const wrap_async_iife = (code) => `(async function() { ${code} })();`;
15+
16+
const check = (code, module = false) => {
17+
const args = ["-e", code];
18+
if (module) args.push("--input-type=module");
19+
const { status, stdout, stderr } = spawnSync("node", args);
20+
expect(stderr.toString()).toBe(""); // No warnings or errors are printed
21+
expect(stdout.toString()).toBe(TARGET_OUTPUT); // The output should match
22+
expect(status).toBe(0); // The process should exit cleanly
23+
};
24+
25+
describe("Testing the bundle", () => {
26+
it("ECMAScript Module (ESM)", () => {
27+
check(`import { pipeline } from "${MODULE_NAME}";${CODE_BODY}`, true);
28+
});
29+
30+
it("CommonJS (CJS) with require", () => {
31+
check(`const { pipeline } = require("${MODULE_NAME}");${wrap_async_iife(CODE_BODY)}`);
32+
});
33+
34+
it("CommonJS (CJS) with dynamic import", () => {
35+
check(`${wrap_async_iife(`const { pipeline } = await import("${MODULE_NAME}");${CODE_BODY}`)}`);
36+
});
37+
});

0 commit comments

Comments
 (0)