@jcoreio/toolchain-esnext 3.6.2 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcoreio/toolchain-esnext",
3
- "version": "3.6.2",
3
+ "version": "3.7.0",
4
4
  "description": "ESNext JS build toolchain",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,15 +26,16 @@
26
26
  "babel-parse-wild-code": "^2.1.3",
27
27
  "babel-plugin-add-module-exports": "^1.0.4",
28
28
  "babel-plugin-istanbul": "^6.1.1",
29
+ "babel-register-esm": "^1.2.5",
29
30
  "dedent-js": "^1.0.1",
30
31
  "eslint": "^8.43.0",
31
32
  "fs-extra": "^10.0.0",
32
33
  "resolve": "^1.22.2",
33
34
  "resolve-bin": "^1.0.0",
34
- "@jcoreio/toolchain": "3.6.2"
35
+ "@jcoreio/toolchain": "3.7.0"
35
36
  },
36
37
  "peerDependencies": {
37
- "@jcoreio/toolchain": "3.6.2"
38
+ "@jcoreio/toolchain": "3.7.0"
38
39
  },
39
40
  "toolchainManaged": {
40
41
  "dependencies": {
@@ -1,10 +1,28 @@
1
1
  module.exports = [
2
2
  (api) => {
3
- const { JCOREIO_TOOLCHAIN_MJS } = process.env
4
- api.cache.using(() => JCOREIO_TOOLCHAIN_MJS)
3
+ const {
4
+ JCOREIO_TOOLCHAIN_CJS,
5
+ JCOREIO_TOOLCHAIN_ESM,
6
+ JCOREIO_TOOLCHAIN_TEST,
7
+ } = process.env
8
+ api.cache.using(() => JCOREIO_TOOLCHAIN_CJS)
9
+ api.cache.using(() => JCOREIO_TOOLCHAIN_ESM)
10
+ api.cache.using(() => JCOREIO_TOOLCHAIN_TEST)
5
11
  return [
6
12
  require.resolve('@babel/plugin-transform-runtime'),
7
- !JCOREIO_TOOLCHAIN_MJS &&
13
+ // for CJS tests, we leave off import extensions, since @babel/register resolves them.
14
+ // for ESM tests, we resolve to .js for babel-register-esm.
15
+ // for CJS build, we resolve to .js.
16
+ // for ESM build, we resolve to .mjs.
17
+ ((JCOREIO_TOOLCHAIN_CJS && !JCOREIO_TOOLCHAIN_TEST) ||
18
+ JCOREIO_TOOLCHAIN_ESM) && [
19
+ require.resolve('../util/babelPluginResolveImports.cjs'),
20
+ {
21
+ outputExtension:
22
+ JCOREIO_TOOLCHAIN_ESM && !JCOREIO_TOOLCHAIN_TEST ? '.mjs' : '.js',
23
+ },
24
+ ],
25
+ !JCOREIO_TOOLCHAIN_ESM &&
8
26
  require.resolve('babel-plugin-add-module-exports'),
9
27
  ].filter(Boolean)
10
28
  },
@@ -2,20 +2,20 @@ const { toolchainConfig } = require('@jcoreio/toolchain/util/findUps.cjs')
2
2
 
3
3
  module.exports = [
4
4
  (api) => {
5
- const { JCOREIO_TOOLCHAIN_MJS } = process.env
6
- api.cache.using(() => JCOREIO_TOOLCHAIN_MJS)
5
+ const { JCOREIO_TOOLCHAIN_ESM } = process.env
6
+ api.cache.using(() => JCOREIO_TOOLCHAIN_ESM)
7
7
  return [
8
8
  [
9
9
  require.resolve('@babel/preset-env'),
10
10
  {
11
- ...(JCOREIO_TOOLCHAIN_MJS
11
+ ...(JCOREIO_TOOLCHAIN_ESM
12
12
  ? toolchainConfig.esmBabelEnv || {
13
13
  targets: {
14
14
  node: 16,
15
15
  },
16
16
  }
17
17
  : toolchainConfig.cjsBabelEnv || { forceAllTransforms: true }),
18
- modules: JCOREIO_TOOLCHAIN_MJS ? false : 'auto',
18
+ modules: JCOREIO_TOOLCHAIN_ESM ? false : 'auto',
19
19
  },
20
20
  ],
21
21
  ]
@@ -8,22 +8,25 @@ const {
8
8
  projectDir,
9
9
  } = require('@jcoreio/toolchain/util/findUps.cjs')
10
10
  const getPluginsArraySync = require('@jcoreio/toolchain/util/getPluginsArraySync.cjs')
11
- const resolveImportsCodemod = require('../util/resolveImportsCodemod.cjs')
12
11
 
13
12
  module.exports = [
14
13
  [
15
14
  async function compile(args = []) {
16
15
  const extensions = getPluginsArraySync('babelExtensions')
17
16
 
18
- await execa('babel', [
19
- 'src',
20
- ...(extensions.length ? ['--extensions', extensions.join(',')] : []),
21
- ...(extensions.includes('.ts') ? ['--ignore', '**/*.d.ts'] : []),
22
- '--out-dir',
23
- 'dist',
24
- '--out-file-extension',
25
- '.js',
26
- ])
17
+ await execa(
18
+ 'babel',
19
+ [
20
+ 'src',
21
+ ...(extensions.length ? ['--extensions', extensions.join(',')] : []),
22
+ ...(extensions.includes('.ts') ? ['--ignore', '**/*.d.ts'] : []),
23
+ '--out-dir',
24
+ 'dist',
25
+ '--out-file-extension',
26
+ '.js',
27
+ ],
28
+ { env: { ...process.env, JCOREIO_TOOLCHAIN_CJS: '1' } }
29
+ )
27
30
  if (extensions.length) {
28
31
  for (const ext of ['.js', '.mjs']) {
29
32
  if (!extensions.includes(ext)) {
@@ -43,7 +46,6 @@ module.exports = [
43
46
  }
44
47
  }
45
48
  const jsFiles = await glob(path.join('dist', '**', '*.js'))
46
- await resolveImportsCodemod(jsFiles)
47
49
  if (toolchainConfig.outputEsm !== false) {
48
50
  if (toolchainConfig.esWrapper) {
49
51
  await Promise.all(
@@ -73,10 +75,8 @@ module.exports = [
73
75
  '--out-file-extension',
74
76
  '.mjs',
75
77
  ],
76
- { env: { ...process.env, JCOREIO_TOOLCHAIN_MJS: '1' } }
78
+ { env: { ...process.env, JCOREIO_TOOLCHAIN_ESM: '1' } }
77
79
  )
78
- const mjsFiles = await glob(path.join('dist', '**', '*.mjs'))
79
- await resolveImportsCodemod(mjsFiles)
80
80
  }
81
81
  }
82
82
  },
@@ -3,24 +3,32 @@ const confirmOutputEsm = require('@jcoreio/toolchain/scripts/migrate/confirmOutp
3
3
  const dedent = require('dedent-js')
4
4
 
5
5
  module.exports = [
6
- async function getConfigFiles() {
7
- return {
8
- 'toolchain.config.cjs': async (existing) => {
9
- if (existing) return existing
10
- const outputEsm = await confirmOutputEsm()
11
- return dedent`
6
+ [
7
+ async function getConfigFiles() {
8
+ return {
9
+ 'toolchain.config.cjs': async (existing) => {
10
+ if (existing) return existing
11
+ const outputEsm = await confirmOutputEsm()
12
+ return dedent`
12
13
  /* eslint-env node, es2018 */
13
14
  module.exports = {
14
15
  cjsBabelEnv: { forceAllTransforms: true },
16
+ ${outputEsm ? '' : '// '}esmBabelEnv: { targets: { node: 16 } },
15
17
  ${
16
- outputEsm
17
- ? `esmBabelEnv: { targets: { node: 16 } }`
18
- : `outputEsm: false`
19
- },
18
+ outputEsm ? '// ' : ''
19
+ }outputEsm: false, // disables ESM output (default: true)
20
+ // esWrapper: true, // outputs ES module wrappers for CJS modules (default: false)
21
+ // scripts: {
22
+ // pretest: 'docker compose up -d',
23
+ // jsExample: {
24
+ // description: 'example of running a JS script',
25
+ // run: async (args = []) => console.log('TEST', ...args),
26
+ // },
27
+ // }
20
28
  }
21
29
  `
22
- },
23
- '.babelrc.cjs': dedent`
30
+ },
31
+ '.babelrc.cjs': dedent`
24
32
  /* eslint-env node, es2018 */
25
33
  module.exports = function (api) {
26
34
  const base = require('${name}/.babelrc.cjs')(api)
@@ -29,6 +37,8 @@ module.exports = [
29
37
  }
30
38
  }
31
39
  `,
32
- }
33
- },
40
+ }
41
+ },
42
+ { after: '@jcoreio/toolchain' },
43
+ ],
34
44
  ]
@@ -0,0 +1,42 @@
1
+ const resolveImportSource = require('./resolveImportSource.cjs')
2
+
3
+ module.exports = function babelPluginResolveImports({ types: t }) {
4
+ function handleSource(path, state) {
5
+ if (!path || !path.node) return
6
+ const source = path.node.value
7
+ const outputExtension = state.opts && state.opts.outputExtension
8
+ const resolved = resolveImportSource({
9
+ file: state.filename,
10
+ source,
11
+ outputExtension,
12
+ })
13
+ if (resolved !== source) {
14
+ path.replaceWith(t.stringLiteral(resolved))
15
+ }
16
+ }
17
+
18
+ return {
19
+ visitor: {
20
+ ImportDeclaration(path, state) {
21
+ handleSource(path.get('source'), state)
22
+ },
23
+ ExportNamedDeclaration(path, state) {
24
+ handleSource(path.get('source'), state)
25
+ },
26
+ ExportAllDeclaration(path, state) {
27
+ handleSource(path.get('source'), state)
28
+ },
29
+ CallExpression(path, state) {
30
+ if (
31
+ (path.get('callee').isImport() ||
32
+ (path.get('callee').isIdentifier() &&
33
+ path.node.callee.name === 'require')) &&
34
+ path.node.arguments.length === 1 &&
35
+ path.get('arguments')[0].isStringLiteral()
36
+ ) {
37
+ handleSource(path.get('arguments')[0], state)
38
+ }
39
+ },
40
+ },
41
+ }
42
+ }
@@ -0,0 +1,4 @@
1
+ const { register } = require('node:module')
2
+ const { pathToFileURL } = require('node:url')
3
+
4
+ register('babel-register-esm', pathToFileURL(__filename))
@@ -1,36 +1,53 @@
1
- const { promisify } = require('util')
2
- const _resolve = require('resolve')
3
- const resolve = promisify(_resolve)
1
+ const resolve = require('resolve/sync')
4
2
  const path = require('path')
5
3
  const fs = require('fs-extra')
6
4
  const builtinModules = new Set(require('module').builtinModules)
7
5
 
8
- module.exports = async function resolveImportSource({ file, source }) {
6
+ module.exports = function resolveImportSource({
7
+ file,
8
+ source,
9
+ outputExtension,
10
+ }) {
11
+ if (
12
+ /\.[cm]?[tj]sx?$/i.test(source) ||
13
+ source.startsWith('node:') ||
14
+ builtinModules.has(source)
15
+ ) {
16
+ return source
17
+ }
18
+
9
19
  const basedir = path.dirname(file)
10
20
  if (source.startsWith('.')) {
11
- const resolved = await resolve(source, {
21
+ const resolved = resolve(source, {
12
22
  basedir,
13
23
  extensions: [
14
24
  path.extname(file.replace(/\.flow$/, '')),
15
25
  '.mjs',
16
26
  '.cjs',
17
27
  '.js',
28
+ '.jsx',
29
+ '.ts',
30
+ '.tsx',
31
+ '.cts',
32
+ '.ctsx',
33
+ '.mts',
34
+ '.mtsx',
18
35
  ],
19
36
  })
20
- const result = path.relative(basedir, resolved)
37
+ let result = path.relative(basedir, resolved)
38
+ if (outputExtension) result = result.replace(/\.[^.]+$/, outputExtension)
21
39
  return result.startsWith('.') ? result : `./${result}`
22
40
  }
23
- if (source.startsWith('node:') || builtinModules.has(source)) return source
24
41
  const match = /^((?:@[^/]+\/)?[^/]+)(?:\/(.+))?$/.exec(source)
25
42
  if (!match) return source
26
43
  const [, pkg, subpath] = match
27
44
  if (!subpath) return source
28
45
  try {
29
- const packageJsonFile = await resolve(`${pkg}/package.json`)
30
- const packageJson = await fs.readJson(packageJsonFile)
46
+ const packageJsonFile = resolve(`${pkg}/package.json`, { basedir })
47
+ const packageJson = fs.readJsonSync(packageJsonFile)
31
48
  const exportMap = packageJson ? packageJson.exports : undefined
32
49
  if (exportMap && exportMap[`./${subpath}`]) return source
33
- const resolved = await resolve(source, {
50
+ const resolved = resolve(source, {
34
51
  basedir,
35
52
  extensions: [path.extname(file), '.mjs', '.cjs', '.js'],
36
53
  })