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

feat(config): support async syncpack.config.mjs #164

Closed
mploquin opened this issue Sep 28, 2023 · 13 comments
Closed

feat(config): support async syncpack.config.mjs #164

mploquin opened this issue Sep 28, 2023 · 13 comments

Comments

@mploquin
Copy link

Help Needed

Hi,

I'm trying to fetch the content of another package and put it in the sortFirst option, but I'm stuck.

Is there a way to do that ? And Is it possible to use esm format for my config file ?

For example, something like this might work:

syncpack.config.js

import { sortOrder } from "sort-package-json";

export default {
  semverRange: "^",
  sortFirst: sortOrder
}

Thank's

@JamieMason
Copy link
Owner

Assuming sortOrder is an array of strings, at first glance it might be that you're using import/export, try this

const { sortOrder } = require("sort-package-json");

module.exports = {
  semverRange: "^",
  sortFirst: sortOrder
}

All this is handled by https://github.com/cosmiconfig/cosmiconfig so there should be more info there on whether there is a way to support import/export.

@mploquin
Copy link
Author

mploquin commented Sep 28, 2023

Thank you for your super fast response !!

I already tried this but I get an error:

// syncpack.config.cjs

const { sortOrder } = require("sort-package-json");

module.exports = {
  semverRange: "^",
  sortFirst: sortOrder
}
Your syncpack config file contains an error
  File: not known (discovered by cosmiconfig)
  Error: Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/monorepo/node_modules/sort-package-json/index.js from /path/to/monorepo/syncpack.config.cjs not supported.
Instead change the require of index.js in /path/to/monorepo/syncpack.config.cjs to a dynamic import() which is available in all CommonJS modules.

Any idea ?

@JamieMason
Copy link
Owner

Try renaming the file to syncpack.config.cjs. Looking at the cosmiconfig docs, it looks like your original attempt might work if you rename that to syncpack.config.mjs.

@mploquin
Copy link
Author

mploquin commented Sep 28, 2023

Sorry I didn't specify but my file was renamed to .cjs.

And when i rename to .mjs it seems that syncpack cannot find/load the config file :

// syncpack.config.mjs

import { sortOrder } from "sort-package-json";

export default {
  semverRange: "^",
  sortFirst: sortOrder
};
SYNCPACK_VERBOSE=true npx syncpack lint
? readConfigFileSync( undefined )
? no config file found

And when i specify the config file, i get an error :

SYNCPACK_VERBOSE=true npx syncpack lint -c syncpack.config.mjs 
? readConfigFileSync( syncpack.config.mjs )
! Your syncpack config file contains an error
  File: syncpack.config.mjs
  Error: Error: No loader specified for extension ".mjs"

@JamieMason
Copy link
Owner

1

Is sort-package-json properly installed? If you run this from the command line from the root of your repo:

node -e 'console.log(require("sort-package-json"))'

do you get this?

Error: Cannot find module 'sort-package-json'
2

if you take sort-package-json out of the picture completely for a moment, does it all work?

// syncpack.config.mjs

export default {
  semverRange: "^",
  sortFirst: ['version', 'name'],
};

@mploquin
Copy link
Author

  1. Throws an error, due to the fact that "sort-package-json" package only expose the esm format i think
node -e 'console.log(require("sort-package-json"))'
node:internal/modules/cjs/loader:1269
      throw err;
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/monorepo/node_modules/sort-package-json/index.js from /path/to/monorepo/[eval] not supported.
Instead change the require of index.js in /path/to/monorepo/[eval] to a dynamic import() which is available in all CommonJS modules.
    at [eval]:1:13
    at Script.runInThisContext (node:vm:129:12)
    at Object.runInThisContext (node:vm:307:38)
    at [eval]-wrapper:6:22 {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.13.0
  1. idem, not working same error
// syncpack.config.mjs

export default {
  semverRange: "^",
  sortFirst: ['version', 'name'],
};
SYNCPACK_VERBOSE=true npx syncpack lint            
? readConfigFileSync( undefined )
? no config file found
SYNCPACK_VERBOSE=true npx syncpack lint -c syncpack.config.mjs
? readConfigFileSync( syncpack.config.mjs )
! Your syncpack config file contains an error
  File: syncpack.config.mjs
  Error: Error: No loader specified for extension ".mjs"

@JamieMason
Copy link
Owner

I think we can rule out sort-package-json then. I'm not sure why this isn't working as syncpack uses cosmiconfig's default behaviour:

const client = cosmiconfigSync('syncpack');
const result = configPath ? client.load(configPath) : client.search();

What do you get for this?

node -e "console.log(require('cosmiconfig').cosmiconfigSync('syncpack').search())"

This failed for me so I think .mjs is not supported in the version of cosmiconfig syncpack currently uses (8.2.0):

echo 'export default {}' > syncpack.config.mjs
node -e "console.log(require('cosmiconfig').cosmiconfigSync('syncpack').load('syncpack.config.mjs'))"

This worked though so I'd go this route for now:

echo 'module.exports = {}' > syncpack.config.cjs
node -e "console.log(require('cosmiconfig').cosmiconfigSync('syncpack').load('syncpack.config.cjs'))"

@mploquin
Copy link
Author

mploquin commented Sep 28, 2023

It seems that the 8.2.0 adds support for ESM to the asynchronous API only and removes support for ESM in the sync API

@JamieMason
Copy link
Owner

Ok, as syncpack is at the moment then, using .cjs sync looks the most likely option.

Does this work in its own?

// syncpack.config.cjs

module.exports = {
  semverRange: "^",
  sortFirst: ['version', 'name'],
};

and with that file in place, does this find it?

node -e "console.log(require('cosmiconfig').cosmiconfigSync('syncpack').load('syncpack.config.cjs'))"

@mploquin
Copy link
Author

Yes it works with .cjs but i cannot grab sortFiles from "sort-package-json" as seen above (it's not possible to require "sort-package-json" => const { sortOrder } = require("sort-package-json"); don't work).

Any plan to migrate to the async API of cosmiconfig and add support for esm format ?

@JamieMason
Copy link
Owner

It'll take a little while because everything is synchronous in syncpack at the moment, but it can be done.

I'll leave this issue open to track doing that.

This captures the situation pretty well

oh-hi-async

@JamieMason JamieMason changed the title Question : is it possible to grab content from another package ? feat(config): support async syncpack.config.mjs Sep 28, 2023
@mploquin
Copy link
Author

Thank you very much for your help and your effort. I'll try to downgrade sort-package-json to test the require version or use a dynamic import if it's possible

@JamieMason
Copy link
Owner

Released in 12.1.0.

👋 If anyone reading this finds syncpack useful, please tell people about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants