Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unable to use node builtins in config file #482

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -77,7 +77,7 @@ export async function normalizeOptions(options, mode) {
// 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
11 changes: 11 additions & 0 deletions packages/wmr/test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,17 @@ describe('fixtures', () => {
});
});

describe('config', () => {
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', () => {
it('should order by plugin.enforce value', async () => {
await loadFixture('plugin-enforce', env);
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'
};
}