-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): Add build command by wrapping rollup (#2544)
* (feat): Add build command by wrapping rollup * Modify the plugin and it can download the files too now from cdn * simplify the build scenario * use fetch from @jspm/generator * Update tests for ownname scenario * fix tests * Resolve all changes from the PR comments * use directories as entry and output for result * updatre documentation for the new commands * Display warning message if the dependency is failed to resolve
- Loading branch information
1 parent
dcefe25
commit d77f468
Showing
40 changed files
with
3,014 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import path from "node:path"; | ||
import process from "node:process"; | ||
import { type RollupOptions, rollup } from "rollup"; | ||
|
||
import { JspmError, exists } from "../utils"; | ||
import type { Flags } from "../types"; | ||
import { RollupImportmapPlugin } from "./rollup-importmap-plugin"; | ||
|
||
export default async function build(entry: string, options: Flags) { | ||
if (!entry && !options.config) { | ||
throw new JspmError(`Please provide entry for the build`); | ||
} | ||
|
||
let buildConfig: RollupOptions; | ||
let outputOptions: RollupOptions["output"]; | ||
|
||
if (entry) { | ||
if (!options.output) { | ||
throw new JspmError(`Build output is required when entry is provided`); | ||
} | ||
|
||
const entryPath = path.join(process.cwd(), entry); | ||
if ((await exists(entryPath)) === false) { | ||
throw new JspmError(`Entry file does not exist: ${entryPath}`); | ||
} | ||
buildConfig = { | ||
input: entryPath, | ||
plugins: [RollupImportmapPlugin(options)], | ||
}; | ||
|
||
outputOptions = { | ||
dir: path.join(process.cwd(), options.output), | ||
}; | ||
} | ||
|
||
if (options.config) { | ||
const buildConfigPath = path.join(process.cwd(), options.config); | ||
if ((await exists(buildConfigPath)) === false) { | ||
throw new JspmError( | ||
`Build config file does not exist: ${buildConfigPath}` | ||
); | ||
} | ||
const rollupConfig = await import(buildConfigPath) | ||
.then((mod) => mod.default) | ||
.catch((err) => { | ||
throw new JspmError(`Failed to load build config: ${err}`); | ||
}); | ||
|
||
if ("output" in rollupConfig) { | ||
outputOptions = rollupConfig.output; | ||
} | ||
|
||
buildConfig = { | ||
...rollupConfig, | ||
plugins: [ | ||
...(rollupConfig?.plugins || []), | ||
RollupImportmapPlugin(options), | ||
], | ||
}; | ||
} | ||
|
||
const builder = await rollup(buildConfig); | ||
await builder.write({ format: "esm", ...outputOptions }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Plugin } from "rollup"; | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { pathToFileURL } from "node:url"; | ||
import { fetch } from "@jspm/generator"; | ||
import { Flags } from "../types"; | ||
import { getGenerator, JspmError } from "../utils"; | ||
|
||
const isValidUrl = (url: string) => { | ||
try { | ||
return Boolean(new URL(url)); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
export const RollupImportmapPlugin = async (flags: Flags): Promise<Plugin> => { | ||
/* | ||
Install without a freeze might bump the versions. | ||
We would like to maintian 1:1 on what users defined in importmap. | ||
*/ | ||
const generator = await getGenerator({ ...flags, freeze: true }); | ||
await generator.install(); | ||
|
||
return { | ||
name: "rollup-importmap-plugin", | ||
resolveId: async (id: string, importer: string) => { | ||
if (isValidUrl(id)) { | ||
const url = new URL(id); | ||
if (url.protocol === "deno:" || url.protocol === "node:") { | ||
return { id, external: true }; | ||
} | ||
} | ||
|
||
try { | ||
const resolved = generator.importMap.resolve(id, importer); | ||
return { id: resolved }; | ||
} catch (err) { | ||
console.warn( | ||
`Failed to resolve ${id} from ${importer}, makring as external` | ||
); | ||
return { id, external: true }; | ||
} | ||
}, | ||
load: async (id: string) => { | ||
try { | ||
const url = new URL(id); | ||
if (url.protocol === "file:") { | ||
const filePath = | ||
path.extname(url.pathname) === "" | ||
? `${url.pathname}.js` | ||
: url.pathname; | ||
|
||
return await fs.readFile(pathToFileURL(filePath), "utf-8"); | ||
} | ||
|
||
if (url.protocol === "https:") { | ||
const response = await fetch(id); | ||
return await response.text(); | ||
} | ||
} catch (err) { | ||
throw new JspmError( | ||
`\n Unsupported protocol ${id} \n ${err.message} \n` | ||
); | ||
} | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.