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

Bundling with webpack does not work #67

Closed
oertels opened this issue Jan 12, 2017 · 13 comments · Fixed by #276
Closed

Bundling with webpack does not work #67

oertels opened this issue Jan 12, 2017 · 13 comments · Fixed by #276

Comments

@oertels
Copy link

oertels commented Jan 12, 2017

Hi,

running fontkit (and pdfkit) in an expressjs app works fine locally, but when I'm building it with webpack, it returns: Error: ENOENT: no such file or directory, open '/var/www/backend/data.trie'

This is my webpack config:

var Webpack = require('webpack');
var path = require('path');
var buildPath = path.resolve(__dirname, 'build');
var mainPath = path.resolve(__dirname, 'src', 'app.js');

var config = {

  target: 'node',
  entry: mainPath,
  output: {
    path: buildPath,
    filename: 'bundle.js'
  },
  node: {
    console: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    dns: 'empty',
    __dirname: false,
    __filename: false
  },
  module: {
    loaders: [
        {
            test: /\.js$/,
            loader: 'babel',
            query: {
                presets: ['es2015']
            }
        },
        {
            test: /\.json$/,
            loader: 'json'
        },
        ]
  }
};

module.exports = config;

When I copy the file data.trie from the node_modules folder to the same directory where I run the node app, it continues, but the same problem appears with use.trie then.
Any idea how to solve it?

@joseSantacruz
Copy link

I have the same issue any help

@Pomax
Copy link
Contributor

Pomax commented Apr 24, 2017

Is there a trace available that explains where that error is thrown?

@liborm85
Copy link
Contributor

