diff --git a/packages/core/src/plugins/EntryChunkPlugin.ts b/packages/core/src/plugins/EntryChunkPlugin.ts index 62b53f09..b5aa00c3 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 0c1089ed..7ea0fa3c 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 0c66221b..7b00ed90 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 9047b5a6..91c12455 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 () => {