Skip to content

Commit

Permalink
fix: strip BOM before calling transform hook (#5268)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed Nov 24, 2023
1 parent a083019 commit 08da9ee
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,6 @@ export default class Module {
const shebangEndPosition = code.indexOf('\n');
this.shebang = code.slice(2, shebangEndPosition);
}
/* eslint-disable-next-line unicorn/number-literal-case */
if (code.charCodeAt(0) === 0xfe_ff) {
code = code.slice(1);
}

timeStart('generate ast', 3);

Expand Down
5 changes: 5 additions & 0 deletions src/ModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ export class ModuleLoader {
: source != null && typeof source === 'object' && typeof source.code === 'string'
? source
: error(logBadLoader(id));
const code = sourceDescription.code;
/* eslint-disable-next-line unicorn/number-literal-case */
if (code.charCodeAt(0) === 0xfe_ff) {
sourceDescription.code = code.slice(1);
}
const cachedModule = this.graph.cachedModules.get(id);
if (
cachedModule &&
Expand Down
8 changes: 8 additions & 0 deletions test/function/samples/strip-bom-1/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const commonjs = require('@rollup/plugin-commonjs');

module.exports = defineTest({
description: 'Works correctly with BOM files and the @rollup/plugin-commonjs plugin.',
options: {
plugins: [commonjs()]
}
});
12 changes: 12 additions & 0 deletions test/function/samples/strip-bom-1/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function () {
const foo = 'foo';
if (typeof define === 'function' && define.amd) {
define([], function () {
return foo;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = foo;
} else {
this.foo = foo;
}
})();

0 comments on commit 08da9ee

Please sign in to comment.