Skip to content

Commit

Permalink
Fix wrong assert used in export-order-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
JReinhold committed Jan 17, 2024
1 parent 1915bbc commit 1116ec1
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions code/builders/builder-webpack5/src/loaders/export-order-loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'assert';
import { parse as parseCjs, init as initCjsParser } from 'cjs-module-lexer';
import { parse as parseEs } from 'es-module-lexer';
import MagicString from 'magic-string';
import type { LoaderContext } from 'webpack';
import assert from "node:assert";
import { parse as parseCjs, init as initCjsParser } from "cjs-module-lexer";
import { parse as parseEs } from "es-module-lexer";
import MagicString from "magic-string";
import type { LoaderContext } from "webpack";

export default async function loader(
this: LoaderContext<any>,
Expand All @@ -22,30 +22,34 @@ export default async function loader(
const parseResult = await parseEs(source);
const namedExportsOrder = (parseResult[1] || [])
.map((e) => source.substring(e.s, e.e))
.filter((e) => e !== 'default');
.filter((e) => e !== "default");

assert(
namedExportsOrder.length > 0,
'No named exports found. Very likely that this is not a ES module.'
"No named exports found. Very likely that this is not a ES module."
);

magicString.append(
`;export const __namedExportsOrder = ${JSON.stringify(namedExportsOrder)};`
`;export const __namedExportsOrder = ${JSON.stringify(
namedExportsOrder
)};`
);
// Try to parse as CJS module
} catch {
await initCjsParser();
const namedExportsOrder = (parseCjs(source).exports || []).filter(
(e: string) => e !== 'default' && e !== '__esModule'
(e: string) => e !== "default" && e !== "__esModule"
);

assert(
namedExportsOrder.length > 0,
'No named exports found. Very likely that this is not a CJS module.'
"No named exports found. Very likely that this is not a CJS module."
);

magicString.append(
`;module.exports.__namedExportsOrder = ${JSON.stringify(namedExportsOrder)};`
`;module.exports.__namedExportsOrder = ${JSON.stringify(
namedExportsOrder
)};`
);
}

Expand Down

0 comments on commit 1116ec1

Please sign in to comment.