Skip to content

Commit 23263a4

Browse files
authored
Merge pull request #183 from finn-no/fix/publish-packages
fix: mirror exports field so we can have individual imports
2 parents 0179a2c + 4cf3636 commit 23263a4

File tree

3 files changed

+147
-46
lines changed

3 files changed

+147
-46
lines changed
Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,87 @@
1+
import fs from "fs";
2+
import path from "path";
13
import { createRequire } from "module";
24
import plugin from "@eik/rollup-plugin";
35
import terser from "@rollup/plugin-terser";
46
import { nodeResolve } from "@rollup/plugin-node-resolve";
57
import commonjs from "@rollup/plugin-commonjs";
68

9+
const moduleName = "@warp-ds/elements-core";
10+
711
const { resolve } = createRequire(import.meta.url);
812

9-
const versions = ["2", "3"];
10-
const config = [];
13+
function getSubpathExports(modulePath) {
14+
let absolutePath = resolve(modulePath);
15+
absolutePath = absolutePath.substring(
16+
0,
17+
absolutePath.indexOf(moduleName) + moduleName.length + "/".length
18+
);
19+
const packageJsonPath = path.join(absolutePath, "package.json");
20+
try {
21+
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
22+
const packageJson = JSON.parse(packageJsonContent);
23+
if (!packageJson) {
24+
console.error(`Error parsing package.json: ${packageJsonPath}`);
25+
return null;
26+
}
27+
if (packageJson.exports) {
28+
for (const [key, value] of Object.entries(packageJson.exports)) {
29+
if (!value.endsWith(".js")) {
30+
delete packageJson.exports[key];
31+
}
32+
}
33+
return packageJson.exports;
34+
} else {
35+
return { ".": packageJson.module || packageJson.main };
36+
}
37+
} catch (error) {
38+
console.error(`Error reading package.json: ${error.message}`);
39+
return null;
40+
}
41+
}
42+
43+
const modulePath = resolve(moduleName);
44+
const subpathExports = getSubpathExports(modulePath);
1145

1246
const lit = (version) => `https://assets.finn.no/npm/lit-${version}/v${version}/lit.min.js`;
47+
const litVersions = ["2", "3"];
48+
const config = [];
49+
for (const litVersion of litVersions) {
50+
for (const [subpath] of Object.entries(subpathExports)) {
51+
const subpathImportPart = subpath !== "." ? `/${subpath.replace("./", "")}` : "";
52+
53+
// Looks messy, but a few things happen here:
54+
// - lit-2 gets the root level folder, other versions in named folders.
55+
// - match the subpath of the input
56+
// - default to index.js for the main entrypoint `"."`.
57+
// - if the subpath does not include `.js`, create an `index.js` in a directory matching the subpath.
58+
// - default to the subpath for all other entrypoints.
59+
let filePath = `dist/`;
60+
if (litVersion !== "2") {
61+
filePath += `lit-${litVersion}/`;
62+
}
63+
if (subpath === ".") {
64+
filePath += "index.js";
65+
} else if (!subpath.endsWith(".js")) {
66+
filePath += `${subpath}/index.js`;
67+
} else {
68+
filePath += subpath.replace("./", "");
69+
}
1370

14-
for (const version of versions) {
15-
config.push({
16-
input: resolve("@warp-ds/elements-core"),
17-
plugins: [
18-
plugin({ maps: [{ imports: { lit: lit(version) } }] }),
19-
nodeResolve(),
20-
commonjs(),
21-
terser(),
22-
],
23-
output: {
24-
file: `dist/lit-v${version}.js`,
25-
format: "esm",
26-
},
27-
});
28-
config.push({
29-
input: resolve("@warp-ds/elements-core/global.js"),
30-
plugins: [
31-
plugin({ maps: [{ imports: { lit: lit(version) } }] }),
32-
nodeResolve(),
33-
commonjs(),
34-
terser(),
35-
],
36-
output: {
37-
file: `dist/global/lit-v${version}.js`,
38-
format: "esm",
39-
},
40-
});
71+
config.push({
72+
input: resolve(`${moduleName}${subpathImportPart}`),
73+
plugins: [
74+
plugin({ maps: [{ imports: { lit: lit(litVersion) } }] }),
75+
nodeResolve(),
76+
commonjs(),
77+
terser(),
78+
],
79+
output: {
80+
file: filePath,
81+
format: "esm",
82+
},
83+
});
84+
}
4185
}
4286

4387
export default config;

packages/warp-ds-elements/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"eik:publish:ci": "../../scripts/publish.js warp-ds-elements @warp-ds/elements"
1313
},
1414
"dependencies": {
15-
"@warp-ds/elements": "1.2.2"
15+
"@warp-ds/elements": "1.2.3-next.2"
1616
},
1717
"devDependencies": {
1818
"@eik/rollup-plugin": "4.0.60",
Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,87 @@
1+
import fs from "fs";
2+
import path from "path";
13
import { createRequire } from "module";
24
import plugin from "@eik/rollup-plugin";
35
import terser from "@rollup/plugin-terser";
46
import { nodeResolve } from "@rollup/plugin-node-resolve";
57
import commonjs from "@rollup/plugin-commonjs";
68

9+
const moduleName = "@warp-ds/elements";
10+
711
const { resolve } = createRequire(import.meta.url);
812

9-
const versions = ["2", "3"];
10-
const config = [];
13+
function getSubpathExports(modulePath) {
14+
let absolutePath = resolve(modulePath);
15+
absolutePath = absolutePath.substring(
16+
0,
17+
absolutePath.indexOf(moduleName) + moduleName.length + "/".length
18+
);
19+
const packageJsonPath = path.join(absolutePath, "package.json");
20+
try {
21+
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
22+
const packageJson = JSON.parse(packageJsonContent);
23+
if (!packageJson) {
24+
console.error(`Error parsing package.json: ${packageJsonPath}`);
25+
return null;
26+
}
27+
if (packageJson.exports) {
28+
for (const [key, value] of Object.entries(packageJson.exports)) {
29+
if (!value.endsWith(".js")) {
30+
delete packageJson.exports[key];
31+
}
32+
}
33+
return packageJson.exports;
34+
} else {
35+
return { ".": packageJson.module || packageJson.main };
36+
}
37+
} catch (error) {
38+
console.error(`Error reading package.json: ${error.message}`);
39+
return null;
40+
}
41+
}
42+
43+
const modulePath = resolve(moduleName);
44+
const subpathExports = getSubpathExports(modulePath);
1145

1246
const lit = (version) => `https://assets.finn.no/npm/lit-${version}/v${version}/lit.min.js`;
47+
const litVersions = ["2", "3"];
48+
const config = [];
49+
for (const litVersion of litVersions) {
50+
for (const [subpath] of Object.entries(subpathExports)) {
51+
const subpathImportPart = subpath !== "." ? `/${subpath.replace("./", "")}` : "";
52+
53+
// Looks messy, but a few things happen here:
54+
// - lit-2 gets the root level folder, other versions in named folders.
55+
// - match the subpath of the input
56+
// - default to index.js for the main entrypoint `"."`.
57+
// - if the subpath does not include `.js`, create an `index.js` in a directory matching the subpath.
58+
// - default to the subpath for all other entrypoints.
59+
let filePath = `dist/`;
60+
if (litVersion !== "2") {
61+
filePath += `lit-${litVersion}/`;
62+
}
63+
if (subpath === ".") {
64+
filePath += "index.js";
65+
} else if (!subpath.endsWith(".js")) {
66+
filePath += `${subpath}/index.js`;
67+
} else {
68+
filePath += subpath.replace("./", "");
69+
}
1370

14-
for (const version of versions) {
15-
config.push({
16-
input: resolve("@warp-ds/elements"),
17-
plugins: [
18-
plugin({ maps: [{ imports: { lit: lit(version) } }] }),
19-
nodeResolve(),
20-
commonjs(),
21-
terser(),
22-
],
23-
output: {
24-
file: `dist/lit-v${version}.js`,
25-
format: "esm",
26-
},
27-
});
71+
config.push({
72+
input: resolve(`${moduleName}${subpathImportPart}`),
73+
plugins: [
74+
plugin({ maps: [{ imports: { lit: lit(litVersion) } }] }),
75+
nodeResolve(),
76+
commonjs(),
77+
terser(),
78+
],
79+
output: {
80+
file: filePath,
81+
format: "esm",
82+
},
83+
});
84+
}
2885
}
2986

3087
export default config;

0 commit comments

Comments
 (0)