|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
1 | 3 | import { createRequire } from "module";
|
2 | 4 | import plugin from "@eik/rollup-plugin";
|
3 | 5 | import terser from "@rollup/plugin-terser";
|
4 | 6 | import { nodeResolve } from "@rollup/plugin-node-resolve";
|
5 | 7 | import commonjs from "@rollup/plugin-commonjs";
|
6 | 8 |
|
| 9 | +const moduleName = "@warp-ds/elements-core"; |
| 10 | + |
7 | 11 | const { resolve } = createRequire(import.meta.url);
|
8 | 12 |
|
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); |
11 | 45 |
|
12 | 46 | 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 | + } |
13 | 70 |
|
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 | + } |
41 | 85 | }
|
42 | 86 |
|
43 | 87 | export default config;
|
0 commit comments