Skip to content

Commit

Permalink
Merge pull request #183 from finn-no/fix/publish-packages
Browse files Browse the repository at this point in the history
fix: mirror exports field so we can have individual imports
  • Loading branch information
wkillerud authored Jan 19, 2024
2 parents 0179a2c + 4cf3636 commit 23263a4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 46 deletions.
102 changes: 73 additions & 29 deletions packages/warp-ds-elements-core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,87 @@
import fs from "fs";
import path from "path";
import { createRequire } from "module";
import plugin from "@eik/rollup-plugin";
import terser from "@rollup/plugin-terser";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

const moduleName = "@warp-ds/elements-core";

const { resolve } = createRequire(import.meta.url);

const versions = ["2", "3"];
const config = [];
function getSubpathExports(modulePath) {
let absolutePath = resolve(modulePath);
absolutePath = absolutePath.substring(
0,
absolutePath.indexOf(moduleName) + moduleName.length + "/".length
);
const packageJsonPath = path.join(absolutePath, "package.json");
try {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
if (!packageJson) {
console.error(`Error parsing package.json: ${packageJsonPath}`);
return null;
}
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
if (!value.endsWith(".js")) {
delete packageJson.exports[key];
}
}
return packageJson.exports;
} else {
return { ".": packageJson.module || packageJson.main };
}
} catch (error) {
console.error(`Error reading package.json: ${error.message}`);
return null;
}
}

const modulePath = resolve(moduleName);
const subpathExports = getSubpathExports(modulePath);

const lit = (version) => `https://assets.finn.no/npm/lit-${version}/v${version}/lit.min.js`;
const litVersions = ["2", "3"];
const config = [];
for (const litVersion of litVersions) {
for (const [subpath] of Object.entries(subpathExports)) {
const subpathImportPart = subpath !== "." ? `/${subpath.replace("./", "")}` : "";

// Looks messy, but a few things happen here:
// - lit-2 gets the root level folder, other versions in named folders.
// - match the subpath of the input
// - default to index.js for the main entrypoint `"."`.
// - if the subpath does not include `.js`, create an `index.js` in a directory matching the subpath.
// - default to the subpath for all other entrypoints.
let filePath = `dist/`;
if (litVersion !== "2") {
filePath += `lit-${litVersion}/`;
}
if (subpath === ".") {
filePath += "index.js";
} else if (!subpath.endsWith(".js")) {
filePath += `${subpath}/index.js`;
} else {
filePath += subpath.replace("./", "");
}

for (const version of versions) {
config.push({
input: resolve("@warp-ds/elements-core"),
plugins: [
plugin({ maps: [{ imports: { lit: lit(version) } }] }),
nodeResolve(),
commonjs(),
terser(),
],
output: {
file: `dist/lit-v${version}.js`,
format: "esm",
},
});
config.push({
input: resolve("@warp-ds/elements-core/global.js"),
plugins: [
plugin({ maps: [{ imports: { lit: lit(version) } }] }),
nodeResolve(),
commonjs(),
terser(),
],
output: {
file: `dist/global/lit-v${version}.js`,
format: "esm",
},
});
config.push({
input: resolve(`${moduleName}${subpathImportPart}`),
plugins: [
plugin({ maps: [{ imports: { lit: lit(litVersion) } }] }),
nodeResolve(),
commonjs(),
terser(),
],
output: {
file: filePath,
format: "esm",
},
});
}
}

export default config;
2 changes: 1 addition & 1 deletion packages/warp-ds-elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"eik:publish:ci": "../../scripts/publish.js warp-ds-elements @warp-ds/elements"
},
"dependencies": {
"@warp-ds/elements": "1.2.2"
"@warp-ds/elements": "1.2.3-next.2"
},
"devDependencies": {
"@eik/rollup-plugin": "4.0.60",
Expand Down
89 changes: 73 additions & 16 deletions packages/warp-ds-elements/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,87 @@
import fs from "fs";
import path from "path";
import { createRequire } from "module";
import plugin from "@eik/rollup-plugin";
import terser from "@rollup/plugin-terser";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

const moduleName = "@warp-ds/elements";

const { resolve } = createRequire(import.meta.url);

const versions = ["2", "3"];
const config = [];
function getSubpathExports(modulePath) {
let absolutePath = resolve(modulePath);
absolutePath = absolutePath.substring(
0,
absolutePath.indexOf(moduleName) + moduleName.length + "/".length
);
const packageJsonPath = path.join(absolutePath, "package.json");
try {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
if (!packageJson) {
console.error(`Error parsing package.json: ${packageJsonPath}`);
return null;
}
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
if (!value.endsWith(".js")) {
delete packageJson.exports[key];
}
}
return packageJson.exports;
} else {
return { ".": packageJson.module || packageJson.main };
}
} catch (error) {
console.error(`Error reading package.json: ${error.message}`);
return null;
}
}

const modulePath = resolve(moduleName);
const subpathExports = getSubpathExports(modulePath);

const lit = (version) => `https://assets.finn.no/npm/lit-${version}/v${version}/lit.min.js`;
const litVersions = ["2", "3"];
const config = [];
for (const litVersion of litVersions) {
for (const [subpath] of Object.entries(subpathExports)) {
const subpathImportPart = subpath !== "." ? `/${subpath.replace("./", "")}` : "";

// Looks messy, but a few things happen here:
// - lit-2 gets the root level folder, other versions in named folders.
// - match the subpath of the input
// - default to index.js for the main entrypoint `"."`.
// - if the subpath does not include `.js`, create an `index.js` in a directory matching the subpath.
// - default to the subpath for all other entrypoints.
let filePath = `dist/`;
if (litVersion !== "2") {
filePath += `lit-${litVersion}/`;
}
if (subpath === ".") {
filePath += "index.js";
} else if (!subpath.endsWith(".js")) {
filePath += `${subpath}/index.js`;
} else {
filePath += subpath.replace("./", "");
}

for (const version of versions) {
config.push({
input: resolve("@warp-ds/elements"),
plugins: [
plugin({ maps: [{ imports: { lit: lit(version) } }] }),
nodeResolve(),
commonjs(),
terser(),
],
output: {
file: `dist/lit-v${version}.js`,
format: "esm",
},
});
config.push({
input: resolve(`${moduleName}${subpathImportPart}`),
plugins: [
plugin({ maps: [{ imports: { lit: lit(litVersion) } }] }),
nodeResolve(),
commonjs(),
terser(),
],
output: {
file: filePath,
format: "esm",
},
});
}
}

export default config;

0 comments on commit 23263a4

Please sign in to comment.