diff --git a/__tests__/tests.ts b/__tests__/tests.ts index 09b61ee..c39bc86 100644 --- a/__tests__/tests.ts +++ b/__tests__/tests.ts @@ -8,6 +8,7 @@ import { stripIndent } from 'common-tags'; import { EmberTemplateCompiler } from '../src/ember-template-compiler'; import sinon from 'sinon'; import { ExtendedPluginBuilder } from '../src/js-utils'; +import assert from 'assert'; import 'code-equality-assertions/jest'; describe('htmlbars-inline-precompile', function () { @@ -15,12 +16,19 @@ describe('htmlbars-inline-precompile', function () { let compiler: EmberTemplateCompiler = { ...require('ember-source/dist/ember-template-compiler') }; let plugins: ([typeof HTMLBarsInlinePrecompile, Options] | [unknown])[]; - function transform(code: string) { + function transform(code: string, opts?: babel.TransformOptions) { let x = babel - .transform(code, { - filename: 'foo-bar.js', - plugins, - })! + .transform( + code, + Object.assign( + { + filename: '/my-computer/workspace/my-package/src/my-file.js', + cwd: '/my-computer/workspace/my-package/', + plugins, + }, + opts || {} + ) + )! .code!.trim(); return x; } @@ -82,6 +90,32 @@ describe('htmlbars-inline-precompile', function () { expect(spy.firstCall.lastArg).toHaveProperty('contents', source); }); + it('moduleName is defined and is a relative path', function () { + let source = 'hello'; + + const result = transform( + `import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('${source}');` + ); + const match = result.match(/"moduleName": ?"(.+)"/); + assert(match); + expect(match[1]).toEqual('src/my-file.js'); + }); + + it('moduleName is defined and is a relative path even if filename is already relative', function () { + let source = 'hello'; + + const result = transform( + `import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('${source}');`, + { + filename: 'some/relative/path.js', + cwd: '/my-computer/workspace/my-package/', + } + ); + const match = result.match(/"moduleName": ?"(.+)"/); + assert(match); + expect(match[1]).toEqual('some/relative/path.js'); + }); + it('uses the user provided isProduction option if present', function () { let source = 'hello'; let spy = sinon.spy(compiler, 'precompile'); diff --git a/src/plugin.ts b/src/plugin.ts index 87e45f9..9498320 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -6,6 +6,7 @@ import { ExpressionParser } from './expression-parser'; import { JSUtils, ExtendedPluginBuilder } from './js-utils'; import type { EmberTemplateCompiler, PreprocessOptions } from './ember-template-compiler'; import { LegacyModuleName } from './public-types'; +import { relative } from 'path'; export * from './public-types'; @@ -96,6 +97,8 @@ interface State { program: NodePath; lastInsertedPath: NodePath | undefined; filename: string; + file: Babel.BabelFile; + cwd: string; } export function makePlugin(loadOptions: (opts: EnvSpecificOptions) => Options) { @@ -277,6 +280,7 @@ function buildPrecompileOptions( } let jsutils = new JSUtils(babel, state, target, userTypedOptions.locals as string[], state.util); let meta = Object.assign({ jsutils }, userTypedOptions?.meta); + return Object.assign( { contents: template, @@ -285,7 +289,7 @@ function buildPrecompileOptions( // TODO: embroider's template-compiler allows this to be overriden to get // backward-compatible module names that don't match the real name of the // on-disk file. What's our plan for migrating people away from that? - moduleName: state.filename, + moduleName: relative(state.cwd, state.filename), // This is here so it's *always* the real filename. Historically, there is // also `moduleName` but that did not match the real on-disk filename, it