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

import/require natively available packages #1

Open
martinjuhasz opened this issue Apr 21, 2018 · 5 comments
Open

import/require natively available packages #1

martinjuhasz opened this issue Apr 21, 2018 · 5 comments

Comments

@martinjuhasz
Copy link

import WiFi from 'Wifi'
// or // const WiFi = require('Wifi')
WiFi.on('connected', () => { console.log('We have an IP Address') })

I tried using this webpack configuration with the native available packages from espruino like Wifi or MQTT but webpack failed resolving the paths.

ERROR in ./src/entry.js
Module not found: Error: Can't resolve 'Wifi' in '/Users/martinjuhasz/Downloads/espruino-webpack-babel-sample-master/src'

Any idea how to resolve this?

@wilberforce
Copy link

wilberforce commented May 15, 2018

Not sure how to handle Native modules like wifi. For modules like MQTT you should be able to copy the module source into the src folder.

@andrewwakeling
Copy link
Owner

andrewwakeling commented May 15, 2018

Apologies for not replying earlier.
The following should get webpack to ignore usage of requiring a module:

const Wifi = global.require('Wifi');

I think there might be other ways, but this is a pretty easy approach.

@wilberforce
Copy link

webpack noob here.

Where would that line go?

Wouldn't this end up as part of the build?

@wilberforce
Copy link

wilberforce commented May 19, 2018

I have made some progress with this. I'll clean it up and post - perhaps on the forum.
I have setup a modules folder - and put the espruino modules in there... and then told webpack they are externals..

part is still hard coded...

const path = require('path');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const fs = require('fs');

var directory='./src/modules';
var modules=fs.readdirSync(directory);

var modules_names=modules.map( file => path.parse(file).name )
var modules_paths=modules.map( file => directory + '/' + file )

console.log( modules_names );
console.log( modules_paths );

module.exports = {
  entry: ['./src/modules/DS18B20.js','./src/test.js'],
  externals: {
      'DS18B20': 'require("DS18B20");'
    },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'entry.bundle.js',
//    libraryTarget:'commonjs2',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          //compress: true,
          ecma: 6,
          mangle: false // Disable name mangling to avoid this issue: https://github.com/espruino/Espruino/issues/1367
        },
        //sourceMap: true
      })
    ]
  },
};

@wilberforce
Copy link

wilberforce commented May 19, 2018

So this is finding in the modules folder:

[ 'DS18B20', 'pid-controller', 'ws', 'www' ]
[ './src/modules/DS18B20.js',
  './src/modules/pid-controller.js',
  './src/modules/ws.js',
  './src/modules/www.js' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants