Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ 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 () {
// eslint-disable-next-line @typescript-eslint/no-var-requires
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;
}
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 5 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -96,6 +97,8 @@ interface State<EnvSpecificOptions> {
program: NodePath<t.Program>;
lastInsertedPath: NodePath<t.Statement> | undefined;
filename: string;
file: Babel.BabelFile;
cwd: string;
}

export function makePlugin<EnvSpecificOptions>(loadOptions: (opts: EnvSpecificOptions) => Options) {
Expand Down Expand Up @@ -277,6 +280,7 @@ function buildPrecompileOptions<EnvSpecificOptions>(
}
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,
Expand All @@ -285,7 +289,7 @@ function buildPrecompileOptions<EnvSpecificOptions>(
// 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
Expand Down