Parcel: alias is ignored if taget is set to node or electron

Created on 5 Oct 2018  路  6Comments  路  Source: parcel-bundler/parcel

馃悰 bug report

I am trying to compile script using parcel that would run in electron. But i am failing. alias key seems to be ignored for some reason if --target is set either to node or electron, it works fine otherwise.

馃帥 Configuration (.babelrc, package.json, cli command)

Here is my package.json , i am making vue alias to point on a right file, to fix the vue error:

[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

It's referring to runtime script by default if you are import Vue from "vue", so alias should fix that, and it does if --target is set to browser.

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ."
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "electron": "^3.0.2",
    "lodash": "^4.17.11",
    "vue": "^2.5.17"
  },
  "alias": {
    "vue" : "./node_modules/vue/dist/vue.common.js"
  }
}

馃 Expected Behavior

I am expecting alias to work with --target either set to node or electron

馃槸 Current Behavior

Judging from the vue error i get in dev console, alias is ignored. However i am judging only from a single alias entry.

馃敠 Context

I am just trying to build an electron app. Compiling my script using next line:
parcel build src/app.js --target electron
and then linking to the bundle from index.html. Building index.html doesn't help either.

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.9.7
| Electron | 3.0.2
| npm/Yarn |
| Operating System | Windows 10

Question

All 6 comments

Parcel isn't responsible for requires on node projects (as these can just be installed as dependencies...).
Try --bundle-node-modules if you want to bundle the node_modules

Ah thank for your information, very interesting, and i guess that makes perfect sense, hmm... any workarounds for my problem? I don't think i want to bundle node modules, so i wonder if i could make it to require the right vue lib script, without explicitly editing the vue module itself (index.js)?

The only way I can think of doing that is by overwriting require, or writing a babel plugin that rewrites those imports.

Yeah, seems like there is no easy way around. Thanks for the insight anyways.

Is this issue supposed to be closed? I don't think it's intended to ignore aliases under node or electron environments. Definitely, the problem is not easy to solve and probably requires overwriting the require function or an appropriate plugin, but what is the solution? At least in the documentation it could be mentioned that aliases are only supported if target is set to browser.

Workaround and more flexible solution is to use babel-plugin-module-resolver like this:

  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "^@src/(.+)": "./src/\\1"
        }
      }
    ]
  ],

I also was tricked by alias to work with target node, when I wanted to switch to clean import statement in node and electron environments. Has there been a particular reason to implement a custom own alias mechanism? I think it makes things easier to rely on common babel plugins.

Was this page helpful?
0 / 5 - 0 ratings