tungsten 0.2.16 → 0.2.17

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.
@@ -1 +1 @@
1
- {"version":3,"file":"BabelTransformPackageImports.d.ts","sourceRoot":"","sources":["../../src/babelPlugins/BabelTransformPackageImports.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAOrD,OAAO,EAGH,QAAQ,EACX,MAAM,aAAa,CAAA;AACpB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AA4B5C,MAAM,MAAM,QAAQ,GAAG,KAAK,GAAG,KAAK,GAAG,SAAS,GAAG,SAAS,GAAG,MAAM,CAAA;AAErE,MAAM,MAAM,QAAQ,GAAG;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,IAAI,EAAE,QAAQ,CAAA;IACd,WAAW,EAAE,WAAW,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,KAAK,GAAG,SAAS,CAAA;AAEnD,MAAM,MAAM,WAAW,GAAG;IACtB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,WAAW,CAAA;IACjB,iBAAiB,EAAE,MAAM,CAAA;IACzB,aAAa,EAAE,MAAM,CAAA;IACrB,aAAa,EAAE,MAAM,CAAA;CACxB,CAAA;AAGD,MAAM,MAAM,WAAW,GAAG;IACtB,CAAC,GAAG,EAAC,MAAM,GAAG,WAAW,CAAA;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@ export function MakeBabelTransformPackageImports(opts) {
93
93
  try {
94
94
  const packageName = location.startsWith('@') ? location.split('/').slice(0, 2).join('/') : location.split('/')[0];
95
95
  const resolutionPaths = packageName === 'react' || packageName === 'react-dom' ? [opts.projectDir, sourcePackageDir] : [sourcePackageDir];
96
- fullLocation = resolveBareImport(location, resolutionPaths) ?? require.resolve(location, {
96
+ // an import site resolves with the 'import' condition, a require()
97
+ // site with 'require' - see resolvePrecinctDependencies()
98
+ fullLocation = fileType === 'esm' ? resolveBareImport(location, resolutionPaths) ?? require.resolve(location, {
99
+ paths: resolutionPaths
100
+ }) : require.resolve(location, {
97
101
  paths: resolutionPaths
98
102
  });
99
103
  } catch (e) {
@@ -172,21 +176,24 @@ export function MakeBabelTransformPackageImports(opts) {
172
176
  history.push(`${error.dependency} (unresolved)`);
173
177
  return 'Your code that is compiled may depend on a node module - this is not allowed.' + `\nCould not resolve "${error.dependency}".` + `\nImport history:\n ${history.join('\n -> ')}`;
174
178
  }
175
- function resolvePrecinctDependencies(deps, file, sourceFilename, history) {
179
+ function resolvePrecinctDependencies(deps, file, fileType, sourceFilename, history) {
180
+ // Only a file that will be served as ESM may resolve with the 'import' condition.
181
+ // A converted CJS file's require() shim looks its dependencies up by the location
182
+ // recorded here, and the CJS transform resolves the same specifier with the
183
+ // 'require' condition - resolving the two differently loses the file.
184
+ const esmSource = fileType === 'esm';
176
185
  const out = [];
177
186
  for (const dep of deps) {
178
187
  if (dep.startsWith('node:')) continue;
179
188
  try {
180
189
  const packageName = dep.startsWith('@') ? dep.split('/').slice(0, 2).join('/') : dep.split('/')[0];
181
190
  if (packageName === 'react' || packageName === 'react-dom') {
182
- out.push(resolveBareImport(dep, [opts.projectDir]) ?? require.resolve(dep, {
191
+ out.push((esmSource ? resolveBareImport(dep, [opts.projectDir]) : undefined) ?? require.resolve(dep, {
183
192
  paths: [opts.projectDir]
184
193
  }));
185
194
  continue;
186
195
  }
187
- // bare imports resolve the way `import` would first, so that a package
188
- // shipping both builds is taken as ESM rather than through the CJS route
189
- const f = (isBareImport(dep) ? resolveBareImport(dep, [dirname(file)]) : undefined) ?? getCjsRequire(file).resolve(dep);
196
+ const f = esmSource && isBareImport(dep) ? resolveBareImport(dep, [dirname(file)]) ?? getCjsRequire(file).resolve(dep) : getCjsRequire(file).resolve(dep);
190
197
  out.push(f);
191
198
  } catch (error) {
192
199
  throw new UnsupportedCompiledDependencyError(dep, file, history.length === 0 ? [sourceFilename] : history, {
@@ -207,7 +214,7 @@ export function MakeBabelTransformPackageImports(opts) {
207
214
  //console.log('getFileDependencies', 'd', d)
208
215
  return isBareImport(d) ? applyImportAliases(d, fileInfo.type) : d;
209
216
  });
210
- const resolved = resolvePrecinctDependencies(deps, file, sourceFilename, history);
217
+ const resolved = resolvePrecinctDependencies(deps, file, fileInfo.type, sourceFilename, history);
211
218
  return resolved;
212
219
  }
213
220
  function getFileEsmGenIntermediateCacheLocation(file, packageInfo) {
@@ -744,8 +751,10 @@ export function MakeBabelTransformPackageImports(opts) {
744
751
  //console.log(chalk.orange`We did the hacky jsx runtime alias in BabelTransformPackageImports`)
745
752
  location = 'tungsten/jsxRuntime.js';
746
753
  }
747
- // the real file location of the file being imported
748
- const importTarget = require.resolve(location);
754
+ // the real file location of the file being imported.
755
+ // User code is served as ESM, so it resolves with the 'import'
756
+ // condition - see resolvePrecinctDependencies()
757
+ const importTarget = resolveBareImport(location, [dirname(filename)]) ?? require.resolve(location);
749
758
  // Should do some checking here to show helpful errors about bad imports
750
759
  if (importTarget === location) {
751
760
  //console.log()
@@ -94,6 +94,71 @@ describe('tungsten compile', ({
94
94
  });
95
95
  }
96
96
  });
97
+ it('takes the import build of a dual package for an import, and the require build for a require', async () => {
98
+ const projectDir = await mkdtemp(join(tmpdir(), 'tungsten-compile-conditions-test-'));
99
+ const cliLocation = join(findProjectDir(import.meta.url), 'dist', 'bin', 'index.js');
100
+ const sourceDir = join(projectDir, 'src');
101
+ const dualDir = join(projectDir, 'node_modules', 'dual-package');
102
+ const cjsDir = join(projectDir, 'node_modules', 'cjs-package');
103
+ async function copiedToPackages(rel) {
104
+ try {
105
+ await readFile(join(projectDir, '.tungsten/test/.packages', rel), 'utf8');
106
+ return true;
107
+ } catch {
108
+ return false;
109
+ }
110
+ }
111
+ try {
112
+ await mkdir(dualDir, {
113
+ recursive: true
114
+ });
115
+ await mkdir(cjsDir, {
116
+ recursive: true
117
+ });
118
+ await mkdir(sourceDir, {
119
+ recursive: true
120
+ });
121
+ await writeFile(join(projectDir, 'package.json'), '{"type":"module"}');
122
+ await writeFile(join(projectDir, 'tungsten.config.js'), 'export default { codeDir: "src", useRootComponent: false }');
123
+ // ships both builds, as @trpc/server and zod do
124
+ await writeFile(join(dualDir, 'package.json'), JSON.stringify({
125
+ name: 'dual-package',
126
+ version: '1.0.0',
127
+ exports: {
128
+ '.': {
129
+ import: './index.mjs',
130
+ require: './index.cjs'
131
+ }
132
+ }
133
+ }));
134
+ await writeFile(join(dualDir, 'index.mjs'), 'export const dual = "esm"');
135
+ await writeFile(join(dualDir, 'index.cjs'), 'module.exports = { dual: "cjs" }');
136
+ // a CJS package that require()s the dual package
137
+ await writeFile(join(cjsDir, 'package.json'), JSON.stringify({
138
+ name: 'cjs-package',
139
+ version: '1.0.0',
140
+ main: './index.js'
141
+ }));
142
+ await writeFile(join(cjsDir, 'index.js'), 'module.exports = require(\'dual-package\')');
143
+ await writeFile(join(sourceDir, 'entry.js'), 'import { dual } from \'dual-package\'\n' + 'import viaCjs from \'cjs-package\'\n' + 'export { dual, viaCjs }');
144
+ const {
145
+ stdout
146
+ } = await execFileAsync(process.execPath, [cliLocation, 'compile', '--js', '--output', '.tungsten/test', '--quiet'], {
147
+ cwd: projectDir
148
+ });
149
+ expect(stdout).to.contain('Compile complete');
150
+ // the ESM import took ./index.mjs
151
+ expect(await copiedToPackages('dual-package@1.0.0/index.mjs')).to.equal(true);
152
+ // and the require() inside the CJS package took ./index.cjs, so that the
153
+ // converted require() shim can find it in its deps
154
+ expect(await copiedToPackages('dual-package@1.0.0/index.cjs')).to.equal(true);
155
+ } finally {
156
+ await rm(projectDir, {
157
+ recursive: true,
158
+ force: true
159
+ });
160
+ }
161
+ });
97
162
  it('overrides the ignoreDirs and ignorePatterns config entries from the command line', async () => {
98
163
  const projectDir = await mkdtemp(join(tmpdir(), 'tungsten-compile-ignore-test-'));
99
164
  const cliLocation = join(findProjectDir(import.meta.url), 'dist', 'bin', 'index.js');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tungsten",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "bin": {