flutter_js icon indicating copy to clipboard operation
flutter_js copied to clipboard

how can i build js lib?

Open weixuefeng opened this issue 2 years ago • 1 comments

my js content is:

function testHello() {
    return "hello"
}

let myLib = {
    testHello
}

module.export = myLib

flutter content is:

 await initRuntime();
    var res = javascriptRuntime.evaluate("""
                testHello();
                """).stringResult;

it's works.

but when i build it with webpack,

const path = require('path')

module.exports = {
  entry: ['./src/main.js'],
  // devtool: 'source-map',
  mode: 'production',
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  output: {
    filename: 'vara-network.min.js',
    path: path.resolve(__dirname, 'dist'),
    globalObject: "this",
    library: 'varaNetwork',
    libraryTarget: 'umd'
  }
};
build: npx webpack --config webpack.config.js
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.varaNetwork=t():e.varaNetwork=t()}(this,(()=>(()=>{var e={225:(e,t,o)=>{(e=o.nmd(e)).export={testHello:function(){return"hello"}}}},t={};function o(r){var n=t[r];if(void 0!==n)return n.exports;var d=t[r]={id:r,loaded:!1,exports:{}};return e[r](d,d.exports,o),d.loaded=!0,d.exports}return o.nmd=e=>(e.paths=[],e.children||(e.children=[]),e),o(225)})()));

it can not works. and got error:

address is:"ERROR: Can't find variable: testHello \n  at global code@"

weixuefeng avatar Nov 16 '23 01:11 weixuefeng

Found a solution using: libraryTarget: 'var', globalObject: 'this',

in the webpack config

stonesong avatar Mar 01 '24 04:03 stonesong