Skip to content

Commit 2f87b62

Browse files
chore: remove condition sort
1 parent 26c91c7 commit 2f87b62

File tree

2 files changed

+1
-249
lines changed

2 files changed

+1
-249
lines changed

index.js

Lines changed: 1 addition & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -270,159 +270,6 @@ const sortScripts = onObject((scripts, packageJson) => {
270270
return sortObjectKeys(scripts, order)
271271
})
272272

273-
/**
274-
* Sorts an array in relative terms defined by the `order`
275-
*
276-
* The effect of relative sort is that keys not in the `order` will be kept
277-
* in the order they were in the original array unless it is shifted to
278-
* accommodate a key in the `order`
279-
*/
280-
const relativeOrderSort = (list, order) => {
281-
const orderMap = new Map(
282-
order.map((key, index) => {
283-
return [key, index]
284-
}),
285-
)
286-
const indexes = list.flatMap((item, i) => {
287-
if (orderMap.has(item)) {
288-
return i
289-
}
290-
return []
291-
})
292-
const sortedIndexes = [...indexes].sort((a, b) => {
293-
const aIndex = orderMap.get(list[a])
294-
const bIndex = orderMap.get(list[b])
295-
return aIndex - bIndex
296-
})
297-
298-
const copy = [...list]
299-
sortedIndexes.forEach((desiredIndex, thisIndex) => {
300-
copy[indexes[thisIndex]] = list[desiredIndex]
301-
})
302-
303-
return copy
304-
}
305-
306-
const withLastKey = (keyName, { [keyName]: keyValue, ...rest }) =>
307-
typeof keyValue !== 'undefined'
308-
? {
309-
...rest,
310-
[keyName]: keyValue,
311-
}
312-
: rest
313-
314-
const withFirstKey = (keyName, { [keyName]: keyValue, ...rest }) =>
315-
typeof keyValue !== 'undefined'
316-
? {
317-
[keyName]: keyValue,
318-
...rest,
319-
}
320-
: rest
321-
322-
const sortConditionObject = (conditionObject) => {
323-
/**
324-
* Sources:
325-
* - WinterCG maintained list of standard runtime keys: https://runtime-keys.proposal.wintercg.org
326-
* - Node.js conditional exports: https://nodejs.org/api/packages.html#conditional-exports
327-
* - Webpack conditions: https://webpack.js.org/guides/package-exports/#conditions
328-
* - Bun condition: https://bun.sh/docs/runtime/modules#importing-packages
329-
* - Bun macro condition: https://bun.sh/docs/bundler/macros#export-condition-macro
330-
*/
331-
const bundlerConditions = ['vite', 'rollup', 'webpack']
332-
333-
const serverVariantConditions = ['react-server']
334-
const edgeConditions = [
335-
'azion',
336-
'edge-light',
337-
'edge-routine',
338-
'fastly',
339-
'lagon',
340-
'netlify',
341-
'wasmer',
342-
'workerd',
343-
]
344-
345-
const referenceSyntaxConditions = [
346-
'svelte',
347-
'asset',
348-
'sass',
349-
'stylus',
350-
'style',
351-
/**
352-
* 'script' condition must come before 'module' condition, as 'script'
353-
* may also be used by bundlers but in more specific conditions than
354-
* 'module'
355-
*/
356-
'script',
357-
'esmodules',
358-
/**
359-
* 'module' condition must come before 'import'. import may include pure node ESM modules
360-
* that are only compatible with node environments, while 'module' may be
361-
* used by bundlers and leverage other bundler features
362-
*/
363-
'module',
364-
'import',
365-
/**
366-
* `module-sync` condition must come before `require` condition and after
367-
* `import`.
368-
*/
369-
'module-sync',
370-
'require',
371-
]
372-
373-
const targetEnvironmentConditions = [
374-
/**
375-
* bun macro condition must come before 'bun'
376-
*/
377-
'macro',
378-
'bun',
379-
'deno',
380-
'browser',
381-
'electron',
382-
'kiesel', // https://runtime-keys.proposal.wintercg.org/#kiesel
383-
'node-addons',
384-
'node',
385-
'moddable', // https://runtime-keys.proposal.wintercg.org/#moddable
386-
'react-native',
387-
'worker',
388-
'worklet',
389-
]
390-
391-
const environmentConditions = ['test', 'development', 'production']
392-
393-
const order = relativeOrderSort(Object.keys(conditionObject), [
394-
/**
395-
* Environment conditions at the top as they are generally used to override
396-
* default behavior based on the environment
397-
*/
398-
...environmentConditions,
399-
/**
400-
* Bundler conditions are generally more important than other conditions
401-
* because they leverage code that will not work outside of the
402-
* bundler environment
403-
*/
404-
...bundlerConditions,
405-
/**
406-
* Edge run-times are often variants of other target environments, so they must come
407-
* before the target environment conditions
408-
*/
409-
...edgeConditions,
410-
/**
411-
* Server variants need to be placed before `referenceSyntaxConditions` and
412-
* `targetEnvironmentConditions` since they may use multiple syntaxes and target
413-
* environments. They should also go after `edgeConditions`
414-
* to allow custom implementations per edge runtime.
415-
*/
416-
...serverVariantConditions,
417-
...targetEnvironmentConditions,
418-
...referenceSyntaxConditions,
419-
])
420-
return withFirstKey(
421-
'types',
422-
withLastKey('default', sortObjectKeys(conditionObject, order)),
423-
)
424-
}
425-
426273
const sortPathLikeObjectWithWildcards = onObject((object) => {
427274
// Replace all '*' with the highest possible unicode character
428275
// To force all wildcards to be at the end, but relative to
@@ -448,24 +295,17 @@ const sortExportsOrImports = onObject((exportOrImports) => {
448295
)
449296

450297
const keys = Object.keys(exportsWithSortedChildren)
451-
let isConditionObject = true
452298
let isPathLikeObject = true
453299
for (const key of keys) {
454300
const keyIsPathLike = key.startsWith('.') || key.startsWith('#')
455-
456-
isConditionObject = isConditionObject && !keyIsPathLike
457301
isPathLikeObject = isPathLikeObject && keyIsPathLike
458302
}
459303

460-
if (isConditionObject) {
461-
return sortConditionObject(exportsWithSortedChildren)
462-
}
463-
464304
if (isPathLikeObject) {
465305
return sortPathLikeObjectWithWildcards(exportsWithSortedChildren)
466306
}
467307

468-
// Object is improperly formatted. Leave it alone
308+
// Object is likely condition object
469309
return exportOrImports
470310
})
471311

tests/fields.js

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -363,91 +363,3 @@ test('exports level 1', macro.sortObject, {
363363
'./sub': './sub/index.js',
364364
},
365365
})
366-
367-
test('exports conditions', macro.sortObject, {
368-
path: 'exports',
369-
value: {
370-
custom: './custom.js',
371-
module: './module.js',
372-
lagon: './lagon.js',
373-
vite: './vite.js',
374-
rollup: './rollup.js',
375-
wasmer: './wasmer.js',
376-
webpack: './webpack.js',
377-
import: './import.js',
378-
types: './types/index.d.ts',
379-
script: './script.js',
380-
node: './node.js',
381-
'edge-light': './edge-light.js',
382-
netlify: './netlify.js',
383-
'react-native': './react-native.js',
384-
stylus: './style.styl',
385-
sass: './style.sass',
386-
esmodules: './esmodules.js',
387-
default: './index.js',
388-
azion: './azion.js',
389-
style: './style.css',
390-
asset: './asset.png',
391-
'react-server': './react-server.js',
392-
browser: './browser.js',
393-
workerd: './workerd.js',
394-
electron: './electron.js',
395-
deno: './deno.js',
396-
fastly: './fastly.js',
397-
worker: './worker.js',
398-
'node-addons': './node-addons.js',
399-
development: './development.js',
400-
bun: './bun.js',
401-
test: './test.js',
402-
require: './require.js',
403-
'edge-routine': './edge-routine.js',
404-
worklet: './worklet.js',
405-
moddable: './moddable.js',
406-
macro: './macro.js',
407-
'module-sync': './module-sync.js',
408-
kiesel: './keisel.js',
409-
production: './production.js',
410-
},
411-
expect: {
412-
types: './types/index.d.ts',
413-
custom: './custom.js',
414-
test: './test.js',
415-
development: './development.js',
416-
production: './production.js',
417-
vite: './vite.js',
418-
rollup: './rollup.js',
419-
webpack: './webpack.js',
420-
azion: './azion.js',
421-
'edge-light': './edge-light.js',
422-
'edge-routine': './edge-routine.js',
423-
fastly: './fastly.js',
424-
lagon: './lagon.js',
425-
netlify: './netlify.js',
426-
wasmer: './wasmer.js',
427-
workerd: './workerd.js',
428-
'react-server': './react-server.js',
429-
macro: './macro.js',
430-
bun: './bun.js',
431-
deno: './deno.js',
432-
browser: './browser.js',
433-
electron: './electron.js',
434-
kiesel: './keisel.js',
435-
'node-addons': './node-addons.js',
436-
node: './node.js',
437-
moddable: './moddable.js',
438-
'react-native': './react-native.js',
439-
worker: './worker.js',
440-
worklet: './worklet.js',
441-
asset: './asset.png',
442-
sass: './style.sass',
443-
stylus: './style.styl',
444-
style: './style.css',
445-
script: './script.js',
446-
esmodules: './esmodules.js',
447-
module: './module.js',
448-
import: './import.js',
449-
'module-sync': './module-sync.js',
450-
require: './require.js',
451-
default: './index.js',
452-
},
453-
})

0 commit comments

Comments
 (0)