diff --git a/.changeset/quiet-ways-attend.md b/.changeset/quiet-ways-attend.md new file mode 100644 index 000000000..07d0e34a5 --- /dev/null +++ b/.changeset/quiet-ways-attend.md @@ -0,0 +1,5 @@ +--- +'wmr': patch +--- + +Fix unable to load node builtins in config file diff --git a/packages/wmr/src/lib/compile-single-module.js b/packages/wmr/src/lib/compile-single-module.js index 722ee33eb..df63cf33f 100644 --- a/packages/wmr/src/lib/compile-single-module.js +++ b/packages/wmr/src/lib/compile-single-module.js @@ -20,7 +20,15 @@ const ROLLUP_FAST_OUTPUT = { // @TODO: this cache needs to be sharded by `input` in order to actually do anything when `preserveModules:true` is enabled let cache; -export const compileSingleModule = withCache(async (input, { cwd, out, hmr = true }) => { +/** + * @param {string} input + * @param {object} options + * @param {string} options.cwd + * @param {string} options.out + * @param {boolean} [options.hmr] + * @param {boolean} [options.rewriteNodeImports] + */ +export const compileSingleModule = withCache(async (input, { cwd, out, hmr = true, rewriteNodeImports = true }) => { input = input.replace(/\.css\.js$/, '.css'); // console.log('compiling ' + input); const bundle = await rollup.rollup({ @@ -41,7 +49,7 @@ export const compileSingleModule = withCache(async (input, { cwd, out, hmr = tru // hmr client if (id === 'wmr') id = '/_wmr.js'; // bare specifier = npm dep - else if (!/^\.?\.?(\/|$)/.test(id)) id = `/@npm/${id}`; + else if (rewriteNodeImports && !/^\.?\.?(\/|$)/.test(id)) id = `/@npm/${id}`; // relative imports (css) else if (/\.css$/.test(id)) id += '.js'; return { id, external: true, moduleSideEffects: true }; diff --git a/packages/wmr/src/lib/normalize-options.js b/packages/wmr/src/lib/normalize-options.js index b376145e6..7a66cf93b 100644 --- a/packages/wmr/src/lib/normalize-options.js +++ b/packages/wmr/src/lib/normalize-options.js @@ -89,7 +89,7 @@ export async function normalizeOptions(options, mode, configWatchFiles = []) { // Create a temporary file to write compiled output into // TODO: Do this in memory configFile = resolve(options.root, 'wmr.config.js'); - await compileSingleModule(file, { cwd: options.cwd, out: resolve('.'), hmr: false }); + await compileSingleModule(file, { cwd: options.cwd, out: resolve('.'), hmr: false, rewriteNodeImports: false }); } const fileUrl = url.pathToFileURL(configFile); diff --git a/packages/wmr/test/fixtures.test.js b/packages/wmr/test/fixtures.test.js index 190605047..f7cfbd2f3 100644 --- a/packages/wmr/test/fixtures.test.js +++ b/packages/wmr/test/fixtures.test.js @@ -639,6 +639,15 @@ describe('fixtures', () => { await waitForMessage(instance.output, /{ foo: 'bar' }/); expect(true).toEqual(true); // Silence linter }); + + it('should support loading node built-ins', async () => { + await loadFixture('config-node-builtins', env); + instance = await runWmrFast(env.tmp.path); + + await waitForMessage(instance.output, /foo\/bar/); + await waitForMessage(instance.output, /plugin-A/); + expect(true).toEqual(true); // Silence linter + }); }); describe('plugins', () => { diff --git a/packages/wmr/test/fixtures/config-node-builtins/index.html b/packages/wmr/test/fixtures/config-node-builtins/index.html new file mode 100644 index 000000000..159202e87 --- /dev/null +++ b/packages/wmr/test/fixtures/config-node-builtins/index.html @@ -0,0 +1 @@ +