Skip to content

Add package exports fallback for the CLI, dist files, etc. #1527

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

Merged
merged 2 commits into from
Oct 28, 2020

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Oct 28, 2020

With the fallback, legacy cases like

import asc from "assemblyscript/cli/asc";
import options from "assemblyscript/cli/util/options";
import distAsc from "assemblyscript/dist/asc";

including others we haven't though of yet are covered by always resolving ./* -> ./*.js. Fixes #1522

At some point we'll most likely want to break this, either to pull packages out of the main packages or make nicer entry points like just /loader, but this gives us sufficient backwards compatibility (on node >= 15, need to check 14) for now so is good to do :)

Edit: According to the node 14 docs, the fallback should work there as well.

  • I've read the contributing guidelines

@SebastianSpeitel
Copy link

SebastianSpeitel commented Oct 28, 2020

Maybe a test suite which tries to import/resolve the subpackages/named exports under different conditions (node versions, type=commonjs, type=module, using TS, using different bundlers, ...) could be useful.

Sorry, if https://github.com/AssemblyScript/assemblyscript/tree/master/tests/packages already includes thoses.

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 28, 2020

I agree that having actual tests would be best, yeah. Needs to somehow install the package before it's published then, via a file: dependency or the like I guess.

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 28, 2020

Also note that 0.17.0 breaks with node <=12 anyhow (14 entered LTS just recently) due to making the ES module the default, with /umd/index.js being what to use on older node versions now. Hence, for exports syntax, node >=14 is of interest. Going to test with a node 12 manually for now to see how it behaves, so I can give proper recommendations.

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 28, 2020

Turns out that node 12 has some sort of experimental support with a warning:

(node:4732) ExperimentalWarning: The ESM module loader is experimental.
internal/process/esm_loader.js:90
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './cli/asc' is not defined by "exports" in path\to\node_modules\assemblyscript\package.json imported from path\to\test\esm\index.js

and does not understand the * syntax yet, so to support node 12 this change needs to be

    "./*": "./*.js",
    "./cli/asc": "./cli/asc.js",
    "./cli/util/options": "./cli/util/options.js",
    "./dist/asc": "./dist/asc.js"

with the fallback triggering for node >=14, and being otherwise ignored.

Raises the question whether we should rather intentionally break with node 12, so users upgrade to a version of node that we can guarantee will work for at least a while.

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 28, 2020

For completeness, node 10 breaks on TextDecoder used as a global in the loader btw, and does not understand exports syntax at all, requiring lib/loader/umd otherwise. Probably not worth to support, but then maybe fallbacks for 12, simply because we can, are fine.

@jtenner jtenner mentioned this pull request Oct 28, 2020
1 task
@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 28, 2020

Merging this for now since I expect most problems to be solved by it, but I also have an external test package now that I can first verify with and perhaps make a test of eventually.

@dcodeIO dcodeIO merged commit 019515e into master Oct 28, 2020
@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 29, 2020

Test package: https://github.com/dcodeIO/assemblyscript-esm-test/runs/1326736751

Import type ^15 15.0.0 ^14 14.0.0 ^13 13.0.0 ^12 12.0.0 ^11 11.0.0 ^10 10.0.0
import "pkg" 2 3 3 3 3 3
require("pkg") 1 4 4 4 4 4
require("pkg/umd") 1 1 1 1 5 5
  1. Package subpath 'pkg' is not defined by "exports" in package.json
  2. SyntaxError: Cannot use import statement outside a module
  3. SyntaxError: Unexpected identifier
  4. SyntaxError: Unexpected token export
  5. ReferenceError: TextDecoder is not defined

TL;DR: Node >=^12 works with the new stuff, 11 works with legacy require, and less than 11 fails due to TextDecoder not being a global in the loader, but otherwise (compiler, rtrace) also works with legacy require.

@jtenner
Copy link
Contributor

jtenner commented Oct 29, 2020

So it works with node 14 and 15 just fine? I'm okay with that.

@dcodeIO dcodeIO deleted the package-exports branch June 1, 2021 15:20
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.

3 participants