Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not resolve .hbs extension if import artifact used inside template literal #5254

Closed
7 tasks done
lifeart opened this issue Oct 11, 2021 · 0 comments
Closed
7 tasks done

Comments

@lifeart
Copy link

lifeart commented Oct 11, 2021

Describe the bug

in dev builds Vite remove imports, if usages of imported item not found in codebase.

source:

import Component, { hbs } from '@glimmerx/component';

import HelloWorld from './components/HelloWorld.hbs';

export default class App extends Component {
    static template = hbs`
        <HelloWorld />
   `
}

source in plugin:

import Component, { hbs } from '@glimmerx/component';

export default class App extends Component {
    static template = hbs`
        <HelloWorld />
   `
}

Reproduction

given:

import Component, { hbs } from '@glimmerx/component';

import HelloWorld from './components/HelloWorld.hbs';

export default class App extends Component {
    static template = hbs`
        <HelloWorld />
   `
}

plugin

const templateFileRegex = /\.(hbs)$/;

export default function vitePluginBabelImport(
  plgOptions
) {
  let viteConfig;
  return {
    name: 'vite:hbs',
    enforce: 'pre',
    configResolved(resolvedConfig) {
      viteConfig = resolvedConfig;
    },
    transform(rawCode, id) {
      let code = rawCode;
      if (!templateFileRegex.test(id)) {
        return;
      }


      code = `
        import { hbs } from '@glimmerx/component';
        export default 42;
      `;

      return {
        code,
        map: null,
      };
    },
  };
}


function transformSrcCode(code, fileName, plgOptions, viteConfig) {
    let result = transformSync(code, {
        sourceType: "module",
        babelrc: false,
        configFile: false,
        envName: viteConfig.mode,
        filename: fileName,
        presets: [function(api, opts) {
            return babelGlimmerPreset(api, {...opts, ...{
                isDebug: !viteConfig.isProduction
            }})
        }]
    });
    return result.code;
}

as result, plugin is not invoking, becasue HelloWorld is not part of JS ast

once i'm adding console.log(HelloWorld), plugin invoked correctly and missing dependency exists

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (6) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 33.06 GB / 47.58 GB
  Binaries:
    Node: 16.11.0 - ~\AppData\Local\Volta\tools\image\node\16.11.0\node.EXE
    Yarn: 1.22.5 - ~\AppData\Local\Volta\tools\image\yarn\1.22.5\bin\yarn.CMD
    npm: 8.0.0 - ~\AppData\Local\Volta\tools\image\node\16.11.0\npm.CMD
  Browsers:
    Chrome: 94.0.4606.71
    Edge: Spartan (44.19041.1023.0), Chromium (94.0.992.38)
    Internet Explorer: 11.0.19041.1202
  npmPackages:
    vite: ^2.6.4 => 2.6.5

Used Package Manager

yarn

Logs

No response

Validations

related: glimmerjs/glimmer.js#365

@lifeart lifeart closed this as completed Oct 11, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant