Skip to content

Commit

Permalink
plugins can export importLoader for use with --import
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Sep 10, 2023
1 parent 2d87a7f commit 5ee2ddd
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"typedoc": "typedoc --tsconfig tsconfig/esm.json ./src/*.ts"
},
"tap": {
"typecheck": false,
"coverage-map": "map.js"
},
"peerDependencies": {
Expand Down
56 changes: 54 additions & 2 deletions src/test/scripts/build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ const seen = new Set<string>()
// raise an error and do not continue.
const hasConfig = new Map<string, string>()
const hasPlugin = new Map<string, string>()

// has a loader, and no importLoader
const hasLoader = new Map<string, string>()
// has an importLoader
const hasImport = new Map<string, string>()
// the loader export, if an importLoader is present
const hasLoaderFallback = new Map<string, string>()

const preloaders = new Map<string, string>()
const preimports = new Map<string, string>()
const testFileExtensions = new Set<string>()

const signature = plugins
Expand Down Expand Up @@ -85,6 +93,7 @@ const pluginNames = plugins.map(p => {
>
}
loader?: string
importLoader?: string
preload?: boolean
testFileExtensions?: string[]
}
Expand Down Expand Up @@ -128,9 +137,15 @@ const pluginNames = plugins.map(p => {
hasPlugin.set(p, name)
isPlugin = true
}
if (typeof imp.importLoader === 'string') {
if (imp.preload === true) preimports.set(p, imp.importLoader)
hasImport.set(p, imp.importLoader)
isPlugin = true
}
if (typeof imp.loader === 'string') {
hasLoader.set(p, imp.loader)
if (imp.preload === true) preloaders.set(p, imp.loader)
if (!imp.importLoader) hasLoader.set(p, imp.loader)
else hasLoaderFallback.set(p, imp.loader)
isPlugin = true
}
if (!isPlugin) {
Expand Down Expand Up @@ -243,6 +258,12 @@ const pluginLoaders = `const preloaders = new Set<string>(${JSON.stringify(
2
)})
const preimports = new Set<string>(${JSON.stringify(
[...preimports.values()],
null,
2
)})
/**
* The set of \`loader\` strings exported by plugins. If a plugin exports
* \`preload = true\`, then it will be sorted to the start of this list, so
Expand All @@ -253,8 +274,39 @@ export const loaders: string[] = ${JSON.stringify(
null,
2
)}.sort(
(a, b) => preloaders.has(a) ? -1 : preloaders.has(b) ? 1 : 0
(a, b) => preloaders.has(a) && !preloaders.has(b) ? -1
: !preloaders.has(a) && preloaders.has(b) ? 1
: 0
)
/**
* The set of \`importLoader\` strings exported by plugins, for use with
* \`Module.register\` in node v20.6 and higher.
*/
export const importLoaders: string[] = ${JSON.stringify(
[...hasImport.values()],
null,
2
)}.sort(
(a, b) => preimports.has(a) && !preimports.has(b) ? -1
: !preimports.has(a) && preimports.has(b) ? 1
: 0
)
/**
* All \`loader\` strings exported by plugins, including fallbacks provided
* for those that also export an \`importLoader\`
*/
export const loaderFallbacks: string[] = ${JSON.stringify(
[...hasLoaderFallback.values()].concat([...hasLoader.values()]),
null,
2
)}.sort(
(a, b) => preloaders.has(a) && !preloaders.has(b) ? -1
: !preloaders.has(a) && preloaders.has(b) ? 1
: 0
)
`

const testFileExtensionsCode = [...testFileExtensions]
Expand Down
5 changes: 5 additions & 0 deletions src/test/scripts/test-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ c
//{{PLUGINS CONFIG END}}

//{{LOADERS START}}
// these are always added with --loader
export const loaders = []
// these are added with --import, if available
export const importLoaders = []
// these are added with --loader, only if --import is unavailable
export const loaderFallbacks = []
//{{LOADERS END}}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/test/test-built/dist/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ export declare const config: <C extends ConfigSet>(jack: Jack<C>) => Jack<C & im
* that Node loads it before other loaders.
*/
export declare const loaders: string[];
/**
* The set of `importLoader` strings exported by plugins, for use with
* `Module.register` in node v20.6 and higher.
*/
export declare const importLoaders: string[];
/**
* All `loader` strings exported by plugins, including fallbacks provided
* for those that also export an `importLoader`
*/
export declare const loaderFallbacks: string[];
/**
* The string signature that lists all loaded plugins alphabetically, used
* to determine whether a rebuild is necessary by comparing it to the `plugin`
Expand Down
2 changes: 1 addition & 1 deletion src/test/test-built/dist/cjs/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions src/test/test-built/dist/cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/test/test-built/dist/cjs/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/test/test-built/dist/mjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ export declare const config: <C extends ConfigSet>(jack: Jack<C>) => Jack<C & im
* that Node loads it before other loaders.
*/
export declare const loaders: string[];
/**
* The set of `importLoader` strings exported by plugins, for use with
* `Module.register` in node v20.6 and higher.
*/
export declare const importLoaders: string[];
/**
* All `loader` strings exported by plugins, including fallbacks provided
* for those that also export an `importLoader`
*/
export declare const loaderFallbacks: string[];
/**
* The string signature that lists all loaded plugins alphabetically, used
* to determine whether a rebuild is necessary by comparing it to the `plugin`
Expand Down

0 comments on commit 5ee2ddd

Please sign in to comment.