Skip to content

Commit

Permalink
Change autoCdnUrl to micropipCdnUrl
Browse files Browse the repository at this point in the history
Change due to pre-release and open issue #4 noting that users may
want to change the url. Having the value be a boolean would mean
setting the url would require a third option.

Opted to set the value as a string instead and default to the
release url. If users want to change it they can set it to whatever
url they would like.
  • Loading branch information
mneil committed Sep 24, 2022
1 parent 647beac commit 6b2a2b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ module.exports = {

## Options

- [autoCdnIndexUrl](#autoCdnIndexUrl)
- [micropipCdnUrl](#micropipCdnUrl)
- [globalLoadPyodide](#globalLoadPyodide)

### autoCdnIndexUrl
### micropipCdnUrl

Type: `boolean`\
Default: `true`\
_Description_: Automatically use the pyodide CDN endpoint for micropip packages. Setting this value to false means you must download micropip and related packages yourself and serve them. This option differs from [loadPyodide indexUrl](https://pyodide.org/en/stable/usage/api/js-api.html) in that it only impacts micropip and pip packages and _does not_ affect the location the main pyodide runtime location.
Type: `string`\
Default: `https://cdn.jsdelivr.net/pyodide/v${installedPyodideVersion}/full/`\
Required: false\
_Description_: CDN endpoint for micropip packages. This option differs from [loadPyodide indexUrl](https://pyodide.org/en/stable/usage/api/js-api.html) in that it only impacts micropip and pip packages and _does not_ affect the location the main pyodide runtime location.

### globalLoadPyodide

Type: `boolean`\
Default: `false`\
Required: false\
_Description_:Whether or not to expose loadPyodide method globally. A globalThis.loadPyodide is useful when using pyodide as a standalone script or in certain frameworks. With webpack we can scope the pyodide package locally to prevent leaks (default).

## Contributing
Expand Down
16 changes: 8 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import * as patterns from "./lib/patterns";

interface PyodideOptions extends Partial<CopyPlugin.PluginOptions> {
/**
* Automatically use the pyodide CDN endpoint for micropip packages.
* Setting this value to false means you must download micropip and
* related packages yourself and serve them. This option differs from
* CDN endpoint for micropip packages
* This option differs from
* [loadPyodide indexUrl](https://pyodide.org/en/stable/usage/api/js-api.html)
* in that it only impacts micropip and pip packages and _does not_ affect
* the location the main pyodide runtime location.
* the location the main pyodide runtime location. Set this value to "" if you want to keep
* the pyodide default of accepting the indexUrl.
*
* @default true
* @default https://cdn.jsdelivr.net/pyodide/v${installedPyodideVersion}/full/
*/
autoCdnIndexUrl?: boolean;
micropipCdnUrl?: string;
/**
* Whether or not to expose loadPyodide method globally. A globalThis.loadPyodide is useful when
* using pyodide as a standalone script or in certain frameworks. With webpack we can scope the
Expand All @@ -42,15 +42,15 @@ export class PyodidePlugin extends CopyPlugin {
const outRoot = "pyodide";
const globalLoadPyodide = options.globalLoadPyodide || false;
const pkg = __non_webpack_require__(path.resolve(PyodidePlugin.pyodidePackagePath, "package.json"));
options.patterns = patterns.chooseAndTransform(pkg.version, options.autoCdnIndexUrl).map((pattern) => {
options.patterns = patterns.chooseAndTransform(pkg.version, options.micropipCdnUrl).map((pattern) => {
return {
from: path.resolve(PyodidePlugin.pyodidePackagePath, pattern.from),
to: path.join(outRoot, pattern.to),
transform: pattern.transform,
};
});
assert.ok(options.patterns.length > 0, `Unsupported version of pyodide. Must use >=${patterns.versions[0]}`);
delete options.autoCdnIndexUrl;
delete options.micropipCdnUrl;
delete options.globalLoadPyodide;
super(options as Required<PyodideOptions>);
this.globalLoadPyodide = globalLoadPyodide;
Expand Down
16 changes: 7 additions & 9 deletions lib/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,26 @@ export function choose(version = "0.0.0"): string[] {
* that is great enough an empty array is returned.
* @param version
* @param pattern
* @param autoIndexUrl
* @param micropipCdnUrl
* @returns {PyodideObjectPattern[]}
*/
export function transform(version: string, pattern: string[], autoIndexUrl): PyodideObjectPattern[] {
export function transform(version: string, pattern: string[], micropipCdnUrl): PyodideObjectPattern[] {
return pattern.map((name) => {
let transform: Transform | undefined;
if (autoIndexUrl && name == "pyodide.asm.js") {
if (micropipCdnUrl && name == "pyodide.asm.js") {
transform = {
transformer: (input) => {
return input
.toString()
.replace(
"resolvePath(file_name,API.config.indexURL)",
`resolvePath(file_name,'https://cdn.jsdelivr.net/pyodide/v${version}/full/')`
);
.replace("resolvePath(file_name,API.config.indexURL)", `resolvePath(file_name,${micropipCdnUrl})`);
},
};
}
return { from: name, to: name, transform };
});
}

export function chooseAndTransform(version = "0.0.0", autoIndexUrl = true) {
return transform(version, choose(version), autoIndexUrl);
export function chooseAndTransform(version = "0.0.0", micropipCdnUrl?: string) {
micropipCdnUrl = micropipCdnUrl ?? `https://cdn.jsdelivr.net/pyodide/v${version}/full/`;
return transform(version, choose(version), micropipCdnUrl);
}

0 comments on commit 6b2a2b3

Please sign in to comment.