From 1116ec149571a5b5c17ae5d7e775586d5abfbd62 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Wed, 17 Jan 2024 09:40:39 +0100 Subject: [PATCH 1/2] Fix wrong assert used in export-order-loader --- .../src/loaders/export-order-loader.ts | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/code/builders/builder-webpack5/src/loaders/export-order-loader.ts b/code/builders/builder-webpack5/src/loaders/export-order-loader.ts index f57c3a16eb66..58c59a088bb4 100644 --- a/code/builders/builder-webpack5/src/loaders/export-order-loader.ts +++ b/code/builders/builder-webpack5/src/loaders/export-order-loader.ts @@ -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, @@ -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 + )};` ); } From 68c5812433c7baf8822471e8e5180073335bc055 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Wed, 17 Jan 2024 09:44:27 +0100 Subject: [PATCH 2/2] revert bad formatting --- .../src/loaders/export-order-loader.ts | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/code/builders/builder-webpack5/src/loaders/export-order-loader.ts b/code/builders/builder-webpack5/src/loaders/export-order-loader.ts index 58c59a088bb4..8f0b90882278 100644 --- a/code/builders/builder-webpack5/src/loaders/export-order-loader.ts +++ b/code/builders/builder-webpack5/src/loaders/export-order-loader.ts @@ -1,8 +1,8 @@ -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"; +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, @@ -22,34 +22,30 @@ 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)};` ); }