Skip to content

Commit

Permalink
chore: support ES only modules and complex exports
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Feb 15, 2024
1 parent e93bd7a commit 79a7cfa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
27 changes: 16 additions & 11 deletions packages/warp-ds-elements-core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
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);

function getSubpathExports(modulePath) {
let absolutePath = resolve(modulePath);
absolutePath = absolutePath.substring(
0,
absolutePath.indexOf(moduleName) + moduleName.length + "/".length
);
let absolutePath = import.meta.resolve(modulePath);
absolutePath = absolutePath
.substring(0, absolutePath.indexOf(moduleName) + moduleName.length + "/".length)
.replace("file:/", "");
const packageJsonPath = path.join(absolutePath, "package.json");
try {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
Expand All @@ -26,7 +22,16 @@ function getSubpathExports(modulePath) {
}
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
if (!value.endsWith(".js")) {
if (typeof value !== "string") {
/**
* Handle these types of exports by replacing the object with the value of `import`
* "./element.js": {
* "import": "./src/element.js",
* "types": "./types/element.d.ts"
* },
*/
packageJson.exports[key] = value.import;
} else if (!value.endsWith(".js")) {
delete packageJson.exports[key];
}
}
Expand All @@ -40,7 +45,7 @@ function getSubpathExports(modulePath) {
}
}

const modulePath = resolve(moduleName);
const modulePath = import.meta.resolve(moduleName);
const subpathExports = getSubpathExports(modulePath);

const lit = (version) => `https://assets.finn.no/npm/lit-${version}/v${version}/lit.min.js`;
Expand Down Expand Up @@ -69,7 +74,7 @@ for (const litVersion of litVersions) {
}

config.push({
input: resolve(`${moduleName}${subpathImportPart}`),
input: import.meta.resolve(`${moduleName}${subpathImportPart}`).replace("file:/", ""),
plugins: [
plugin({ maps: [{ imports: { lit: lit(litVersion) } }] }),
nodeResolve(),
Expand Down
27 changes: 16 additions & 11 deletions packages/warp-ds-elements/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
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);

function getSubpathExports(modulePath) {
let absolutePath = resolve(modulePath);
absolutePath = absolutePath.substring(
0,
absolutePath.indexOf(moduleName) + moduleName.length + "/".length
);
let absolutePath = import.meta.resolve(modulePath);
absolutePath = absolutePath
.substring(0, absolutePath.indexOf(moduleName) + moduleName.length + "/".length)
.replace("file:/", "");
const packageJsonPath = path.join(absolutePath, "package.json");
try {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
Expand All @@ -26,7 +22,16 @@ function getSubpathExports(modulePath) {
}
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
if (!value.endsWith(".js")) {
if (typeof value !== "string") {
/**
* Handle these types of exports by replacing the object with the value of `import`
* "./element.js": {
* "import": "./src/element.js",
* "types": "./types/element.d.ts"
* },
*/
packageJson.exports[key] = value.import;
} else if (!value.endsWith(".js")) {
delete packageJson.exports[key];
}
}
Expand All @@ -40,7 +45,7 @@ function getSubpathExports(modulePath) {
}
}

const modulePath = resolve(moduleName);
const modulePath = import.meta.resolve(moduleName);
const subpathExports = getSubpathExports(modulePath);

const lit = (version) => `https://assets.finn.no/npm/lit-${version}/v${version}/lit.min.js`;
Expand Down Expand Up @@ -69,7 +74,7 @@ for (const litVersion of litVersions) {
}

config.push({
input: resolve(`${moduleName}${subpathImportPart}`),
input: import.meta.resolve(`${moduleName}${subpathImportPart}`).replace("file:/", ""),
plugins: [
plugin({ maps: [{ imports: { lit: lit(litVersion) } }] }),
nodeResolve(),
Expand Down

0 comments on commit 79a7cfa

Please sign in to comment.