Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: source maps to work correctly #51

Merged
merged 7 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
"webpack": "4 || 5"
},
"dependencies": {
"@cspotcode/source-map-support": "^0.8.1",
Copy link
Owner

@privatenumber privatenumber Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me how this fork is different from the original source-map-support so I'm going to revert this.

NVM just saw the issue you linked!

"fs-require": "^1.6.0",
"memfs": "^3.5.0",
"source-map-support": "^0.5.21",
"source-map-url": "^0.4.1",
"yargs": "^16.2.0"
},
"devDependencies": {
Expand Down
74 changes: 56 additions & 18 deletions pnpm-lock.yaml

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

43 changes: 40 additions & 3 deletions src/lib/memfs.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
import path from 'path';
import { createFsFromVolume, Volume } from 'memfs';
import { createFsRequire } from 'fs-require';
import sourceMapSupport from 'source-map-support';
import sourceMapSupport from '@cspotcode/source-map-support';
import sourceMappingURL from 'source-map-url';

export const mfs = createFsFromVolume(new Volume());

// @ts-expect-error To support Webpack 4. No longer needed in WP5
mfs.join = path.join;

function removeFsRequirePrefix(filePath: string) {
const fsRequirePrefix = 'fs-require://1';

if (filePath.startsWith(fsRequirePrefix)) {
return filePath.slice(fsRequirePrefix.length);
}

return filePath;
}

sourceMapSupport.install({
environment: 'node',
retrieveFile(filePath: string) {
if (mfs.existsSync(filePath)) {
return mfs.readFileSync(filePath).toString();
const filteredFilePath = removeFsRequirePrefix(filePath);

if (mfs.existsSync(filteredFilePath)) {
return mfs.readFileSync(filteredFilePath).toString();
}
},
retrieveSourceMap(sourceFilePath: string) {
const filteredSourceFilePath = removeFsRequirePrefix(sourceFilePath);

if (mfs.existsSync(filteredSourceFilePath)) {
const sourceFileContent = mfs.readFileSync(filteredSourceFilePath).toString();
const sourceMapFilePath = sourceMappingURL.getFrom(sourceFileContent);

if (sourceMapFilePath === null) {
return null;
}

const parentDirectory = path.dirname(filteredSourceFilePath);
const sourceMapFileFullPath = `${parentDirectory}/${sourceMapFilePath}`;

if (mfs.existsSync(sourceMapFileFullPath)) {
return {
url: sourceFilePath,
map: mfs.readFileSync(sourceMapFileFullPath).toString(),
};
}
}

return null;
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests seem to pass when removing this entire retrieveSourceMap function. Is it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch :)
Indeed, tests are OK without it.
It might have been useful at some point in the development process but its no longer the case so it should be removed.
We can also remove direct dependency to source-map-url.

});

export const mRequire = (modulePath: string): any => (
Expand Down
Loading