@ttsc/strip 0.38.0 → 0.38.2

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.
Files changed (2) hide show
  1. package/driver/config.go +10 -93
  2. package/package.json +1 -1
package/driver/config.go CHANGED
@@ -5,7 +5,6 @@ import (
5
5
  "context"
6
6
  "crypto/sha256"
7
7
  "encoding/json"
8
- "errors"
9
8
  "fmt"
10
9
  "os"
11
10
  "os/exec"
@@ -13,11 +12,11 @@ import (
13
12
  "runtime"
14
13
  "strconv"
15
14
  "strings"
16
- "syscall"
17
15
 
18
16
  "github.com/samchon/ttsc/packages/ttsc/driver"
19
17
  "github.com/samchon/ttsc/packages/ttsc/driver/compileraliases"
20
18
  "github.com/samchon/ttsc/packages/ttsc/driver/configloaderscratch"
19
+ "github.com/samchon/ttsc/packages/ttsc/driver/packagemanifest"
21
20
  "github.com/samchon/ttsc/packages/ttsc/driver/resolutioninputs"
22
21
  "github.com/samchon/ttsc/packages/ttsc/driver/windowsjunction"
23
22
  )
@@ -502,6 +501,9 @@ declare const process: {
502
501
  // see stripConfigToolAnchors. An already-open identity store can hold the
503
502
  // loader on a cross-volume TEMP setup without moving the config directory.
504
503
  func loadStripTypeScriptConfigFileWithInputs(location, resolutionRoot string) (stripLoadedConfig, error) {
504
+ if !filepath.IsAbs(location) {
505
+ return stripLoadedConfig{}, fmt.Errorf("@ttsc/strip: resolve relative config import %s: expected an absolute config path", location)
506
+ }
505
507
  tempDir, err := createStripConfigLoaderTempDir(location, os.TempDir(), os.Getenv("TTSC_NATIVE_PLUGIN_RECORD_ROOT"))
506
508
  if err != nil {
507
509
  return stripLoadedConfig{}, fmt.Errorf("@ttsc/strip: create config loader tempdir: %w", err)
@@ -575,23 +577,11 @@ func loadStripTypeScriptConfigFileWithInputs(location, resolutionRoot string) (s
575
577
  return loaded, nil
576
578
  }
577
579
 
578
- // createStripConfigLoaderTempDir uses this invocation's existing identity
579
- // directory when it is safe for the config; otherwise it preserves the
580
- // historical loader base. Expected filesystem refusal of the preferred base
581
- // also falls back, while unexpected errors remain visible to the caller.
580
+ // createStripConfigLoaderTempDir creates the strip config loader's private
581
+ // scratch directory through the shared allocator, so every native config
582
+ // loader places its scratch by the same rule.
582
583
  func createStripConfigLoaderTempDir(location, systemTemp, recordRoot string) (string, error) {
583
- preferred := configloaderscratch.IdentityTempBase(location, systemTemp, recordRoot)
584
- var tempDir string
585
- var err error
586
- if preferred != "" {
587
- tempDir, err = os.MkdirTemp(preferred, "ttsc-strip-config-")
588
- }
589
- if preferred == "" || (err != nil &&
590
- (os.IsPermission(err) || os.IsNotExist(err) ||
591
- errors.Is(err, syscall.ENOTDIR) || errors.Is(err, syscall.EROFS))) {
592
- tempDir, err = os.MkdirTemp(stripLoaderTempBase(location, systemTemp), "ttsc-strip-config-")
593
- }
594
- return tempDir, err
584
+ return configloaderscratch.CreateLoaderTempDir(location, systemTemp, recordRoot, "ttsc-strip-config-")
595
585
  }
596
586
 
597
587
  // stripTypeScriptLoaderTsconfig generates the JSON content of the ephemeral
@@ -661,46 +651,13 @@ func stripTypeScriptLoaderTsconfig(loader, location, outDir string) string {
661
651
  func stripConfigModuleOption(location string) string {
662
652
  switch strings.ToLower(filepath.Ext(location)) {
663
653
  case ".ts", ".tsx", ".js":
664
- if stripNearestPackageType(location) == "commonjs" {
654
+ if packagemanifest.NearestType(location) == "commonjs" {
665
655
  return "CommonJS"
666
656
  }
667
657
  }
668
658
  return "ESNext"
669
659
  }
670
660
 
671
- // stripNearestPackageType mirrors Node's package-scope lookup for the nearest
672
- // package.json above location: the walk stops at the FIRST manifest it finds,
673
- // and a manifest declaring no "type" means CommonJS rather than a reason to
674
- // keep climbing. Reaching the filesystem root without any manifest also means
675
- // CommonJS. The location is made absolute first, so a relative config path
676
- // cannot end the walk at "." after a single step.
677
- func stripNearestPackageType(location string) string {
678
- absolute, err := filepath.Abs(location)
679
- if err != nil {
680
- absolute = location
681
- }
682
- dir := filepath.Dir(absolute)
683
- for {
684
- raw, err := os.ReadFile(filepath.Join(dir, "package.json"))
685
- if err == nil {
686
- var manifest struct {
687
- Type string `json:"type"`
688
- }
689
- // A manifest that does not parse still bounds the package scope; Node
690
- // refuses to look past it, and CommonJS is the format it defaults to.
691
- if json.Unmarshal(raw, &manifest) == nil && manifest.Type == "module" {
692
- return "module"
693
- }
694
- return "commonjs"
695
- }
696
- parent := filepath.Dir(dir)
697
- if parent == dir {
698
- return "commonjs"
699
- }
700
- dir = parent
701
- }
702
- }
703
-
704
661
  // stripLoaderRootDir returns the widest rootDir that still contains the
705
662
  // loader tsconfig's inputs: the volume root of the loader temp dir (`C:/` on
706
663
  // Windows, `/` elsewhere). A literal "/" is not an ancestor of drive-letter
@@ -715,46 +672,6 @@ func stripLoaderRootDir(outDir string) string {
715
672
  return filepath.ToSlash(vol + `\`)
716
673
  }
717
674
 
718
- // stripLoaderTempBase picks the parent directory for the ephemeral
719
- // config-loader tree. The system temp dir is the default, but when it sits on
720
- // a different volume than the config file (Windows: TEMP on `C:`, project on
721
- // `D:`) the loader cannot work from there — no single tsconfig rootDir spans
722
- // two volumes and filepath.Rel cannot produce a relative import across drives
723
- // (#305) — so the tree is created under the config's nearest
724
- // node_modules/.cache instead, falling back to the config's own directory
725
- // when no node_modules exists (or its .cache cannot be created): any location
726
- // on the config's volume beats the system temp dir, which is guaranteed to
727
- // fail. Returns "" (the os.MkdirTemp default) when the volumes already match.
728
- func stripLoaderTempBase(location, systemTemp string) string {
729
- // A relative location has no volume; "" must not be read as "a volume
730
- // other than the system temp's" — it keeps the historical default (and
731
- // the Rel-failure contract for relative config paths).
732
- vol := filepath.VolumeName(location)
733
- if vol == "" || strings.EqualFold(filepath.VolumeName(systemTemp), vol) {
734
- return ""
735
- }
736
- nodeModules := stripFindNearestNodeModules(filepath.Dir(location))
737
- if nodeModules == "" {
738
- return filepath.Dir(location)
739
- }
740
- // Resolve a linked node_modules (junction/symlink — common in managed
741
- // setups) before descending into it: the ESM runtime realpaths the loader
742
- // module at import time, and a relative config specifier computed from the
743
- // link-form path would resolve against the wrong directory. NTFS junctions
744
- // defeat filepath.EvalSymlinks, so the link component is chased by hand
745
- // first. Realpathing may also land on another volume, which defeats the
746
- // whole point — fall back to the config's directory then.
747
- base := filepath.Join(stripResolveDirLink(nodeModules), ".cache")
748
- if err := os.MkdirAll(base, 0o755); err != nil {
749
- return filepath.Dir(location)
750
- }
751
- real, err := filepath.EvalSymlinks(base)
752
- if err != nil || !strings.EqualFold(filepath.VolumeName(real), filepath.VolumeName(location)) {
753
- return filepath.Dir(location)
754
- }
755
- return real
756
- }
757
-
758
675
  // stripResolveDirLink chases a directory that is itself a symlink or NTFS junction
759
676
  // to its target (bounded against link cycles). os.Readlink is the probe:
760
677
  // it resolves junctions, which report neither ModeSymlink nor an
@@ -881,7 +798,7 @@ func stripTsgoBinaryFrom(anchor string) string {
881
798
  Name string `json:"name"`
882
799
  Version string `json:"version"`
883
800
  }
884
- if err != nil || json.Unmarshal(body, &metadata) != nil {
801
+ if err != nil || packagemanifest.Unmarshal(body, &metadata) != nil {
885
802
  return ""
886
803
  }
887
804
  major, err := strconv.Atoi(strings.SplitN(metadata.Version, ".", 2)[0])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttsc/strip",
3
- "version": "0.38.0",
3
+ "version": "0.38.2",
4
4
  "description": "First-party ttsc plugin that removes configured calls and statements from emitted JavaScript.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",