Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/importmap-node-module",
"version": "7.1.1",
"version": "7.2.0",
"description": "Generate importmap for node_modules",
"license": "MIT",
"repository": {
Expand Down
5 changes: 3 additions & 2 deletions src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ if (positionals.length > 1) {
if (
!outfile.endsWith(".html") &&
!outfile.endsWith(".importmap") &&
!outfile.endsWith(".json")
!outfile.endsWith(".json") &&
!outfile.endsWith(".js")
) {
console.error("Error: outfile must end with .html, .importmap or .json");
console.error("Error: outfile must end with .html, .importmap, .json or .js");
process.exit(1);
}

Expand Down
45 changes: 43 additions & 2 deletions src/step_write_into_files/write_into_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ export const writeIntoFiles = (
const importmapAsJson = JSON.stringify(importmap, null, " ");
if (fileUrl.endsWith(".html")) {
writeIntoHtmlFile(fileUrl, importmapAsJson, { logger });
} else if (fileUrl.endsWith(".js")) {
writeInfoJsFile(fileUrl, importmapAsJson, { logger });
} else {
writeFileSync(fileUrl, importmapAsJson);
logger.info(`-> ${urlToFileSystemPath(fileUrl)}`);
writeIntoJsonFile(fileUrl, importmapAsJson, { logger });
}
}
};

const writeIntoJsonFile = (jsonFileUrl, importmapAsJson, { logger }) => {
writeFileSync(jsonFileUrl, importmapAsJson);
logger.info(`-> ${urlToFileSystemPath(jsonFileUrl)}`);
};

const writeIntoHtmlFile = (htmlFileUrl, importmapAsJson, { logger }) => {
const htmlAst = parseHtml({
html: readFileSync(htmlFileUrl, { as: "string" }),
Expand Down Expand Up @@ -80,3 +86,38 @@ const writeIntoHtmlFile = (htmlFileUrl, importmapAsJson, { logger }) => {
const html = stringifyHtmlAst(htmlAst);
writeFileSync(new URL(htmlFileUrl), html);
};

const writeInfoJsFile = (jsFileUrl, importmapAsJson, { logger }) => {
const jsFileContent = `
const currentScript = document.currentScript;
if (!currentScript) {
throw new Error(
"document.currentScript is not available, cannot inject importmap"
);
}
const baseUrl = new URL(".", currentScript.src).href;
const importmap = ${importmapAsJson};
const topLevelMappings = importmap.imports;
const scopedMappings = importmap.scopes;
const makeMappingsAbsolute = () => {
for (const key of Object.keys(mappings)) {
mappings[key] = baseUrl + mappings[key];
}
}
if (topLevelMappings) {
makeMappingsAbsolute(topLevelMappings);
}
if (scopedMappings) {
for (const scope of Object.keys(scopedMappings)) {
const mappings = scopedMappings[scope];
makeMappingsAbsolute(mappings);
}
}
const importmapScript = document.createElement("script");
importmapScript.type = "importmap";
importmapScript.textContent = importmap;
currentScript.after(importmapScript);
`;
writeFileSync(jsFileUrl, jsFileContent);
logger.info(`-> ${urlToFileSystemPath(jsFileUrl)}`);
};
92 changes: 92 additions & 0 deletions tests/write_inside_js/_write_inside_js.test.mjs/0_basic/0_basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# [0_basic](../../write_inside_js.test.mjs#L21)

```js
run("0_basic")
```

# 1/2 write 6 files into "./git_ignored/"

## node_modules/foo/foo.js
```js
console.log('foo');
```

## node_modules/foo/package.json
```json
{
"name": "foo",
"main": "./foo.js",
"private": true
}
```

## index.html
```html
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8">
<link rel="icon" href="data:,">
</head>

<body>
<script type="module" src="./main.js"></script>
</body>
</html>
```

## main.js
```js
import "foo";

```

## package.json
```json
{
"name": "root",
"private": true,
"dependencies": {
"foo": "*"
}
}
```

## index.js

```

const currentScript = document.currentScript;
if (!currentScript) {
throw new Error(
"document.currentScript is not available, cannot inject importmap"
);
}
const baseUrl = new URL(".", currentScript.src).href;
const importmap = {
"imports": {
"root/": "./",
"foo/": "./node_modules/foo/",
"root": "./index",
"foo": "./node_modules/foo/foo.js"
},
"scopes": {}
};
const topLevelMappings = importmap.imports;
const scopedMappings = importmap.scopes;
const makeMappingsAbsolute = () => {
```
see [git_ignored/index.js](git_ignored/index.js) for more

# 2/2 resolve

```js
undefined
```

---

<sub>
Generated by <a href="https://github.com/jsenv/core/tree/main/packages/independent/snapshot">@jsenv/snapshot</a>
</sub>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# [write_inside_js.test.mjs](../write_inside_js.test.mjs)


- [0_basic](0_basic/0_basic.md)

---

<sub>
Generated by <a href="https://github.com/jsenv/core/tree/main/packages/independent/snapshot">@jsenv/snapshot</a>
</sub>
12 changes: 12 additions & 0 deletions tests/write_inside_js/fixtures/0_basic/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8" />
<link rel="icon" href="data:," />
</head>

<body>
<script type="module" src="./main.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions tests/write_inside_js/fixtures/0_basic/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "foo";

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

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

7 changes: 7 additions & 0 deletions tests/write_inside_js/fixtures/0_basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "root",
"private": true,
"dependencies": {
"foo": "*"
}
}
22 changes: 22 additions & 0 deletions tests/write_inside_js/write_inside_js.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { replaceFileStructureSync } from "@jsenv/filesystem";
import { writeImportmaps } from "@jsenv/importmap-node-module";
import { snapshotWriteImportmaps } from "@jsenv/importmap-node-module/tests/snapshot_write_importmaps.js";

const run = async (scenario, options) => {
replaceFileStructureSync({
from: new URL(`./fixtures/${scenario}/`, import.meta.url),
to: new URL("./git_ignored/", import.meta.url),
});
await writeImportmaps({
logLevel: "warn",
directoryUrl: new URL("./git_ignored/", import.meta.url),
importmaps: {
"index.js": {},
},
...options,
});
};

await snapshotWriteImportmaps(import.meta.url, ({ test }) => {
test("0_basic", () => run("0_basic"));
});
Loading