Skip to content

Commit

Permalink
Fix unable to use node builtins in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 28, 2021
1 parent 1dd733b commit 9cce548
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-ways-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Fix unable to load node builtins in config file
12 changes: 10 additions & 2 deletions packages/wmr/src/lib/compile-single-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion packages/wmr/src/lib/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions packages/wmr/test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/config-node-builtins/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello world</h1>
10 changes: 10 additions & 0 deletions packages/wmr/test/fixtures/config-node-builtins/wmr.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from 'path';
import { pluginA } from './wmr/a.mjs';

export default function wmr() {
// Random path usage to check if it works
const random = path.join('foo', 'bar').split(path.sep).join(path.posix.sep);
return {
plugins: [{ name: random }, pluginA()]
};
}
5 changes: 5 additions & 0 deletions packages/wmr/test/fixtures/config-node-builtins/wmr/a.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function pluginA() {
return {
name: 'plugin-A'
};
}

0 comments on commit 9cce548

Please sign in to comment.