Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Switch to ESMs - Make sure that two loader calls are isolated
- Loading branch information
Showing
13 changed files
with
3,718 additions
and
13,706 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
files: ['test/**/*.mjs'] | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {parse} from "marked"; | ||
|
||
export function markdownLoader(markdown) { | ||
const options = this.getOptions(); | ||
|
||
return parse(markdown, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const loaderImport = import("./loader.js"); | ||
|
||
async function trampolin(...args) { | ||
const {markdownLoader} = await loaderImport; | ||
|
||
return markdownLoader.call(this, ...args); | ||
} | ||
|
||
module.exports = trampolin; |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import * as url from "url"; | ||
import * as path from "path"; | ||
import { createRequire } from "module"; | ||
import test from "ava"; | ||
import webpack from "webpack"; | ||
import { Renderer } from "marked"; | ||
|
||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); | ||
const require = createRequire(import.meta.url); | ||
|
||
async function createBundle({ fixture, output, options }) { | ||
await new Promise((resolve, reject) => { | ||
webpack( | ||
{ | ||
entry: path.resolve(__dirname, "fixtures", fixture), | ||
mode: "development", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.md$/, | ||
use: [ | ||
{ | ||
loader: "html-loader", | ||
}, | ||
{ | ||
loader: require.resolve("../src/main.cjs"), | ||
options, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
output: { | ||
library: { | ||
type: "commonjs2", | ||
}, | ||
path: path.resolve(__dirname, "output"), | ||
filename: output, | ||
}, | ||
}, | ||
function onCompilationFinished(error, stats) { | ||
if (error) { | ||
return reject(error); | ||
} | ||
if (stats.hasErrors()) { | ||
return reject(stats.compilation.errors[0]); | ||
} | ||
if (stats.hasWarnings()) { | ||
return reject(stats.compilation.warnings[0]); | ||
} | ||
resolve(); | ||
} | ||
); | ||
}); | ||
|
||
const result = await import(`./output/${output}`); | ||
|
||
// The result is wrapped in 2 default exports: | ||
// - The html-loader creates an ESM with the string assigned to export default | ||
// - The final bundle is a CommonJS module that re-exports the result from the html-loader | ||
return result.default.default; | ||
} | ||
|
||
test("plain markdown", async (t) => { | ||
const code = await createBundle({ | ||
fixture: "plain-markdown.md", | ||
output: "plain-markdown.cjs", | ||
}); | ||
|
||
t.snapshot(code); | ||
}); | ||
|
||
test("with code", async (t) => { | ||
const code = await createBundle({ | ||
fixture: "with-code.md", | ||
output: "with-code.cjs", | ||
}); | ||
|
||
t.snapshot(code); | ||
}); | ||
|
||
test("with custom renderer", async (t) => { | ||
class CustomRenderer extends Renderer { | ||
text() { | ||
return "CUSTOM RENDERER"; | ||
} | ||
} | ||
|
||
let code = await createBundle({ | ||
fixture: "plain-markdown.md", | ||
output: "with-custom-renderer.cjs", | ||
options: { | ||
renderer: new CustomRenderer(), | ||
}, | ||
}); | ||
|
||
t.assert(code.includes("CUSTOM RENDERER")); | ||
|
||
// Make sure that the second call doesn't pick up custom renderer | ||
// This would happen if we used setOptions(), see https://github.com/markedjs/marked/issues/907 | ||
code = await createBundle({ | ||
fixture: "plain-markdown.md", | ||
output: "without-custom-renderer.cjs", | ||
}); | ||
|
||
t.assert(code.includes("CUSTOM RENDERER") === false); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.