mmcglynn
mmcglynn

Reputation: 7672

Why can't I run my esbuild script outside of package.json?

I am able to minify a CSS file when using esbuild in package.json like so.

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "esbuild foo/src/css/public.css --minify=true --sourcemap --outfile=foo/css/public.css"
  },

But when I move the script to an external file, I get a path resolution error. I have tried every possible path and still get the error.

require('esbuild').buildSync({
    entryPoints: ['foo/src/css/public.css'],
    outfile: 'foo/css/public.css',
    minify: true,
    bundle: false,
    sourcemap: true,
});

This is the error.

  errors: [
    {
      detail: undefined,
      id: '',
      location: null,
      notes: [],
      pluginName: '',
      text: 'Could not resolve "foo/src/css/public.css"'
    }

What do I need to do to make the very simple function work?

Upvotes: 1

Views: 99

Answers (1)

Lastik
Lastik

Reputation: 981

I've tested your script and it completed successfully without any flaws.

In order for your script to work you need to specify css file path relative to your package.json location.

If package.json is located in <project dir>/package.json and has the following scripts section:

  "scripts": {
    "build-css": "node scripts/build-css.js"
  },

And your script is placed in <project dir>/scripts/build-css.js and has the following code:

require('esbuild').buildSync({
    entryPoints: ['css/public.css'],
    outfile: 'css/out/public.css',
    minify: true,
    bundle: false,
    sourcemap: true,
});

You need to have your public.css placed into <project dir>/css/public.css for your script to work.

Upvotes: 0

Related Questions