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: make /standalone entry work with require() #2247

Merged

Conversation

shufo
Copy link
Contributor

@shufo shufo commented Sep 17, 2023

Context

Problem

Solution

  • Added cjs output in rollup config.
 $  yarn run build
yarn run v1.22.19
$ rollup --config build/rollup.config.js

/home/shuhei/develop/plugin-php/src/index.js → standalone.js, standalone.cjs...
created standalone.js, standalone.cjs in 5.8s
Done in 6.30s.

 $  ls standalone.cjs
.rw-rw-r-- 179k shuhei 17 Sep 15:03 standalone.cjs
  • fix package.json to include standalone.cjs in package and add export mapping.

example code calling from cjs:

$ vim test.js

const prettier = require("prettier");
const phpPlugin = require("@prettier/plugin-php/standalone.cjs");

(async () => {
  const result = await prettier.format("<?php $a = [1,2,3]; ?>", {
    parser: "php",
    plugins: [phpPlugin],
  });
  console.log(result);
})();

 $  node test.js
<?php $a = [1, 2, 3]; ?>

@czosel
Copy link
Collaborator

czosel commented Sep 17, 2023

Thanks for contributing @shufo! This looks like a valid approach to me, but I’d still like to hear from @fisker and/or @loilo before we merge :)

@shufo shufo changed the title Add backward compatibility for CJS chore: add backward compatibility for CJS Sep 17, 2023
@loilo
Copy link
Collaborator

loilo commented Sep 17, 2023

No objections from my side. I'm not generally a proponent of exposing both ESM and CJS in a package, due to dual package hazard, but I don't think Prettier plugins are in particular danger of being subject to that problem.

@shufo
Copy link
Contributor Author

shufo commented Sep 17, 2023

Thanks for confirming. I think it may be safe to use the dual package since Prettier also offers dual package.

      "require": "./src/index.cjs",
      "default": "./src/index.js"

@fisker
Copy link
Member

fisker commented Sep 17, 2023

As I understand, standalone.js supposed to use with prettier standalone version which targets the browser, I don't think the cjs version should named as standalone.cjs.

@fisker
Copy link
Member

fisker commented Sep 17, 2023

I also don't really understand why you are using require() instead of import, at least import is obviously better choice in your example case, since you can use "Top Level Await".

@shufo
Copy link
Contributor Author

shufo commented Sep 17, 2023

Sorry, I should describe my use case first.
My use case is to use prettier/plugin-php from standalone version as described in README.md#WithBundlers.

Remember that even when using a bundler, you still have to use the standalone builds:

import prettier from "prettier/standalone";
import * as prettierPluginPhp from "@prettier/plugin-php/standalone";

My final build target is VSCode Extension and VSCode can only use CJS packages. While there is a solution to consume ESM packages from VSCode, it is a difficult process. Thus, I require the CJS version of this package.

And If we require a cjs version of this plugin, providing a stand-alone version through a rollup was the quickest and most convenient approach without changing the code base.
That's why I changed the build config and added standalone.cjs to exports in this PR.

@fisker
Copy link
Member

fisker commented Sep 17, 2023

My final build target is VSCode Extension and VSCode can only use CJS packages.

That make sense, actually current standalone.js is a UMD module, it should be able to require(), it doesn't work only because type: "module" in package.json makes .js treat as module. Maybe we can remove that and rename other .js files as .mjs?

@loilo
Copy link
Collaborator

loilo commented Sep 17, 2023

That make sense, actually current standalone.js is a UMD module, it should be able to require()

I had this thought as well, but didn't have time to do some research earlier.

Shouldn't we be able to just declare the existing standalone.js under the the "require" exports key? I haven't done this yet, but from the docs it looks like this should work.

@fisker
Copy link
Member

fisker commented Sep 17, 2023

Shouldn't we be able to just declare the existing standalone.js under the the "require" exports key?

I don't think that will work. But you can have a test.

@fisker
Copy link
Member

fisker commented Sep 17, 2023

In Prettier we ship package.json without type, so the UMD version can use .js , other module file uses .mjs extension.

@loilo
Copy link
Collaborator

