milesj / packemon

📦 Build and prepare packages for npm distribution using standardized configurations and practices. Gotta pack 'em all!
https://packemon.dev
MIT License
203 stars 3 forks source link

'--loadConfigs' does not load Babel plugins specified in babel.config.json #248

Closed BinToss closed 2 months ago

BinToss commented 2 months ago

Is this intentional or is it a bug?


Packemon 4.0.1

babel.config.json

{
    "presets": [
        "moon"
    ],
    "plugins": [
        "@babel/plugin-syntax-import-attributes"
    ]
}

package.json

{
...
"packemon": [
    {
      "format": "mjs",
      "platform": "node",
      "inputs": {
        "commitlintConfig": "src/commitlintConfig.ts",
        "dotnetGHPR": "src/dotnet/dotnetGHPR.ts",
        "dotnetGLPR": "src/dotnet/dotnetGLPR.ts",
        "dotnetHelpers": "src/dotnet/dotnetHelpers.ts",
        "dotnetMSBuildProject": "src/dotnet/MSBuildProject.ts",
        "dotnetMSBuildProjectProperties": "src/dotnet/MSBuildProjectProperties.ts",
        "eslintConfig": "src/eslintConfig.ts",
        "findStaticConfig": "src/findStaticConfig.ts",
        "index": "src/index.ts",
        "semanticReleaseConfig": "src/semanticReleaseConfig.ts",
        "semanticReleaseConfigDotnet": "src/semanticReleaseConfigDotnet.ts",
        "setupGitPluginSpec": "src/setupGitPluginSpec.ts"
      }
    }
  ]
}

npx cross-env BABEL_SHOW_CONFIG_FOR=C:\Repos\HaloSPV3\HCE.Shared\src\eslintConfig.ts packemon build --addEngines --addExports --addFiles --declaration --loadConfigs --debug


