Skip to content

Commit

Permalink
Fix node: imports (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Jul 14, 2022
1 parent 4ce8cfc commit 3930466
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-bikes-smash.md
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": patch
---

Using `node:` to import Node builtins no longer triggers a "package is not specified in dependencies or peerDependencies" error
42 changes: 42 additions & 0 deletions packages/cli/src/build/__tests__/basic.ts
Expand Up @@ -1155,3 +1155,45 @@ test("bigint usage", async () => {
`);
});

test("node: is external", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "@scope/test",
main: "dist/scope-test.cjs.js",
module: "dist/scope-test.esm.js",
}),
"src/index.js": ts`
import fs from "node:fs";
fs.writeFileSync("test.txt", "test");
`,
});
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/scope-test.cjs.dev.js, dist/scope-test.cjs.prod.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
'use strict';
var fs = require('node:fs');
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
var fs__default = /*#__PURE__*/_interopDefault(fs);
fs__default['default'].writeFileSync("test.txt", "test");
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/scope-test.cjs.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./scope-test.cjs.prod.js");
} else {
module.exports = require("./scope-test.cjs.dev.js");
}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/scope-test.esm.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
import fs from 'node:fs';
fs.writeFileSync("test.txt", "test");
`);
});
11 changes: 9 additions & 2 deletions packages/cli/src/build/rollup.ts
Expand Up @@ -22,8 +22,10 @@ import { EXTENSIONS } from "../constants";
import { inlineProcessEnvNodeEnv } from "../rollup-plugins/inline-process-env-node-env";
import normalizePath from "normalize-path";

type ExternalPredicate = (source: string) => boolean;

// this makes sure nested imports of external packages are external
const makeExternalPredicate = (externalArr: string[]) => {
const makeExternalPredicate = (externalArr: string[]): ExternalPredicate => {
if (externalArr.length === 0) {
return () => false;
}
Expand Down Expand Up @@ -53,8 +55,13 @@ export let getRollupConfig = (
external.push(...Object.keys(pkg.json.dependencies));
}

let wrapExternalPredicate = (inner: ExternalPredicate): ExternalPredicate =>
inner;

if (type === "node-dev" || type === "node-prod") {
external.push(...builtInModules);
wrapExternalPredicate = (inner) => (source) =>
source.startsWith("node:") || inner(source);
}

let input: Record<string, string> = {};
Expand All @@ -76,7 +83,7 @@ export let getRollupConfig = (

const config: RollupOptions = {
input,
external: makeExternalPredicate(external),
external: wrapExternalPredicate(makeExternalPredicate(external)),
onwarn: (warning) => {
if (typeof warning === "string") {
warnings.push(
Expand Down

0 comments on commit 3930466

Please sign in to comment.