Skip to content

Commit

Permalink
win
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Nov 14, 2024
1 parent 4a7c331 commit fc198ad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/plugins/EntryChunkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;') ||
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/plugins/entryModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
14 changes: 4 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions tests/integration/shims/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit fc198ad

Please sign in to comment.