From cb4ebd679b8e6393ea63bc76a2513522a44f8baf Mon Sep 17 00:00:00 2001 From: asakusuma Date: Tue, 17 Jan 2023 17:11:41 -0800 Subject: [PATCH 1/3] Add test --- __tests__/tests.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/__tests__/tests.ts b/__tests__/tests.ts index 09b61ee..d0a8e66 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 () { @@ -18,7 +19,8 @@ describe('htmlbars-inline-precompile', function () { function transform(code: string) { let x = babel .transform(code, { - filename: 'foo-bar.js', + filename: '/my-computer/workspace/my-package/src/my-file.js', + cwd: '/my-computer/workspace/my-package/', plugins, })! .code!.trim(); @@ -82,6 +84,17 @@ 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('uses the user provided isProduction option if present', function () { let source = 'hello'; let spy = sinon.spy(compiler, 'precompile'); From ecc3c6c9723f7444f51a65d7dad96076d54c3d62 Mon Sep 17 00:00:00 2001 From: asakusuma Date: Tue, 17 Jan 2023 17:22:35 -0800 Subject: [PATCH 2/3] Add another test if filename is already relative --- __tests__/tests.ts | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/__tests__/tests.ts b/__tests__/tests.ts index d0a8e66..c39bc86 100644 --- a/__tests__/tests.ts +++ b/__tests__/tests.ts @@ -16,13 +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: '/my-computer/workspace/my-package/src/my-file.js', - cwd: '/my-computer/workspace/my-package/', - 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; } @@ -95,6 +101,21 @@ describe('htmlbars-inline-precompile', function () { 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'); From 7a0104235ca7265200c8f57eb0e1a3b75b0e0a48 Mon Sep 17 00:00:00 2001 From: asakusuma Date: Tue, 17 Jan 2023 17:11:41 -0800 Subject: [PATCH 3/3] use relative path --- src/plugin.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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