-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
fix: make /standalone
entry work with require()
#2247
Conversation
This commit adds cjs output to rollup config.
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. |
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" |
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. |
I also don't really understand why you are using |
Sorry, I should describe my use case first.
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 make sense, actually current |
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 |
I don't think that will work. But you can have a test. |
In Prettier we ship package.json without |
You're right, this doesn't work. Dang it. |
If we don't want bundle files for Node.js, I think
is the best choice. |
The another choice is simply rename current |
Thanks @fisker for suggesting the solution.
This seems to have worked for me as well. Tested in my environment.
$ cat package.json | grep type
$ echo $?
1
$ 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
$ 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.
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
$ 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. |
I think it's safe. |
Without this require, vscode-prettier can't resolve php plugin
/standalone
entry work with require()
If there is no other concerns, I'd appreciate it if this PR merged as fisker approved. |
Sorry about the delay! Thanks for contributing @shufo |
Thank you @czosel! |
Released in |
Context
Problem
Solution
example code calling from cjs: