Skip to content

Commit 87b2ca9

Browse files
Specify custom npm registry via --registry
1 parent 5180df1 commit 87b2ca9

File tree

8 files changed

+29
-10
lines changed

8 files changed

+29
-10
lines changed

packages/wmr/src/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ prog
3535
.option('--visualize', 'Launch interactive bundle visualizer')
3636
.option('--minify', 'Enable minification of generated code (default: true)', true)
3737
.option('--autoInstall', 'Fetch missing npm packages from npm registry automatically (default: false')
38+
.option('--registry', 'NPM registry url to fetch if "--autoInstall" is set (default: https://registry.npmjs.org)')
3839
.action(opts => {
3940
opts.minify = bool(opts.minify);
4041
run(build(opts));
@@ -62,6 +63,7 @@ prog
6263
.option('--profile', 'Generate build statistics')
6364
.option('--reload', 'Switch off hmr and reload on file saves')
6465
.option('--autoInstall', 'Fetch missing npm packages from npm registry automatically (default: false')
66+
.option('--registry', 'NPM registry url to fetch if "--autoInstall" is set (default: https://registry.npmjs.org)')
6567
.action(opts => {
6668
opts.optimize = !/false|0/.test(opts.compress);
6769
opts.compress = bool(opts.compress);

packages/wmr/src/lib/normalize-options.js

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export async function normalizeOptions(options, mode, configWatchFiles = []) {
2525
options.features = { preact: true };
2626
options.alias = options.alias || options.aliases || {};
2727
options.customRoutes = options.customRoutes || [];
28+
options.registry = options.registry || 'https://registry.npmjs.org';
2829

2930
// `wmr` / `wmr start` is a development command.
3031
// `wmr build` / `wmr serve` are production commands.

packages/wmr/src/lib/plugins.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export function getPlugins(options) {
4646
sourcemap,
4747
features,
4848
visualize,
49-
autoInstall
49+
autoInstall,
50+
registry
5051
} = options;
5152

5253
// Plugins are pre-sorted
@@ -99,7 +100,7 @@ export function getPlugins(options) {
99100
// Only transpile CommonJS in node_modules and explicit .cjs files:
100101
include: /(^npm\/|[/\\]node_modules[/\\]|\.cjs$)/
101102
}),
102-
npmPlugin({ cwd, autoInstall, production }),
103+
npmPlugin({ cwd, autoInstall, production, registryUrl: registry }),
103104
resolveExtensionsPlugin({
104105
extensions: ['.ts', '.tsx', '.js', '.cjs'],
105106
index: true

packages/wmr/src/plugins/npm-plugin/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const log = debug('npm', 196);
1313
* @param {string} options.cwd
1414
* @param {boolean} options.autoInstall
1515
* @param {boolean} options.production
16+
* @param {string} options.registryUrl
1617
* @returns {import('rollup').Plugin}
1718
*/
18-
export function npmPlugin({ cwd, autoInstall, production }) {
19+
export function npmPlugin({ cwd, autoInstall, production, registryUrl }) {
1920
const PREFIX = '\0npm:';
2021

2122
const cacheDir = path.join(cwd, '.cache', '@npm');
@@ -52,7 +53,7 @@ export function npmPlugin({ cwd, autoInstall, production }) {
5253
}
5354

5455
log(kl.dim(`bundle: `) + kl.cyan(id));
55-
let result = await npmBundle(id, { autoInstall, production, cacheDir, cwd, resolutionCache });
56+
let result = await npmBundle(id, { autoInstall, production, cacheDir, cwd, resolutionCache, registryUrl });
5657

5758
await Promise.all(
5859
result.output.map(async chunkOrAsset => {

packages/wmr/src/plugins/npm-plugin/npm-auto-install.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,10 @@ async function parseTarball(bodyStream, onFile, { include, exclude }) {
9696
* prototyping.
9797
* @param {object} options
9898
* @param {string} options.cacheDir
99+
* @param {string} options.registryUrl
99100
* @returns {import('rollup').Plugin}
100101
*/
101-
export function npmAutoInstall({ cacheDir }) {
102-
// TODO: Detect from `.npmrc`
103-
const registryUrl = 'https://registry.npmjs.org';
104-
102+
export function npmAutoInstall({ cacheDir, registryUrl }) {
105103
/** Files that should always be ignored when storing packages */
106104
const FILES_EXCLUDE = /([._-]test\.|__tests?|\/tests?\/|\/node_modules\/|\.d\.ts$)/i;
107105

packages/wmr/src/plugins/npm-plugin/npm-bundle.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ function customWarn(warning) {
2929
* @param {boolean} options.production
3030
* @param {string} options.cacheDir
3131
* @param {string} options.cwd
32+
* @param {string} options.registryUrl
3233
* @param {Map<string, string>} options.resolutionCache
3334
*/
34-
export async function npmBundle(requestId, { autoInstall, production, cacheDir, cwd, resolutionCache }) {
35+
export async function npmBundle(requestId, { autoInstall, production, cacheDir, cwd, resolutionCache, registryUrl }) {
3536
const meta = getPackageInfo(requestId);
3637
const pkgName = meta.name;
3738

@@ -46,7 +47,7 @@ export async function npmBundle(requestId, { autoInstall, production, cacheDir,
4647
browserFieldPlugin({ browserReplacement }),
4748
npmExternalDeps({ requestId }),
4849
!process.env.DISABLE_LOCAL_NPM && npmLocalPackage({ root: cwd }),
49-
autoInstall && npmAutoInstall({ cacheDir }),
50+
autoInstall && npmAutoInstall({ cacheDir, registryUrl }),
5051
npmLoad({ browserReplacement, resolutionCache }),
5152
jsonPlugin({ root: cwd }),
5253
commonjsPlugin({ production }),

packages/wmr/test/npm.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
serveStatic,
88
setupTest,
99
teardown,
10+
waitForMessage,
1011
waitForPass,
1112
withLog
1213
} from './test-helpers.js';
@@ -267,6 +268,16 @@ describe('node modules', () => {
267268
});
268269
});
269270

271+
// eslint-disable-next-line jest/expect-expect
272+
it('should fetch package from --registry', async () => {
273+
await loadFixture('npm-auto-install-version', env);
274+
instance = await runWmrFast(env.tmp.path, '--autoInstall', '--registry', 'https://example.com', {
275+
env: { DISABLE_LOCAL_NPM: true }
276+
});
277+
await getOutput(env, instance);
278+
await waitForMessage(instance.output, /500.*https:\/\/example\.com\/smoldash/);
279+
});
280+
270281
it('should load CSS from installed package', async () => {
271282
await loadFixture('npm-auto-install-css', env);
272283
instance = await runWmrFast(env.tmp.path, '--autoInstall', { env: { DISABLE_LOCAL_NPM: true } });

packages/wmr/types.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ declare module 'wmr' {
8484
* This is only intended for quick prototyping.
8585
*/
8686
autoInstall: boolean;
87+
/**
88+
* NPM registry url to use if `--autoInstall` is enabled
89+
*/
90+
registry: string;
8791
}
8892

8993
export type BuildError = RollupError & { clientMessage?: string };

0 commit comments

Comments
 (0)