loilo commented Sep 17, 2023

Shouldn't we be able to just declare the existing standalone.js under the the "require" exports key?

I don't think that will work. But you can have a test.

You're right, this doesn't work. Dang it.

@fisker
Copy link
Member

fisker commented Sep 17, 2023

If we don't want bundle files for Node.js, I think

remove that and rename other .js files as .mjs

is the best choice.

@fisker
Copy link
Member

fisker commented Sep 17, 2023

The another choice is simply rename current standalone.js as standalone.cjs, that may work but it really odd to have .cjs extension for browser. So I still suggest the solution above.

@shufo
Copy link
Contributor Author

shufo commented Sep 18, 2023

Thanks @fisker for suggesting the solution.

remove that and rename other .js files as .mjs

This seems to have worked for me as well. Tested in my environment.

  • remove type from package.json
 $  cat package.json | grep type
 $  echo $?
1
  • rename all .js files to .mjs except standalone.js
 $  find -name "*.mjs" . | grep -v node_modules
./jest.config.mjs
./build/rollup.config.mjs
./tests_config/get_engine.mjs
./tests_config/run_spec.mjs
./src/needs-parens.mjs
./src/loc.mjs
./src/comments.mjs
./src/parser.mjs
./src/printer.mjs
./src/clean.mjs
./src/options.mjs
./src/pragma.mjs
./src/util.mjs
./src/index.mjs
./build/shims/buffer.mjs
./build/shims/assert.mjs
./tests/ignore/jsfmt.spec.mjs
./tests/offsetlookup/jsfmt.spec.mjs
./tests/traitalias/jsfmt.spec.mjs
./tests/attributes/jsfmt.spec.mjs
./tests/assign/jsfmt.spec.mjs
./tests/trailing_comma_func/jsfmt.spec.mjs
./tests/array/jsfmt.spec.mjs
./tests/uniontypes/jsfmt.spec.mjs
./tests/propertylookup/jsfmt.spec.mjs
./tests/exit/jsfmt.spec.mjs
./tests/staticlookup/jsfmt.spec.mjs
./tests/newline/jsfmt.spec.mjs
./tests/propertystatement/jsfmt.spec.mjs
./tests/encapsed/jsfmt.spec.mjs
./tests/program/jsfmt.spec.mjs
./tests/trailing_commas/jsfmt.spec.mjs
./tests/identifier-and-reference/jsfmt.spec.mjs
./tests/global/jsfmt.spec.mjs
./tests/include/jsfmt.spec.mjs
./tests/block/jsfmt.spec.mjs
./tests/goto/jsfmt.spec.mjs
./tests/boolean/jsfmt.spec.mjs
./tests/match/jsfmt.spec.mjs
./tests/bin/jsfmt.spec.mjs
./tests/use/jsfmt.spec.mjs
./tests/brace-style/jsfmt.spec.mjs
./tests/trailing_whitespace/jsfmt.spec.mjs
./tests/attributes-trail-comma/jsfmt.spec.mjs
./tests/functions/jsfmt.spec.mjs
./tests/clone/jsfmt.spec.mjs
./tests/shebang/jsfmt.spec.mjs
./tests/preserve_line/jsfmt.spec.mjs
./tests/interface/jsfmt.spec.mjs
./tests/number/jsfmt.spec.mjs
./tests/nullsafepropertylookup/jsfmt.spec.mjs
./tests/string/jsfmt.spec.mjs
./tests/property/jsfmt.spec.mjs
./tests/method/jsfmt.spec.mjs
./tests/for/jsfmt.spec.mjs
./tests/sys/jsfmt.spec.mjs
./tests/halt/jsfmt.spec.mjs
./tests/call/jsfmt.spec.mjs
./tests/while/jsfmt.spec.mjs
./tests/foreach/jsfmt.spec.mjs
./tests/arrowfunc/jsfmt.spec.mjs
./tests/magic/jsfmt.spec.mjs
./tests/formatting/jsfmt.spec.mjs
./tests/return/jsfmt.spec.mjs
./tests/insert-pragma/jsfmt.spec.mjs
./tests/unset/jsfmt.spec.mjs
./tests/enum/jsfmt.spec.mjs
./tests/echo/jsfmt.spec.mjs
./tests/string-single-quote/jsfmt.spec.mjs
./tests/inline/jsfmt.spec.mjs
./tests/isset/jsfmt.spec.mjs
./tests/namespace/jsfmt.spec.mjs
./tests/classconstant/jsfmt.spec.mjs
./tests/traituse/jsfmt.spec.mjs
./tests/require-pragma/jsfmt.spec.mjs
./tests/member_chain/jsfmt.spec.mjs
./tests/cast/jsfmt.spec.mjs
./tests/variadic/jsfmt.spec.mjs
./tests/namedarguments/jsfmt.spec.mjs
./tests/static/jsfmt.spec.mjs
./tests/print/jsfmt.spec.mjs
./tests/intersection-types/jsfmt.spec.mjs
./tests/parameter/jsfmt.spec.mjs
./tests/extensions/jsfmt.spec.mjs
./tests/continue/jsfmt.spec.mjs
./tests/list/jsfmt.spec.mjs
./tests/break/jsfmt.spec.mjs
./tests/traits/jsfmt.spec.mjs
./tests/syntax-error/jsfmt.spec.mjs
./tests/markdown/jsfmt.spec.mjs
./tests/comments/jsfmt.spec.mjs
./tests/statement/jsfmt.spec.mjs
./tests/variable/jsfmt.spec.mjs
./tests/retif/jsfmt.spec.mjs
./tests/declare/jsfmt.spec.mjs
./tests/do/jsfmt.spec.mjs
./tests/yield/jsfmt.spec.mjs
./tests/if/jsfmt.spec.mjs
./tests/eval/jsfmt.spec.mjs
./tests/assignref/jsfmt.spec.mjs
./tests/silent/jsfmt.spec.mjs
./tests/string-double-quote/jsfmt.spec.mjs
./tests/switch/jsfmt.spec.mjs
./tests/kitchen_sink/jsfmt.spec.mjs
./tests/case/jsfmt.spec.mjs
./tests/new/jsfmt.spec.mjs
./tests/constant/jsfmt.spec.mjs
./tests/class/jsfmt.spec.mjs
./tests/closure/jsfmt.spec.mjs
./tests/parens/jsfmt.spec.mjs
./tests/nowdoc/jsfmt.spec.mjs
./tests/empty/jsfmt.spec.mjs
./tests/errors/jsfmt.spec.mjs
./coverage/lcov-report/prettify.mjs
./coverage/lcov-report/block-navigation.mjs
./coverage/lcov-report/sorter.mjs
  • Passed Test
 $  yarn run test
