How BabelJS can transpile of nodejs module polyfill (eg. buffer) to ES5 (target js engine: duktape)

After days of searching a solutions, I would much appreciate a bit of help!

The idea is simple: I would like to transpile a js code which use the node ‘buffer’ to pure ES5 so it can run on Duktape (duktape.org). (with webpack and babeljs)

The code have been simplified in a test.js, which use buffer and console.

JS code src/test.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import "core-js/stable"; // just in case it might help
const { Buffer } = require('buffer');
const buf1 = Buffer.alloc(10);
let a = 3;
console.log("BLAH");
</code>
<code>import "core-js/stable"; // just in case it might help const { Buffer } = require('buffer'); const buf1 = Buffer.alloc(10); let a = 3; console.log("BLAH"); </code>
import "core-js/stable"; // just in case it might help
const { Buffer } = require('buffer');

const buf1 = Buffer.alloc(10);

let a = 3;
console.log("BLAH");

webpack.config.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
const path = require('path');
const webpack = require('webpack');
// Provide code node modules in webpack 5+ (eg. path, stream, sys, timers, ...)
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
module.exports = {
//mode: 'development', // otherwise it is using long evals which duktape doesn't like
mode: 'production',
target: 'es5',
entry: [
'core-js/stable',
'./src/test.js'
],
optimization: {
minimize: false,
},
plugins: [
// polyfill the nodejs modules
// use NodePolyfillPlugin instead of resolve.fallback(s) - both have been tried without success
new NodePolyfillPlugin({
onlyAliases: ['buffer', 'Buffer'],
}),
],
module: {
rules: [
{
test: /.(?:js|mjs|cjs)$/,
exclude: /node_modules/,
include: [ path.resolve(__dirname, "src") ],
use: ['babel-loader']
// options in .babelrc
// no targets -> scrictest environment => es5
}
]
},
output: {
filename: 'run-es5-sync-contacts.js',
path: path.resolve(__dirname, 'dist'),
chunkFormat: false,
// copy/past from webpack doc
environment: {
// The environment supports arrow functions ('() => { ... }').
arrowFunction: false,
// The environment supports async function and await ('async function () { await ... }').
asyncFunction: false,
// The environment supports BigInt as literal (123n).
bigIntLiteral: false,
// The environment supports const and let for variable declarations.
const: false,
// The environment supports destructuring ('{ a, b } = obj').
destructuring: false,
// The environment supports an async import() function to import EcmaScript modules.
dynamicImport: false,
// The environment supports an async import() when creating a worker, only for web targets at the moment.
dynamicImportInWorker: false,
// The environment supports 'for of' iteration ('for (const x of array) { ... }').
forOf: false,
// The environment supports 'globalThis'.
globalThis: false,
// The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
module: false,
// The environment supports optional chaining ('obj?.a' or 'obj?.()').
optionalChaining: false,
// The environment supports template literals.
templateLiteral: false,
},
},
performance: {
maxEntrypointSize: 2048000,
maxAssetSize: 2048000
},
};
</code>
<code> const path = require('path'); const webpack = require('webpack'); // Provide code node modules in webpack 5+ (eg. path, stream, sys, timers, ...) const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); module.exports = { //mode: 'development', // otherwise it is using long evals which duktape doesn't like mode: 'production', target: 'es5', entry: [ 'core-js/stable', './src/test.js' ], optimization: { minimize: false, }, plugins: [ // polyfill the nodejs modules // use NodePolyfillPlugin instead of resolve.fallback(s) - both have been tried without success new NodePolyfillPlugin({ onlyAliases: ['buffer', 'Buffer'], }), ], module: { rules: [ { test: /.(?:js|mjs|cjs)$/, exclude: /node_modules/, include: [ path.resolve(__dirname, "src") ], use: ['babel-loader'] // options in .babelrc // no targets -> scrictest environment => es5 } ] }, output: { filename: 'run-es5-sync-contacts.js', path: path.resolve(__dirname, 'dist'), chunkFormat: false, // copy/past from webpack doc environment: { // The environment supports arrow functions ('() => { ... }'). arrowFunction: false, // The environment supports async function and await ('async function () { await ... }'). asyncFunction: false, // The environment supports BigInt as literal (123n). bigIntLiteral: false, // The environment supports const and let for variable declarations. const: false, // The environment supports destructuring ('{ a, b } = obj'). destructuring: false, // The environment supports an async import() function to import EcmaScript modules. dynamicImport: false, // The environment supports an async import() when creating a worker, only for web targets at the moment. dynamicImportInWorker: false, // The environment supports 'for of' iteration ('for (const x of array) { ... }'). forOf: false, // The environment supports 'globalThis'. globalThis: false, // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...'). module: false, // The environment supports optional chaining ('obj?.a' or 'obj?.()'). optionalChaining: false, // The environment supports template literals. templateLiteral: false, }, }, performance: { maxEntrypointSize: 2048000, maxAssetSize: 2048000 }, }; </code>

const path = require('path');
const webpack = require('webpack');
// Provide code node modules in webpack 5+ (eg. path, stream, sys, timers, ...)
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

module.exports = {
  //mode: 'development', // otherwise it is using long evals which duktape doesn't like
  mode: 'production',

  target: 'es5',

  entry: [
    'core-js/stable',
    './src/test.js'
  ],

  optimization: {
    minimize: false,
  },
       
  plugins: [
    // polyfill the nodejs modules
    // use NodePolyfillPlugin instead of resolve.fallback(s) - both have been tried without success
    new NodePolyfillPlugin({
        onlyAliases: ['buffer', 'Buffer'],
    }),
  ],
    
    module: {
        rules: [
            {
                test: /.(?:js|mjs|cjs)$/,
                exclude: /node_modules/,
                include: [ path.resolve(__dirname, "src") ],
                use: ['babel-loader']
                
                // options in .babelrc
                // no targets -> scrictest environment => es5
            }
        ]
    },
    

    
  output: {
      
    filename: 'run-es5-sync-contacts.js',
    path: path.resolve(__dirname, 'dist'),
    
    chunkFormat: false,
      
    // copy/past from webpack doc
    environment: {
            // The environment supports arrow functions ('() => { ... }').
            arrowFunction: false,
            // The environment supports async function and await ('async function () { await ... }').
            asyncFunction: false,
            // The environment supports BigInt as literal (123n).
            bigIntLiteral: false,
            // The environment supports const and let for variable declarations.
            const: false,
            // The environment supports destructuring ('{ a, b } = obj').
            destructuring: false,
            // The environment supports an async import() function to import EcmaScript modules.
            dynamicImport: false,
            // The environment supports an async import() when creating a worker, only for web targets at the moment.
            dynamicImportInWorker: false,
            // The environment supports 'for of' iteration ('for (const x of array) { ... }').
            forOf: false,
            // The environment supports 'globalThis'.
            globalThis: false,
            // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
            module: false,
            // The environment supports optional chaining ('obj?.a' or 'obj?.()').
            optionalChaining: false,
            // The environment supports template literals.
            templateLiteral: false,
        },
  },

    performance: {
            maxEntrypointSize: 2048000,
            maxAssetSize: 2048000
    },
    
};

.babelrc
(“targets” is explicitly omited so it use the strictest javascript standard: es5)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"presets": [
["@babel/preset-env", {
"debug": true,
"corejs": { "version": 3, "proposals": true },
"useBuiltIns": "usage",
"forceAllTransforms": true, // for stricter non-standard environment: duktape
"ignoreBrowserslistConfig": true
}]
]
}
</code>
<code>{ "presets": [ ["@babel/preset-env", { "debug": true, "corejs": { "version": 3, "proposals": true }, "useBuiltIns": "usage", "forceAllTransforms": true, // for stricter non-standard environment: duktape "ignoreBrowserslistConfig": true }] ] } </code>
{
  "presets": [
    ["@babel/preset-env", {
        "debug": true,
        "corejs": { "version": 3, "proposals": true },
        "useBuiltIns": "usage",
        "forceAllTransforms": true, // for stricter non-standard environment: duktape
        "ignoreBrowserslistConfig": true
    }]
  ]
}

The code is properly transpiled, yet, the final output include the code coming from nodes_modules/buffer/index.js which use the js keyword let and const, and therefore failed when executing it using duktape (as duktape does apparently not support let and const).

dist/run-es5-sync-contacts.js still somewhere include the lines from buffer/index.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>...
Buffer.allocUnsafeSlow = function (size) {
return allocUnsafe(size)
}
function fromString (string, encoding) {
if (typeof encoding !== 'string' || encoding === '') {
encoding = 'utf8'
}
if (!Buffer.isEncoding(encoding)) {
throw new TypeError('Unknown encoding: ' + encoding)
}
const length = byteLength(string, encoding) | 0
let buf = createBuffer(length)
const actual = buf.write(string, encoding)
if (actual !== length) {
// Writing a hex string, for example, that contains invalid characters will
// cause everything after the first invalid character to be ignored. (e.g.
// 'abxxcd' will be treated as 'ab')
buf = buf.slice(0, actual)
}
return buf
}
...
</code>
<code>... Buffer.allocUnsafeSlow = function (size) { return allocUnsafe(size) } function fromString (string, encoding) { if (typeof encoding !== 'string' || encoding === '') { encoding = 'utf8' } if (!Buffer.isEncoding(encoding)) { throw new TypeError('Unknown encoding: ' + encoding) } const length = byteLength(string, encoding) | 0 let buf = createBuffer(length) const actual = buf.write(string, encoding) if (actual !== length) { // Writing a hex string, for example, that contains invalid characters will // cause everything after the first invalid character to be ignored. (e.g. // 'abxxcd' will be treated as 'ab') buf = buf.slice(0, actual) } return buf } ... </code>
...
Buffer.allocUnsafeSlow = function (size) {
  return allocUnsafe(size)
}

function fromString (string, encoding) {
  if (typeof encoding !== 'string' || encoding === '') {
    encoding = 'utf8'
  }

  if (!Buffer.isEncoding(encoding)) {
    throw new TypeError('Unknown encoding: ' + encoding)
  }

  const length = byteLength(string, encoding) | 0
  let buf = createBuffer(length)

  const actual = buf.write(string, encoding)

  if (actual !== length) {
    // Writing a hex string, for example, that contains invalid characters will
    // cause everything after the first invalid character to be ignored. (e.g.
    // 'abxxcd' will be treated as 'ab')
    buf = buf.slice(0, actual)
  }

  return buf
}
...

Does anyone has an idea how to tell babeljs to transpile the code imported through NodePolyfillPlugin (or directly specified with resolve.fallback) to a strict ‘es5’ target?

I would expect that output.environment does the trick, yet not for the nodejs modules polyfilled through NodePolyfillPlugin nor resolve.fallback.

I’ve look through all documentation, search for similar problems, tried many (!) combinations, the final code still use let keywords.

Using let directly in test.js, is properly transpiled.

Tried setting resolve.fallback instead of using NodePolyfillPlugin, same result.

With the provided code, console is expected to be missing.

Help will be greatly appreciated!

New contributor

Jean-Luc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật