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: @marko/register source maps #1997

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changeset/witty-actors-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@marko/compiler": patch
"marko": patch
"@marko/translator-default": patch
---

Ensure source maps are loaded in dev mode when using the @marko/register hook.
14 changes: 5 additions & 9 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions packages/babel-utils/src/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,12 @@ export function resolveTagImport(path, request) {

function createNewFileOpts(opts, filename) {
const sourceFileName = basename(filename);
const sourceRoot = dirname(filename);
const filenameRelative = relative(CWD, filename);
return {
...opts,
filename,
sourceRoot,
sourceFileName,
filenameRelative,
parserOpts: {
Expand All @@ -310,6 +312,7 @@ function createNewFileOpts(opts, filename) {
generatorOpts: {
...opts.generatorOpts,
filename,
sourceRoot,
sourceFileName
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"raptor-util": "^3.2.0",
"resolve-from": "^5.0.0",
"self-closing-tags": "^1.0.1",
"source-map-support": "^0.5.21",
"strip-ansi": "^6.0.0",
"strip-json-comments": "^3.1.1"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/compiler/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { buildCodeFrameError } from "./util/build-code-frame";
import throwAggregateError from "./util/merge-errors";
export { taglib };

const CWD = process.cwd();

let globalConfig = { ...defaultConfig };
export function configure(newConfig) {
globalConfig = { ...defaultConfig, ...newConfig };
Expand Down Expand Up @@ -88,9 +90,8 @@ function loadBabelConfig(filename, { babelConfig, ...markoConfig }) {
]
];
const baseBabelConfig = {
filenameRelative: filename
? path.relative(process.cwd(), filename)
: undefined,
filenameRelative: filename ? path.relative(CWD, filename) : undefined,
sourceRoot: filename ? path.dirname(filename) : undefined,
sourceFileName: filename ? path.basename(filename) : undefined,
configFile: isTranslated,
babelrc: isTranslated,
Expand Down
53 changes: 37 additions & 16 deletions packages/compiler/src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@ const compiler = require(".");
const shouldOptimize = require("./util/should-optimize").default;
const requiredOptions = { modules: "cjs" };
const isDev = !shouldOptimize();
const sourceMaps = new Map();
let installSourceMaps = () => {
installSourceMaps = () => {};
require("source-map-support").install({
handleUncaughtExceptions: false,
environment: "node",
retrieveSourceMap(source) {
const map = sourceMaps.get(source);
if (map) {
return { url: null, map };
}
return null;
}
});
};

module.exports = register;
register();

function register({ extensions = require.extensions, ...options } = {}) {
extensions[".marko"] = (module, filename) =>
module._compile(
compiler.compileFileSync(
filename,
Object.assign(
{
meta: true,
hot: process.env.BROWSER_REFRESH_URL !== undefined,
// eslint-disable-next-line no-constant-condition
sourceMaps: isDev ? "inline" : false
},
options,
requiredOptions
)
).code,
filename
extensions[".marko"] = (module, filename) => {
const compiled = compiler.compileFileSync(
filename,
Object.assign(
{
meta: true,
hot: process.env.BROWSER_REFRESH_URL !== undefined,
// eslint-disable-next-line no-constant-condition
sourceMaps: isDev ? "both" : false
},
options,
requiredOptions
)
);

if (compiled.map) {
sourceMaps.set(filename, compiled.map);
installSourceMaps();
}

return module._compile(compiled.code, filename);
};

return extensions;
}
Loading