【问题标题】:how to get a javascript class import without loader如何在没有加载器的情况下获取 javascript 类导入
【发布时间】:2018-01-24 15:25:38
【问题描述】:

babel 配置有:

{
  "presets": [
    ["env", { "modules": false }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "comments": false,
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": [ "istanbul" ]
    }
  }
}

和包装:

"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-loader": "^7.1.2",
"babel-plugin-istanbul": "^4.1.1",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",

在 webpack.conf 中,我有一个(完全有货的)babel-loader 部分:

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}
//...
  {
    test: /\.js$/,
    loader: 'babel-loader',
    include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
  },

但我在加载某些文件时遇到错误,例如:

import Auth from './auth.js'

屈服

 error  in ./fe-common/src/auth.js

Module parse failed: Unexpected token (11:18)
You may need an appropriate loader to handle this file type.
| export default class Auth
| {
|     authenticated = this.isAuthenticated()
|     authNotifier = new EventEmitter()
| 

 @ ./fe-common/src/main.js 19:0-28
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

每个请求,项目结构:

robertotomas$ tree -d -I node_modules --noreport
.
├── build
├── config
├── fe-common
│   ├── src
│   │   ├── assets
│   │   │   └── images
│   │   ├── components
│   │   ├── css
│   │   ├── router
│   │   └── store
│   └── static
│       ├── images
│       └── jasper
├── fe-styleguide
│   ├── sass
│   │   └── partials
│   │       └── variables
│   └── src
│       ├── assets
│       │   └── images
│       │       ├── font_awesome
│       │       └── rights_owner_types
│       └── components
├── src
│   ├── assets
│   ├── components
│   ├── lang
│   │   └── en
│   ├── router
│   └── store
│       ├── UsageScenarios
│       └── lib
│           └── Schemas
├── static
└── test
    ├── e2e
    │   ├── custom-assertions
    │   └── specs
    └── unit
        └── specs

【问题讨论】:

  • 你能发布完整的webpack.config.js吗?
  • @NanduKalidindi 它是vue-cli webpack config .. 加上一些别名。它被拆分为多个文件,这使得包含完整的 webpack.config 有点难看(我们正在谈论大约 8 个文件)
  • 知道了,能否请您从加载程序哈希中删除include: 键并尝试一下?
  • 成功了,谢谢!...那里有什么问题?
  • 已添加 :) 再次感谢

标签: javascript webpack babel-loader


【解决方案1】:

我认为问题在于您的加载程序的include: 值。 resolve(src) 没有正确搜索文件。

根据您的项目结构,您可能希望在 webpack 配置中使用它

{
  test: /\.js$/,
  loader: 'babel-loader',
  include: [    
    resolve('fe-common/src'),
    resolve('fe-styleguide/src'),
    resolve('src'),
    resolve('test'), 
    resolve('node_modules/webpack-dev-server/client')
  ]
},

【讨论】:

  • 嗯,在我的描述中添加了 resolve 函数,因为这也是相关的。
  • 看来你已经做对了。我认为您提供给 webpack 的路径存在问题。你的 webpack 配置在哪里?
  • note .. auth.js 在子模块 fe-common 中.. 也许我只需要添加这两行?
  • es2015 来自 node_modules/v-mask 中的 babelrc,但应该没有必要,因为有 dist。所以,我的根 .babelrc 就可以了.. 这个问题可能就是为什么 vue-cli 带有配置为获取父目录的 webpack 的原因
  • Nandu,较早的更改或删除包含解决了问题,但它在节点模块中开始失败,例如`错误无法编译并出现 2 个错误 ./node_modules/v-mask 中的 11:25:29 错误/dist/v-mask.esm.js 模块构建失败:错误:找不到相对于目录“/Users/robertotomas/Work/Github/fe-marketing/node_modules/v‌​-mask”的预设“es2015”。此更改不能解决问题。
猜你喜欢
  • 2021-04-22
  • 2010-10-23
  • 2011-03-28
  • 2015-01-27
  • 1970-01-01
  • 2022-12-25
  • 2012-05-17
  • 1970-01-01
  • 2021-12-10
相关资源
最近更新 更多