In pdfmake we using brfs transformation ( see https://github.com/bpampuch/pdfmake/blob/33df9bbec76b358bec8d08bc49a1d2892445a3fa/webpack.config.js#L63 ). And then no external files are required.

@dana-ross
Copy link

@Pomax Here's a stack trace:

dave@DMR-SURFACEPRO3:/mnt/c/Users/dave/Code/font-server$ node app/dist/bundle.js
fs.js:584
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open '//data.trie'
    at Object.fs.openSync (fs.js:584:18)
    at Object.fs.readFileSync (fs.js:491:33)
    at Object.<anonymous> (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:41204:27)
    at Object.module.exports.categories (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:41338:30)
    at __webpack_require__ (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:20:30)
    at Object.<anonymous> (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:21372:31)
    at Object.<anonymous> (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:36269:30)
    at __webpack_require__ (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:20:30)
    at Object.<anonymous> (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:9158:17)
    at __webpack_require__ (/mnt/c/Users/dave/Code/font-server/app/dist/bundle.js:20:30)

@albertoforni
Copy link

I got it working by installing transform-loader -> npm i transform-loader --save-dev

and then in the webpack.config.js

    module: {
        loaders: [
            ...loaders,
            {enforce: 'post', test: /fontkit[\/\\]index.js$/, loader: "transform?brfs"},
	    {enforce: 'post', test: /unicode-properties[\/\\]index.js$/, loader: "transform?brfs"},
            {enforce: 'post', test: /linebreak[\/\\]src[\/\\]linebreaker.js/, loader: "transform?brfs"}
        ]
    },

I hope this helps.

@kkesley
Copy link

kkesley commented Apr 24, 2018

Hello, I got fs is undefined when using transform-loader?brfs in fontkit. How do you guys resolve this issue?

@Pomax
Copy link
Contributor

Pomax commented Apr 30, 2018

Looking at https://github.com/webpack-contrib/transform-loader#webpack-2-config-example, it feels like the confs shown so far are not quite in line with how brfs should be used with webpack. It might be worth replicating the code as shown in the transform-loader README.md and seeing if that solves the problem.

@Tinoooo
Copy link

Tinoooo commented Mar 12, 2019

@albertoforni suggestion helps.
For newer webpack versions, this works for me:

module: {
        rules: [
            {enforce: 'post', test: /unicode-properties[\/\\]index.js$/, loader: "transform-loader?brfs"},
            {enforce: 'post', test: /fontkit[\/\\]index.js$/, loader: "transform-loader?brfs"},
            {enforce: 'post', test: /linebreak[\/\\]src[\/\\]linebreaker.js/, loader: "transform-loader?brfs"}
        ]
    }

Are there any plans to implement a solution, that works without these workarounds?

darh added a commit to cortezaproject/corteza-server-corredor that referenced this issue Mar 18, 2020
See foliojs/fontkit#67
Proposed solutions (adt. webpack config rules) do not help.
@hyvyys
Copy link

hyvyys commented Aug 14, 2020

I don't really want to mess with my config just to get this to work in the browser, this should work out of the box :(

I had no idea how to modify the webpack config via chainWebpack (I'm trying to use fontkit in a Vue app that uses Vue-CLI), but the below seems to work – it eliminates the fs.readFileSync is not a function error upon importing fontkit.

The error still occurs when trying to call fontkit.open() or fontkit.openSync() but no error is raised when using fontkit.create() instead.

module.exports = {
  chainWebpack: config => {
      config.module
      .rule('eslint')
      .use('eslint-loader')
      .tap(opts => ({ ...opts, emitWarning: true }));
      
      config.module
      .rule('fontkit')
      .test(/fontkit[\/\\]index.js$/)
      .use('transform-loader')
      .loader("transform-loader?brfs")

      config.module
      .rule('unicode-properties')
      .test(/unicode-properties[\/\\]index.js$/)
      .use('transform-loader')
      .loader("transform-loader?brfs")

      config.module
      .rule('linebreak')
      .test(/linebreak[\/\\]src[\/\\]linebreaker.js/)
      .use('transform-loader')
      .loader("transform-loader?brfs")

      // The above trying to replicate this:
      // {enforce: 'post', test: /fontkit[\/\\]index.js$/, loader: "transform?brfs"},
      // {enforce: 'post', test: /unicode-properties[\/\\]index.js$/, loader: "transform?brfs"},
      // {enforce: 'post', test: /linebreak[\/\\]src[\/\\]linebreaker.js/, loader: "transform?brfs"}
  }
}

And then

  async openFromFile(file) {
    const arrayBuffer = await file.arrayBuffer();
    const f = fontkit.create(new Buffer(arrayBuffer));
  }

@blikblum
Copy link
Member

Try fontkit-next package. It has this PR applied and should work

@howlettga
Copy link

howlettga commented Apr 1, 2021

I don't think this is an adequate solution. transform-loader has not been updated for 4 years and doesn't purport to support greater than webpack 2.x. I am using webpack 5.x and would love to be able to handle this module with asset modules at the most and no crazy work-arounds. It would be really nice if fontkit handled importing these .trie files using standard import or require statements instead of reading directly off the filesystem so webpack can pick it up. Bundling a module is not an unusual requirement. Is there any reason why it can't do this? I will take a look when I have some time to investigate

@Pomax
Copy link
Contributor

Pomax commented Apr 1, 2021

at the very least, I'd say "see if it works with esbuild instead" given the monster that webpack is these days, and given how much faster (and arguably better) esbuild is.

That said, the way fontkit works it's entirely likely that still won't get you to something that works without running the .trie data build yourself as an explicit step in your build.

@michaeljherrmann
Copy link

The following worked for me:

const nodeExternals = require('webpack-node-externals');

config = {
    ...
    target: 'node',
    externals: [nodeExternals()],
    ...
};

As described by webpack-node-externals:

When bundling with Webpack for the backend - you usually don't want to bundle its node_modules dependencies

That library is helpful to handle all node_modules, but you can also just specify fontkit in externals

config.externals = {
    'fontkit-next': 'commonjs fontkit-next',
};

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

Successfully merging a pull request may close this issue.