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 node: imports #476

Merged
merged 2 commits into from Jul 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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