razon
December 13, 2022, 8:06am
1
Hi, I found that the import
behavior is different between assets and node_modules if there is extension specified at the end of path.
{{ $js := resources.Get "js/index.ts" }}
{{ $js = $js | js.Build }}
<script src="{{ $js.Permalink }}"></script>
// assets/js/index.ts
import 'foo/index.js'; // Specify the extension as `js`.
// assets/foo/index.ts
console.log('assets/foo/index.ts');
// node_modules/foo/index.ts
console.log('node_modules/foo/index.ts');
The node_modules/foo/index.ts
will be imported, which is unexpected.
Structure
βββ assets
β βββ foo
β β βββ index.ts
β βββ js
β βββ index.ts
βββ node_modules
β βββ foo
β βββ index.ts
Full example can be found on GitHub - razonyang/hugo-lab at typescript-import .
Background
I want to create a WorkBox module that mount JS lib on assets
, but canβt resolve the dependencies, since their source code import modules with .js
ext.
For example: https://github.com/GoogleChrome/workbox/blob/v6/packages/workbox-routing/src/index.ts .
bep
December 13, 2022, 1:19pm
2
The logic in Hugo is 1) Try to find it in /assets 2) If not, let ESBuild find it in node_modules
I find the above behaviour a little surprising, but if Node/ESBuild supports this, then Hugo should. But you need to create a GitHub issue.
As to a workaround, I think you can import ts files into js files, so if you instead create
assets/foo/index.js
wouldnβt that work?
1 Like
razon
December 13, 2022, 1:26pm
3
Yes, rename to assets/foo/index.js
works, but the third party source code (mount on assets
) is out of my control. The source files are saved with ts
ext, but import
with js
ext.
// assets/js/index.ts
import 'third-party'; // import third party lib.
/* Third party codes out of my control that mounted by module. */
// assets/third-party/index.ts
import third-party/foo.js // imports with .js ext, but there isn't foo.js.
// assets/third-party/foo.ts
razon
December 14, 2022, 5:40am
4
Thanks for the quick fix, whatβs the next release name will be?
I want to specify the required Hugo version on my WorkBox module in advance.
1 Like
system
Closed
December 16, 2022, 8:52am
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.