yarn run v1.22.19
$ yarn test:node && yarn test:standalone
$ jest
 PASS   test-node  tests/preserve_line/jsfmt.spec.mjs
 PASS   test-node  tests/class/jsfmt.spec.mjs
 PASS   test-node  tests/member_chain/jsfmt.spec.mjs
 PASS   test-node  tests/list/jsfmt.spec.mjs
 PASS   test-node  tests/insert-pragma/jsfmt.spec.mjs
 PASS   test-node  tests/newline/jsfmt.spec.mjs
 PASS   test-node  tests/brace-style/jsfmt.spec.mjs
 PASS   test-node  tests/require-pragma/jsfmt.spec.mjs
 PASS   test-node  tests/nowdoc/jsfmt.spec.mjs
 PASS   test-node  tests/functions/jsfmt.spec.mjs
 PASS   test-node  tests/staticlookup/jsfmt.spec.mjs
 PASS   test-node  tests/array/jsfmt.spec.mjs
 PASS   test-node  tests/declare/jsfmt.spec.mjs
 PASS   test-node  tests/assign/jsfmt.spec.mjs
 PASS   test-node  tests/retif/jsfmt.spec.mjs
 PASS   test-node  tests/identifier-and-reference/jsfmt.spec.mjs
 PASS   test-node  tests/switch/jsfmt.spec.mjs
 PASS   test-node  tests/bin/jsfmt.spec.mjs
 PASS   test-node  tests/case/jsfmt.spec.mjs
 PASS   test-node  tests/encapsed/jsfmt.spec.mjs
 PASS   test-node  tests/new/jsfmt.spec.mjs
 PASS   test-node  tests/return/jsfmt.spec.mjs
 PASS   test-node  tests/number/jsfmt.spec.mjs
 PASS   test-node  tests/match/jsfmt.spec.mjs
 PASS   test-node  tests/echo/jsfmt.spec.mjs
 PASS   test-node  tests/namespace/jsfmt.spec.mjs
 PASS   test-node  tests/offsetlookup/jsfmt.spec.mjs
 PASS   test-node  tests/propertylookup/jsfmt.spec.mjs
 PASS   test-node  tests/trailing_comma_func/jsfmt.spec.mjs
 PASS   test-node  tests/attributes/jsfmt.spec.mjs
 PASS   test-node  tests/closure/jsfmt.spec.mjs
 PASS   test-node  tests/variable/jsfmt.spec.mjs
 PASS   test-node  tests/classconstant/jsfmt.spec.mjs
 PASS   test-node  tests/markdown/jsfmt.spec.mjs
 PASS   test-node  tests/string/jsfmt.spec.mjs
 PASS   test-node  tests/call/jsfmt.spec.mjs
 PASS   test-node  tests/sys/jsfmt.spec.mjs
 PASS   test-node  tests/isset/jsfmt.spec.mjs
 PASS   test-node  tests/print/jsfmt.spec.mjs
 PASS   test-node  tests/ignore/jsfmt.spec.mjs
 PASS   test-node  tests/enum/jsfmt.spec.mjs
 PASS   test-node  tests/shebang/jsfmt.spec.mjs
 PASS   test-node  tests/cast/jsfmt.spec.mjs
 PASS   test-node  tests/namedarguments/jsfmt.spec.mjs
 PASS   test-node  tests/kitchen_sink/jsfmt.spec.mjs
 PASS   test-node  tests/halt/jsfmt.spec.mjs
 PASS   test-node  tests/syntax-error/jsfmt.spec.mjs
 PASS   test-node  tests/exit/jsfmt.spec.mjs
 PASS   test-node  tests/parameter/jsfmt.spec.mjs
 PASS   test-node  tests/silent/jsfmt.spec.mjs
 PASS   test-node  tests/assignref/jsfmt.spec.mjs
 PASS   test-node  tests/if/jsfmt.spec.mjs
 PASS   test-node  tests/inline/jsfmt.spec.mjs
 PASS   test-node  tests/constant/jsfmt.spec.mjs
 PASS   test-node  tests/variadic/jsfmt.spec.mjs
 PASS   test-node  tests/property/jsfmt.spec.mjs
 PASS   test-node  tests/string-double-quote/jsfmt.spec.mjs
 PASS   test-node  tests/yield/jsfmt.spec.mjs
 PASS   test-node  tests/foreach/jsfmt.spec.mjs
 PASS   test-node  tests/magic/jsfmt.spec.mjs
 PASS   test-node  tests/use/jsfmt.spec.mjs
 PASS   test-node  tests/interface/jsfmt.spec.mjs
 PASS   test-node  tests/while/jsfmt.spec.mjs
 PASS   test-node  tests/attributes-trail-comma/jsfmt.spec.mjs
 PASS   test-node  tests/propertystatement/jsfmt.spec.mjs
 PASS   test-node  tests/for/jsfmt.spec.mjs
 PASS   test-node  tests/static/jsfmt.spec.mjs
 PASS   test-node  tests/errors/jsfmt.spec.mjs
 PASS   test-node  tests/trailing_whitespace/jsfmt.spec.mjs
 PASS   test-node  tests/trailing_commas/jsfmt.spec.mjs
 PASS   test-node  tests/nullsafepropertylookup/jsfmt.spec.mjs
 PASS   test-node  tests/clone/jsfmt.spec.mjs
 PASS   test-node  tests/unset/jsfmt.spec.mjs
 PASS   test-node  tests/continue/jsfmt.spec.mjs
 PASS   test-node  tests/arrowfunc/jsfmt.spec.mjs
 PASS   test-node  tests/traits/jsfmt.spec.mjs
 PASS   test-node  tests/break/jsfmt.spec.mjs
 PASS   test-node  tests/uniontypes/jsfmt.spec.mjs
 PASS   test-node  tests/empty/jsfmt.spec.mjs
 PASS   test-node  tests/traituse/jsfmt.spec.mjs
 PASS   test-node  tests/traitalias/jsfmt.spec.mjs
 PASS   test-node  tests/do/jsfmt.spec.mjs
 PASS   test-node  tests/string-single-quote/jsfmt.spec.mjs
 PASS   test-node  tests/eval/jsfmt.spec.mjs
 PASS   test-node  tests/formatting/jsfmt.spec.mjs
 PASS   test-node  tests/intersection-types/jsfmt.spec.mjs
 PASS   test-node  tests/boolean/jsfmt.spec.mjs
 PASS   test-node  tests/program/jsfmt.spec.mjs
 PASS   test-node  tests/block/jsfmt.spec.mjs
 PASS   test-node  tests/goto/jsfmt.spec.mjs
 PASS   test-node  tests/include/jsfmt.spec.mjs
 PASS   test-node  tests/global/jsfmt.spec.mjs
 PASS   test-node  tests/extensions/jsfmt.spec.mjs
 PASS   test-node  tests/statement/jsfmt.spec.mjs
 PASS   test-node  tests/method/jsfmt.spec.mjs
 PASS   test-node  tests/parens/jsfmt.spec.mjs
 PASS   test-node  tests/comments/jsfmt.spec.mjs

Test Suites: 97 passed, 97 total
Tests:       657 passed, 657 total
Snapshots:   656 passed, 656 total
Time:        2.591 s
Ran all test suites.
$ yarn run build && cross-env RUN_STANDALONE_TESTS=true yarn jest
$ rollup --config build/rollup.config.mjs

/home/shuhei/develop/plugin-php/src/index.mjs → standalone.js...
created standalone.js in 4.2s
$ /home/shuhei/develop/plugin-php/node_modules/.bin/jest
 PASS   test-standalone  tests/call/jsfmt.spec.mjs
 PASS   test-standalone  tests/newline/jsfmt.spec.mjs
 PASS   test-standalone  tests/preserve_line/jsfmt.spec.mjs
 PASS   test-standalone  tests/list/jsfmt.spec.mjs
 PASS   test-standalone  tests/class/jsfmt.spec.mjs
 PASS   test-standalone  tests/insert-pragma/jsfmt.spec.mjs
 PASS   test-standalone  tests/nowdoc/jsfmt.spec.mjs
 PASS   test-standalone  tests/member_chain/jsfmt.spec.mjs
 PASS   test-standalone  tests/string/jsfmt.spec.mjs
 PASS   test-standalone  tests/require-pragma/jsfmt.spec.mjs
 PASS   test-standalone  tests/identifier-and-reference/jsfmt.spec.mjs
 PASS   test-standalone  tests/array/jsfmt.spec.mjs
 PASS   test-standalone  tests/brace-style/jsfmt.spec.mjs
 PASS   test-standalone  tests/case/jsfmt.spec.mjs
 PASS   test-standalone  tests/new/jsfmt.spec.mjs
 PASS   test-standalone  tests/retif/jsfmt.spec.mjs
 PASS   test-standalone  tests/parameter/jsfmt.spec.mjs
 PASS   test-standalone  tests/bin/jsfmt.spec.mjs
 PASS   test-standalone  tests/markdown/jsfmt.spec.mjs
 PASS   test-standalone  tests/variable/jsfmt.spec.mjs
 PASS   test-standalone  tests/return/jsfmt.spec.mjs
 PASS   test-standalone  tests/echo/jsfmt.spec.mjs
 PASS   test-standalone  tests/closure/jsfmt.spec.mjs
 PASS   test-standalone  tests/number/jsfmt.spec.mjs
 PASS   test-standalone  tests/cast/jsfmt.spec.mjs
 PASS   test-standalone  tests/offsetlookup/jsfmt.spec.mjs
 PASS   test-standalone  tests/namespace/jsfmt.spec.mjs
 PASS   test-standalone  tests/encapsed/jsfmt.spec.mjs
 PASS   test-standalone  tests/attributes/jsfmt.spec.mjs
 PASS   test-standalone  tests/static/jsfmt.spec.mjs
 PASS   test-standalone  tests/declare/jsfmt.spec.mjs
 PASS   test-standalone  tests/for/jsfmt.spec.mjs
 PASS   test-standalone  tests/assign/jsfmt.spec.mjs
 PASS   test-standalone  tests/staticlookup/jsfmt.spec.mjs
 PASS   test-standalone  tests/halt/jsfmt.spec.mjs
 PASS   test-standalone  tests/inline/jsfmt.spec.mjs
 PASS   test-standalone  tests/propertylookup/jsfmt.spec.mjs
 PASS   test-standalone  tests/classconstant/jsfmt.spec.mjs
 PASS   test-standalone  tests/switch/jsfmt.spec.mjs
 PASS   test-standalone  tests/trailing_comma_func/jsfmt.spec.mjs
 PASS   test-standalone  tests/eval/jsfmt.spec.mjs
 PASS   test-standalone  tests/match/jsfmt.spec.mjs
 PASS   test-standalone  tests/variadic/jsfmt.spec.mjs
 PASS   test-standalone  tests/include/jsfmt.spec.mjs
 PASS   test-standalone  tests/assignref/jsfmt.spec.mjs
 PASS   test-standalone  tests/yield/jsfmt.spec.mjs
 PASS   test-standalone  tests/program/jsfmt.spec.mjs
 PASS   test-standalone  tests/syntax-error/jsfmt.spec.mjs
 PASS   test-standalone  tests/trailing_commas/jsfmt.spec.mjs
 PASS   test-standalone  tests/isset/jsfmt.spec.mjs
 PASS   test-standalone  tests/print/jsfmt.spec.mjs
 PASS   test-standalone  tests/formatting/jsfmt.spec.mjs
 PASS   test-standalone  tests/foreach/jsfmt.spec.mjs
 PASS   test-standalone  tests/errors/jsfmt.spec.mjs
 PASS   test-standalone  tests/exit/jsfmt.spec.mjs
 PASS   test-standalone  tests/enum/jsfmt.spec.mjs
 PASS   test-standalone  tests/magic/jsfmt.spec.mjs
 PASS   test-standalone  tests/interface/jsfmt.spec.mjs
 PASS   test-standalone  tests/trailing_whitespace/jsfmt.spec.mjs
 PASS   test-standalone  tests/namedarguments/jsfmt.spec.mjs
 PASS   test-standalone  tests/use/jsfmt.spec.mjs
 PASS   test-standalone  tests/continue/jsfmt.spec.mjs
 PASS   test-standalone  tests/functions/jsfmt.spec.mjs
 PASS   test-standalone  tests/property/jsfmt.spec.mjs
 PASS   test-standalone  tests/nullsafepropertylookup/jsfmt.spec.mjs
 PASS   test-standalone  tests/uniontypes/jsfmt.spec.mjs
 PASS   test-standalone  tests/if/jsfmt.spec.mjs
 PASS   test-standalone  tests/ignore/jsfmt.spec.mjs
 PASS   test-standalone  tests/traituse/jsfmt.spec.mjs
 PASS   test-standalone  tests/propertystatement/jsfmt.spec.mjs
 PASS   test-standalone  tests/silent/jsfmt.spec.mjs
 PASS   test-standalone  tests/empty/jsfmt.spec.mjs
 PASS   test-standalone  tests/unset/jsfmt.spec.mjs
 PASS   test-standalone  tests/kitchen_sink/jsfmt.spec.mjs
 PASS   test-standalone  tests/shebang/jsfmt.spec.mjs
 PASS   test-standalone  tests/while/jsfmt.spec.mjs
 PASS   test-standalone  tests/traits/jsfmt.spec.mjs
 PASS   test-standalone  tests/sys/jsfmt.spec.mjs
 PASS   test-standalone  tests/constant/jsfmt.spec.mjs
 PASS   test-standalone  tests/string-double-quote/jsfmt.spec.mjs
 PASS   test-standalone  tests/traitalias/jsfmt.spec.mjs
 PASS   test-standalone  tests/intersection-types/jsfmt.spec.mjs
 PASS   test-standalone  tests/do/jsfmt.spec.mjs
 PASS   test-standalone  tests/statement/jsfmt.spec.mjs
 PASS   test-standalone  tests/boolean/jsfmt.spec.mjs
 PASS   test-standalone  tests/goto/jsfmt.spec.mjs
 PASS   test-standalone  tests/arrowfunc/jsfmt.spec.mjs
 PASS   test-standalone  tests/global/jsfmt.spec.mjs
 PASS   test-standalone  tests/block/jsfmt.spec.mjs
 PASS   test-standalone  tests/string-single-quote/jsfmt.spec.mjs
 PASS   test-standalone  tests/extensions/jsfmt.spec.mjs
 PASS   test-standalone  tests/clone/jsfmt.spec.mjs
 PASS   test-standalone  tests/attributes-trail-comma/jsfmt.spec.mjs
 PASS   test-standalone  tests/break/jsfmt.spec.mjs
 PASS   test-standalone  tests/method/jsfmt.spec.mjs
 PASS   test-standalone  tests/parens/jsfmt.spec.mjs
 PASS   test-standalone  tests/comments/jsfmt.spec.mjs

