Skip to content

Commit

Permalink
fix: @marko/register source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 12, 2023
1 parent f214d55 commit 3ac77a8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
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;
}

0 comments on commit 3ac77a8

Please sign in to comment.