npx cross-env BABEL_SHOW_CONFIG_FOR=C:\Repos\HaloSPV3\HCE.Shared\src\eslintConfig.ts packemon build --addEngines --addExports --addFiles --declaration --loadConfigs --debug
packemon:core Running packemon in C:\Repos\HaloSPV3\HCE.Shared +0ms
packemon:core Finding package in C:\Repos\HaloSPV3\HCE.Shared +1ms
packemon:core Running packemon in C:\Repos\HaloSPV3\HCE.Shared +0ms
packemon:core Starting `build` process +1ms
packemon:package:HCE Generating artifacts +0ms
packemon:package:HCE  - node:stable:mjs +0ms
packemon:package:HCE Building artifacts +2ms
packemon:package:HCE Loading feature flags +1ms
packemon:package:HCE  - TypeScript (strict, non-decorators) +159ms
packemon:artifact:HCE:node:stable:mjs Building code artifacts with Rollup +0ms
packemon:artifact:HCE:node:stable:mjs Building types artifacts with TypeScript +6ms
Babel configs on "C:\Repos\HaloSPV3\HCE.Shared\src\eslintConfig.ts" (ascending priority):
programmatic options from packemon
{
"plugins": [],
"sourceMaps": false,
"caller": {
"name": "packemon",
"supportsStaticESM": true,
"supportsDynamicImport": true,
"supportsTopLevelAwait": true,
"supportsExportNamespaceFrom": true
},
"comments": true,
"parserOpts": {
"sourceType": "unambiguous",
"strictMode": true
},
"presets": [
[
"C:\\Repos\\HaloSPV3\\HCE.Shared\\node_modules\\@babel\\preset-typescript\\lib\\index.js",
{
"allowDeclareFields": true,
"onlyRemoveTypeImports": false,
"optimizeConstEnums": true
}
]
],
"configFile": false,
"babelrc": false,
"filename": "C:\\Repos\\HaloSPV3\\HCE.Shared\\src\\eslintConfig.ts"
}
-----End Babel configs-----
Babel configs on "C:\Repos\HaloSPV3\HCE.Shared\src\eslintConfig.ts" (ascending priority):
programmatic options from packemon
{
"assumptions": {},
"sourceMaps": false,
"caller": {
"name": "packemon",
"supportsStaticESM": true,
"supportsDynamicImport": true,
"supportsTopLevelAwait": true,
"supportsExportNamespaceFrom": true
},
"comments": true,
"parserOpts": {
"sourceType": "unambiguous",
"strictMode": true
},
"configFile": false,
"babelrc": false,
"filename": "C:\\Repos\\HaloSPV3\\HCE.Shared\\src\\eslintConfig.ts",
"targets": {},
"cloneInputAst": true,
"browserslistConfigFile": false,
"passPerPreset": false,
"envName": "development",
"cwd": "C:\\Repos\\HaloSPV3\\HCE.Shared",
"root": "C:\\Repos\\HaloSPV3\\HCE.Shared",
"rootMode": "root",
"plugins": [

ERROR

C:\Repos\HaloSPV3\HCE.Shared\src\eslintConfig.ts: Support for the experimental syntax 'importAttributes' isn't currently enabled (9:65):

7 | import { configs as jsoncConfigs } from "eslint-plugin-jsonc";
8 | import * as globals from "globals";

9 | import { node as nodeGlobals } from "globals/globals.json" with {type: "json"} | ^ 10 | import path from "path"; 11 | import tseslint from "typescript-eslint"; 12 | import { pathToFileURL } from "url";

Add @babel/plugin-syntax-import-attributes (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-import-attributes) to the 'plugins' section of your Babel config to enable parsing.

If you already added the plugin for this syntax to your config, it's possible that your config isn't being loaded. You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration: npx cross-env BABEL_SHOW_CONFIG_FOR=C:\Repos\HaloSPV3\HCE.Shared\src\eslintConfig.ts See https://babeljs.io/docs/configuration#print-effective-configs for more info.

STACK TRACE

at constructor (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:353:19)
at TypeScriptParserMixin.raise (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:3277:19)
at TypeScriptParserMixin.expectPlugin (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:3305:16)
at TypeScriptParserMixin.expectImportAttributesPlugin (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:10729:12)
at TypeScriptParserMixin.maybeParseImportAttributes (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:13792:16)
at TypeScriptParserMixin.parseImportSourceAndAttributes (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:13702:10)
at TypeScriptParserMixin.parseImportSpecifiersAndAfter (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:13696:17)
at TypeScriptParserMixin.parseImport (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:13687:17)
at TypeScriptParserMixin.parseImport (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:9059:26)
at TypeScriptParserMixin.parseStatementContent (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12347:27)
at TypeScriptParserMixin.parseStatementContent (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:9142:18)
at TypeScriptParserMixin.parseStatementLike (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12239:17)
at TypeScriptParserMixin.parseModuleItem (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12216:17)
at TypeScriptParserMixin.parseBlockOrModuleBlockBody (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12796:36)
at TypeScriptParserMixin.parseBlockBody (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12789:10)
at TypeScriptParserMixin.parseProgram (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12116:10)
at TypeScriptParserMixin.parseTopLevel (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:12106:25)
at TypeScriptParserMixin.parse (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:13905:10)
at TypeScriptParserMixin.parse (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:9743:18)
at parse (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\parser\lib\index.js:13926:26)
at parser (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\core\lib\parser\index.js:41:34)
at parser.next (<anonymous>)
at normalizeFile (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\core\lib\transformation\normalize-file.js:64:37)
at normalizeFile.next (<anonymous>)
at run (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\core\lib\transformation\index.js:21:50)
at run.next (<anonymous>)
at transform (C:\Repos\HaloSPV3\HCE.Shared\node_modules\@babel\core\lib\transform.js:22:33)
at transform.next (<anonymous>)
at step (C:\Repos\HaloSPV3\HCE.Shared\node_modules\gensync\index.js:261:32)
at C:\Repos\HaloSPV3\HCE.Shared\node_modules\gensync\index.js:273:13
at async.call.result.err.err (C:\Repos\HaloSPV3\HCE.Shared\node_modules\gensync\index.js:223:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
milesj commented 2 months ago

@BinToss --loadConfigs only loads packemon specific configs. You would need to customize babel through those.

https://packemon.dev/docs/advanced#customizing-babel-swc-and-rollup

BinToss commented 2 months ago

Oh, I misunderstood the requirements, then. I though only one of the conditions were needed and that --loadConfigs would allow Babel and Rollup to load their config files.


Since I have your attention, should I also create new Issues to report Packemon's Types.ts not being exported or recognized by Typescript? I followed the example on the page you linked, but import type { ConfigFile } from 'packemon'; is invalid. In fact, none of Types.ts's exports are visible.

Could not find a declaration file for module 'packemon'. 
'c:/Repos/HaloSPV3/HCE.Shared/node_modules/packemon/mjs/index.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/packemon` if it exists or add a new declaration (.d.ts) file containing `declare module 'packemon';`
ts(7016)

That type can be imported via relative path to src/Types.ts, but that file contains syntax errors. All imports in that file are expected to be type imports, but most are not.


Also in Types.ts is non-type imports of interface types. Typescript does not tolerate this with 'verbatimModuleSyntax'.

node_modules/packemon/src/types.ts:1:10 - error TS1484: 'OutputOptions' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.

1 import { OutputOptions as RollupOutputOptions, RollupOptions } from 'rollup';
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/packemon/src/types.ts:1:48 - error TS1484: 'RollupOptions' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.

1 import { OutputOptions as RollupOutputOptions, RollupOptions } from 'rollup';
                                                 ~~~~~~~~~~~~~

node_modules/packemon/src/types.ts:2:10 - error TS1484: 'TransformOptions' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.

2 import { TransformOptions as BabelOptions } from '@babel/core';
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/packemon/src/types.ts:3:10 - error TS1484: 'PackageStructure' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.

3 import { PackageStructure } from '@boost/common';
           ~~~~~~~~~~~~~~~~
milesj commented 2 months ago

Yeah open up a new issue, those should work.

BinToss commented 2 months ago

Should I also open a feature request issue to add the @babel/plugin-syntax-import-attributes plugin to allow JSON imports with type assertion (import p from "p.json" with {type:"json"})? Babel's complaints about needing that plugin is what led to this Issue being opened.

milesj commented 2 months ago

@BinToss Go for it.

BinToss commented 2 months ago

Though somewhat unrelated to Packemon, I think you should be aware of this issue in case another user experiences it.

The resolve package affects Rollup's output directory layout unexpectedly. If any source files import it, the output directory becomes nested. Instead of outputting files to ./mjs, they're output to ./mjs/src. Packemon and Typescript are not made aware of this and assume the output directory is unnested. When built with --addExports --declarations, the generated package.json#exports (and main, types, et cetera) are not aware the MJS files have been nested. Package exports and types will not work at all when this happens.

Expected Layout:

./
|_ mjs/
|   |_ *.mjs
|   |_ *.map
|   |_ *.d.ts
|_ src/
|   |_ *.ts
|_ (other root folders/files)

Actual Layout when importing resolve:

./
|_ mjs/
|   |_ _virtual/
|   |_ node_modules/
|   |_ mjs/
|   |  |_ *.mjs
|   |  |_ *.map
|   |_ *.d.ts
|_ src/
|   |_ *.ts
|_ (other root folders/files)
milesj commented 2 months ago

Is resolve in the package deps? It should be marked external so this doesn't happen.

BinToss commented 2 months ago

Is resolve in the package deps?

Not explicitly. I was depending on it as a transitive dependency. That probably contributed to the issue.

It should be marked external so this doesn't happen.

Thank you! I haven't had to mess with rollup's externals configuration before, so your tip was greatly appreciated! For now, I ended up using JSON import attributes instead of resolve + a dynamic import/require. But, just in case it pops up later, I've changed packemon.config.js#config.rollupInput to conditionally add resolve to the externals array.