alonrbar/easy-template-x

The package is not compatible with Vite in production build: `require is not defined`

Closed this issue · 1 comments

ezze commented

I am migrating my project from webpack to vite. When building the project for production using Rollup under the hood I get the following error:

require is not defined

The reason is that Vite works only with ES modules and is not compatible with CommonJS. Diving deeper I stated that require is used in src/compilation/scopeData.ts:

const getProp = require("lodash.get");

As the result, ESM version of this package contains this require in the build. In order to fix the issue the dependency should be imported like this:

import getProp from 'lodash.get';

As a quick workaround I managed to apply the following patch to 3.0.0 using patch-package:

diff --git a/node_modules/easy-template-x/dist/es/easy-template-x.js b/node_modules/easy-template-x/dist/es/easy-template-x.js
index 641554d..f0d43f0 100644
--- a/node_modules/easy-template-x/dist/es/easy-template-x.js
+++ b/node_modules/easy-template-x/dist/es/easy-template-x.js
@@ -1163,7 +1163,7 @@ class DelimiterSearcher {
 
 }
 
-const getProp = require("lodash.get");
+import getProp from 'lodash.get';
 
 class ScopeData {
   static defaultResolver(args) {
ezze commented

Created PR to fix this issue.