Skip to content

Commit

Permalink
fix: unable to use module type in package.json (#6199)
Browse files Browse the repository at this point in the history
Fixes #6130
Fixes #3601

Similar to our usage of peer dependencies, emitting .cjs instead of .js better aligns with expectations of js runtimes. While most will work with .js in most cases, we are currently explicitly emitting commonjs modules (using `exports` and `require`) so why not signal that to the runtime to remove any confusion?

~TODO~
- [x] Verify e2e
- [x] Add test

Misc:
- Updated bundling to target node 20, since that's the minimum supported version
- `wing pack` now packs all js/ts variants

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
MarkMcCulloh authored Apr 10, 2024
1 parent 8ea635b commit ec229e9
Show file tree
Hide file tree
Showing 282 changed files with 3,004 additions and 2,944 deletions.
30 changes: 16 additions & 14 deletions apps/wing/src/commands/pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,34 @@ describe("wing pack", () => {

expect(Object.keys(tarballContents).sort()).toMatchInlineSnapshot(`
[
"$lib/.wing/inflight.Store-2.js",
"$lib/.wing/inflight.Store-2.js.map",
"$lib/.wing/inflight.Util-1.js",
"$lib/.wing/inflight.Util-1.js.map",
"$lib/.wing/inflight.Store-2.cjs",
"$lib/.wing/inflight.Store-2.cjs.map",
"$lib/.wing/inflight.Util-1.cjs",
"$lib/.wing/inflight.Util-1.cjs.map",
"$lib/.wing/preflight.cjs",
"$lib/.wing/preflight.cjs.map",
"$lib/.wing/preflight.d.ts",
"$lib/.wing/preflight.enums-1.cjs",
"$lib/.wing/preflight.enums-1.cjs.map",
"$lib/.wing/preflight.enums-1.d.ts",
"$lib/.wing/preflight.enums-1.js",
"$lib/.wing/preflight.enums-1.js.map",
"$lib/.wing/preflight.js",
"$lib/.wing/preflight.js.map",
"$lib/.wing/preflight.store-3.cjs",
"$lib/.wing/preflight.store-3.cjs.map",
"$lib/.wing/preflight.store-3.d.ts",
"$lib/.wing/preflight.store-3.js",
"$lib/.wing/preflight.store-3.js.map",
"$lib/.wing/preflight.subdir-4.cjs",
"$lib/.wing/preflight.subdir-4.cjs.map",
"$lib/.wing/preflight.subdir-4.d.ts",
"$lib/.wing/preflight.subdir-4.js",
"$lib/.wing/preflight.subdir-4.js.map",
"$lib/.wing/preflight.util-2.cjs",
"$lib/.wing/preflight.util-2.cjs.map",
"$lib/.wing/preflight.util-2.d.ts",
"$lib/.wing/preflight.util-2.js",
"$lib/.wing/preflight.util-2.js.map",
"LICENSE",
"README.md",
"enums.w",
"package.json",
"store.w",
"subdir/util.w",
"util.extern.d.ts",
"util.js",
"util.ts",
]
`);

Expand Down
7 changes: 6 additions & 1 deletion apps/wing/src/commands/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { compile } from "./compile";

const defaultGlobs = [
"**/*.js",
"**/*.cjs",
"**/*.mjs",
"**/*.ts",
"**/*.cts",
"**/*.mts",
"**/*.w",
"README*",
"LICENSE*",
Expand Down Expand Up @@ -133,7 +138,7 @@ export async function pack(options: PackageOptions = {}): Promise<string> {
}
}
pkgJson.files = [...pkgJsonFiles];
pkgJson.main = path.join(compilerOutputFolder, dotWingDir, "preflight.js");
pkgJson.main = path.join(compilerOutputFolder, dotWingDir, "preflight.cjs");

// add "winglang" to "keywords"
const keywords = new Set(pkgJson.keywords ?? []);
Expand Down
2 changes: 1 addition & 1 deletion apps/wingcli-v2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn run_javascript_node(source_file: &Utf8Path, target_dir: &Utf8Path, target: Ta
let source_dir = source_file_canonical.parent().expect("source file has no parent");

let mut command = std::process::Command::new("node");
command.arg(target_dir.join(".wing").join("preflight.js"));
command.arg(target_dir.join(".wing").join("preflight.cjs"));
command.env("NODE_PATH", WING_CACHE_DIR.join("node_modules").as_str());
command.env("WING_PLATFORMS", target.to_string());
command.env("WING_SOURCE_DIR", source_dir);
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/expect/assert.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test "equal sets" {
// FIXME: Doesn't work
// ERROR: unable to serialize immutable data object of type Datetime

// examples/tests/sdk_tests/expect/target/test/assert.main.wsim.256800.tmp/.wing/preflight.js:319
// examples/tests/sdk_tests/expect/target/test/assert.main.wsim.256800.tmp/.wing/preflight.cjs:319

// let dt = datetime.fromIso("2023-07-18T20:18:25.177+03:00");

Expand Down
3 changes: 3 additions & 0 deletions examples/tests/valid/esm/esm_extern.extern.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface extern {
exampleInflight: () => Promise<void>,
}
1 change: 1 addition & 0 deletions examples/tests/valid/esm/esm_extern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const exampleInflight = async () => {};
10 changes: 10 additions & 0 deletions examples/tests/valid/esm/module_type.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bring cloud;

class Extern {
extern "./esm_extern.js"
pub static inflight exampleInflight();
}

test "run extern" {
Extern.exampleInflight();
}
7 changes: 7 additions & 0 deletions examples/tests/valid/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "examples-valid-esm",
"type": "module",
"volta": {
"extends": "../../../package.json"
}
}
2 changes: 1 addition & 1 deletion examples/wing-fixture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"engines": {
"node": ">=v20.0.0"
},
"main": "target/wing-fixture.wsim/.wing/preflight.js",
"main": "target/wing-fixture.wsim/.wing/preflight.cjs",
"repository": {
"type": "git",
"url": "git+https://github.com/winglang/wing.git"
Expand Down
14 changes: 8 additions & 6 deletions libs/wingc/src/dtsify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod extern_dtsify;
pub const TYPE_INFLIGHT_POSTFIX: &str = "$Inflight";
const TYPE_INTERNAL_NAMESPACE: &str = "$internal";
const TYPE_STD: &str = "std";
const EMIT_FILE_EXTENSION: &str = ".cjs";

pub struct DTSifier<'a> {
preflight_file_map: &'a IndexMap<Utf8PathBuf, String>,
Expand Down Expand Up @@ -62,9 +63,10 @@ impl<'a> DTSifier<'a> {
}

let mut dts_file_name = self.preflight_file_map.get(source_path).unwrap().clone();
assert!(dts_file_name.ends_with(".js"));

dts_file_name.replace_range((dts_file_name.len() - 3).., ".d.ts");
assert!(dts_file_name.ends_with(EMIT_FILE_EXTENSION));

dts_file_name.replace_range((dts_file_name.len() - EMIT_FILE_EXTENSION.len()).., ".d.ts");

match self.output_files.borrow_mut().add_file(dts_file_name, dts.to_string()) {
Ok(()) => {}
Expand Down Expand Up @@ -290,20 +292,20 @@ impl<'a> DTSifier<'a> {
match source {
BringSource::BuiltinModule(sym) => code.line(format!("import {{ {sym} }} from \"{WINGSDK_ASSEMBLY_NAME}\"")),
BringSource::TrustedModule(sym, path) => {
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".js", "");
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".cjs", "");
code.line(format!("import * as {sym} from \"./{preflight_file_name}\";"))
}
BringSource::WingLibrary(sym, path) => {
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".js", "");
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".cjs", "");
code.line(format!("import * as {sym} from \"./{preflight_file_name}\";"))
}
BringSource::JsiiModule(sym) => code.line(format!("import * as {identifier} from \"{sym}\"")),
BringSource::WingFile(path) => {
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".js", "");
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".cjs", "");
code.line(format!("import * as {identifier} from \"./{preflight_file_name}\";"))
}
BringSource::Directory(path) => {
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".js", "");
let preflight_file_name = self.preflight_file_map.get(path).unwrap().replace(".cjs", "");
code.line(format!("import * as {identifier} from \"./{preflight_file_name}\";"))
}
}
Expand Down
128 changes: 64 additions & 64 deletions libs/wingc/src/dtsify/snapshots/declarations.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub class Child extends ParentClass impl ClassInterface {
}
```
## inflight.Child-1.js
## inflight.Child-1.cjs
```js
"use strict";
Expand All @@ -48,10 +48,10 @@ module.exports = function({ $ParentClass }) {
}
return Child;
}
//# sourceMappingURL=inflight.Child-1.js.map
//# sourceMappingURL=inflight.Child-1.cjs.map
```
## inflight.ParentClass-1.js
## inflight.ParentClass-1.cjs
```js
"use strict";
Expand All @@ -69,80 +69,29 @@ module.exports = function({ }) {
}
return ParentClass;
}
//# sourceMappingURL=inflight.ParentClass-1.js.map
//# sourceMappingURL=inflight.ParentClass-1.cjs.map
```
## preflight.d.ts
```js
export * from "./preflight.lib-1.js"
```
## preflight.js
## preflight.cjs
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const std = $stdlib.std;
const $helpers = $stdlib.helpers;
module.exports = {
...require("./preflight.lib-1.js"),
...require("./preflight.lib-1.cjs"),
};
//# sourceMappingURL=preflight.js.map
//# sourceMappingURL=preflight.cjs.map
```
## preflight.lib-1.d.ts
## preflight.d.ts
```js
import * as $internal from "@winglang/sdk/lib/core/types"
import { std } from "@winglang/sdk"
export interface Struct {
readonly n: number;
readonly d: (readonly (std.Duration)[]);
readonly j: Readonly<$internal.Json>;
}
export interface Interface
{
readonly method: (s: Struct) => string;
}
export interface Interface$Inflight
{
readonly inflightMethod: () => Promise<string>;
}
export interface ClassInterface
{
readonly addHandler: (handler: $internal.Inflight<(arg0: string) => Promise<string>>) => void;
}
export interface ClassInterface$Inflight
{
readonly bar: () => Promise<void>;
}
export class ParentClass extends std.Resource implements ClassInterface
{
constructor(scope: $internal.Construct, id: string);
[$internal.INFLIGHT_SYMBOL]?: ParentClass$Inflight;
_supportedOps(): $internal.OperationsOf<ParentClass$Inflight>;
addHandler: (handler: $internal.Inflight<(arg0: string) => Promise<string>>) => void;
}
export class ParentClass$Inflight implements ClassInterface$Inflight
{
constructor();
static static_method: () => Promise<void>;
bar: () => Promise<void>;
}
export class Child extends ParentClass implements ClassInterface
{
constructor(scope: $internal.Construct, id: string);
[$internal.INFLIGHT_SYMBOL]?: Child$Inflight;
_supportedOps(): $internal.OperationsOf<Child$Inflight>;
}
export class Child$Inflight extends ParentClass$Inflight implements ClassInterface$Inflight
{
constructor();
}
export * from "./preflight.lib-1.cjs"
```
## preflight.lib-1.js
## preflight.lib-1.cjs
```js
"use strict";
Expand All @@ -157,7 +106,7 @@ class ParentClass extends $stdlib.std.Resource {
}
static _toInflightType() {
return `
require("${$helpers.normalPath(__dirname)}/inflight.ParentClass-1.js")({
require("${$helpers.normalPath(__dirname)}/inflight.ParentClass-1.cjs")({
})
`;
}
Expand Down Expand Up @@ -195,7 +144,7 @@ class Child extends ParentClass {
}
static _toInflightType() {
return `
require("${$helpers.normalPath(__dirname)}/inflight.Child-1.js")({
require("${$helpers.normalPath(__dirname)}/inflight.Child-1.cjs")({
$ParentClass: ${$stdlib.core.liftObject(ParentClass)},
})
`;
Expand All @@ -219,6 +168,57 @@ class Child extends ParentClass {
}
}
module.exports = { ParentClass, Child };
//# sourceMappingURL=preflight.lib-1.js.map
//# sourceMappingURL=preflight.lib-1.cjs.map
```
## preflight.lib-1.d.ts
```js
import * as $internal from "@winglang/sdk/lib/core/types"
import { std } from "@winglang/sdk"
export interface Struct {
readonly n: number;
readonly d: (readonly (std.Duration)[]);
readonly j: Readonly<$internal.Json>;
}
export interface Interface
{
readonly method: (s: Struct) => string;
}
export interface Interface$Inflight
{
readonly inflightMethod: () => Promise<string>;
}
export interface ClassInterface
{
readonly addHandler: (handler: $internal.Inflight<(arg0: string) => Promise<string>>) => void;
}
export interface ClassInterface$Inflight
{
readonly bar: () => Promise<void>;
}
export class ParentClass extends std.Resource implements ClassInterface
{
constructor(scope: $internal.Construct, id: string);
[$internal.INFLIGHT_SYMBOL]?: ParentClass$Inflight;
_supportedOps(): $internal.OperationsOf<ParentClass$Inflight>;
addHandler: (handler: $internal.Inflight<(arg0: string) => Promise<string>>) => void;
}
export class ParentClass$Inflight implements ClassInterface$Inflight
{
constructor();
static static_method: () => Promise<void>;
bar: () => Promise<void>;
}
export class Child extends ParentClass implements ClassInterface
{
constructor(scope: $internal.Construct, id: string);
[$internal.INFLIGHT_SYMBOL]?: Child$Inflight;
_supportedOps(): $internal.OperationsOf<Child$Inflight>;
}
export class Child$Inflight extends ParentClass$Inflight implements ClassInterface$Inflight
{
constructor();
}
```
Loading

0 comments on commit ec229e9

Please sign in to comment.