From fc198ad5abbe00f2b7658a5066355cd710d70421 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Thu, 14 Nov 2024 10:48:02 +0800 Subject: [PATCH] win --- packages/core/src/plugins/EntryChunkPlugin.ts | 14 +++++++++++--- packages/core/src/plugins/entryModuleLoader.ts | 8 +++++--- pnpm-lock.yaml | 14 ++++---------- tests/integration/shims/index.test.ts | 10 ++++++---- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/core/src/plugins/EntryChunkPlugin.ts b/packages/core/src/plugins/EntryChunkPlugin.ts index 62b53f096..b5aa00c3e 100644 --- a/packages/core/src/plugins/EntryChunkPlugin.ts +++ b/packages/core/src/plugins/EntryChunkPlugin.ts @@ -74,6 +74,8 @@ class EntryChunkPlugin { encoding: 'utf-8', }); + console.log('🥵', content.includes('\n'), content.includes('\r\n')); + // Shebang if (content.startsWith(SHEBANG_PREFIX)) { const shebangMatch = matchFirstLine(content, SHEBANG_REGEX); @@ -118,6 +120,12 @@ class EntryChunkPlugin { for (const name of chunkAsset) { compilation.updateAsset(name, (old) => { const oldSource = old.source().toString(); + console.log( + '🤡', + oldSource.includes('\n'), + oldSource.includes('\r\n'), + ); + const replaceSource = new rspack.sources.ReplaceSource(old); if ( oldSource.startsWith('use strict;') || @@ -126,7 +134,7 @@ class EntryChunkPlugin { replaceSource.replace( 0, 11, // 'use strict;'.length, - `"use strict";${os.EOL}${importMetaUrlShim}`, + `"use strict";\n${importMetaUrlShim}`, ); } else { replaceSource.insert(0, importMetaUrlShim); @@ -154,13 +162,13 @@ class EntryChunkPlugin { const replaceSource = new rspack.sources.ReplaceSource(old); // Shebang if (shebangValue) { - replaceSource.insert(0, `${shebangValue}${os.EOL}`); + replaceSource.insert(0, `${shebangValue}\n`); this.shebangInjectedAssets.add(name); } // React directives if (reactDirectiveValue) { - replaceSource.insert(0, `${reactDirectiveValue}${os.EOL}`); + replaceSource.insert(0, `${reactDirectiveValue}\n`); } return replaceSource; diff --git a/packages/core/src/plugins/entryModuleLoader.ts b/packages/core/src/plugins/entryModuleLoader.ts index 0c1089ed9..7ea0fa3ce 100644 --- a/packages/core/src/plugins/entryModuleLoader.ts +++ b/packages/core/src/plugins/entryModuleLoader.ts @@ -10,14 +10,16 @@ const loader: LoaderDefinition = function loader(source) { let result = source; if (this.resourceQuery === `?${RSLIB_ENTRY_QUERY}`) { - const [firstLine1, ...rest1] = result.split(os.EOL).slice(1); + console.log('👝', source.includes('\n'), source.includes('\r\n')); + const [firstLine1, ...rest1] = result.split(os.EOL); + if (SHEBANG_REGEX.test(firstLine1!)) { - result = rest1.join(os.EOL); + result = rest1.join('\n'); } const [firstLine2, ...rest2] = result.split(os.EOL); if (REACT_DIRECTIVE_REGEX.test(firstLine2!)) { - result = rest2.join(os.EOL); + result = rest2.join('\n'); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0c66221bd..7b00ed90f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -522,7 +522,10 @@ importers: tests/integration/define: {} -<<<<<<< HEAD + tests/integration/directive/react/bundleless: {} + + tests/integration/directive/shebang: {} + tests/integration/dts/build/__references__: {} tests/integration/dts/build/abort-on-error: {} @@ -534,15 +537,6 @@ importers: tests/integration/dts/build/process-files: {} tests/integration/dts/build/tsconfig: {} -======= - tests/integration/directive/react/bundleless: {} - -<<<<<<< HEAD - tests/integration/directive/shebang/bundle: {} ->>>>>>> 74692e1 (feat: add EntryChunkPlugin to handle shebang and shims) -======= - tests/integration/directive/shebang: {} ->>>>>>> 4e050fc (up) tests/integration/dts/bundle-false/abort-on-error: {} diff --git a/tests/integration/shims/index.test.ts b/tests/integration/shims/index.test.ts index 9047b5a65..91c124552 100644 --- a/tests/integration/shims/index.test.ts +++ b/tests/integration/shims/index.test.ts @@ -95,10 +95,12 @@ describe('CJS shims', () => { const fileUrl = pathToFileURL(entryFiles.cjs).href; expect(importMetaUrl).toBe(fileUrl); expect(requiredModule).toBe('ok'); - expect(cjsCode.startsWith('"use strict"')).toBe(true); - expect(cjsCode).toContain( - 'const __rslib_import_meta_url__ = /*#__PURE__*/ function() {', - ); + console.log('🥺', cjsCode.includes('\n'), cjsCode.includes('\r\n')); + expect( + cjsCode.startsWith( + `"use strict";\nconst __rslib_import_meta_url__ = /*#__PURE__*/ function() {`, + ), + ).toBe(true); }); test('ESM should not be affected by CJS shims configuration', async () => {