Test Suites: 97 passed, 97 total
Tests:       657 passed, 657 total
Snapshots:   656 passed, 656 total
Time:        2.536 s
Ran all test suites.
Done in 12.09s.
  • Calling from CJS seems ok
import * as phpPlugin from '@prettier/plugin-php/standalone';
 $  ./bin/blade-formatter.js test.blade.php
@extends('layouts.test')
@section('title', 'This is title')
@section('content')
    <div class=\"someClass\">
        This is content.
    </div>
@endsection·
@if (true)
    @push('some-stack', $some->getContent())
    @section($aSection, $some->content)
    @push('some-stack')
        more
    @endpush
    @prepend($stack->name, 'here we go')
@endif
  • Calling from ESM is ok
 $  cat package.json | grep type
  "type": "module"

 $  cat test2.js
import prettier from "prettier/standalone";
import * as phpPlugin from "@prettier/plugin-php/standalone";

const result = await prettier.format("<?php $a = [1,2,3]; ?>", {
  parser: "php",
  plugins: [phpPlugin],
});
console.log(result);

 $  node test2.js
<?php $a = [1, 2, 3]; ?>

I don't know migrating all .js files to .mjs in this package is the safe way, but I think this seems the clean way to go.

@fisker
Copy link
Member

fisker commented Sep 18, 2023

I don't know migrating all .js files to .mjs in this package is the safe way

I think it's safe.

src/comments.mjs Outdated Show resolved Hide resolved
src/comments.mjs Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
jest.config.mjs Outdated Show resolved Hide resolved
@fisker fisker changed the title chore: add backward compatibility for CJS fix: make /standalone entry work with require() Sep 20, 2023
@fisker fisker requested review from loilo and czosel September 21, 2023 15:21
@shufo
Copy link
Contributor Author

shufo commented Oct 17, 2023

@czosel @loilo
Sorry for pinging.
Any update on this pull request?

@shufo
Copy link
Contributor Author

shufo commented Dec 16, 2023

If there is no other concerns, I'd appreciate it if this PR merged as fisker approved.

@czosel
Copy link
Collaborator

czosel commented Dec 16, 2023

Sorry about the delay! Thanks for contributing @shufo

@czosel czosel merged commit 69a68d8 into prettier:main Dec 16, 2023
@shufo
Copy link
Contributor Author

shufo commented Dec 16, 2023

Thank you @czosel!

@czosel
Copy link
Collaborator

czosel commented Dec 16, 2023

Released in v0.22.0 🎉

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

Successfully merging this pull request may close these issues.

Backward compatibility for CommonJS
4 participants