简体   繁体   English

为什么Webpack文档中的“ exports-loader”示例不起作用?

[英]Why does the “exports-loader” example in Webpack's documentation not work?

Webpack provides the example below in its shimming documentation . 的WebPack提供下面的例子及其在匀场文档 In the global exports portion of that page, it gives the following example. 在该页面的全球出口部分中,给出以下示例。

webpack.config.js webpack.config.js

module.exports = {
    module: {
        rules: [
            {
                test: require.resolve('globals.js'),
                use: exports-loader?file,parse=helpers.parse
            }
        ]
    }
}

./src/globals.js ./src/globals.js

var file = 'blah.txt';
var helpers = {
    test: function() { console.log('test something'); },
    parse: function() { console.log('parse something'); }
};

But when I attempt to build, I get: 但是当我尝试构建时,我得到:

ERROR in ./webpack.config.js
Module not found: Error: Can't resolve 'globals.js' in '/workspace/my-app'

Why is globals.js not resolving, and why does the example in their documentation assume it will? 为什么globals.js无法解析,为什么他们的文档中的示例假设可以解决? Am I missing something? 我想念什么吗? Thanks. 谢谢。

Getting this to work with a global exports-loader configuration 使它与全局exports-loader配置一起使用

I have this working with the following setup: 我使用以下设置进行此操作:

src/deps.js // this file just declare a global file variable src / deps.js //此文件仅声明一个全局file变量

const file = 'this is the file';

src/app.js // entry point of the webpack bundle. src / app.js // webpack包的入口点。 It import 's deps.js (even if deps.js does not have an export statement, thanks to export-loader ): import deps.js (由于export-loader ,即使deps.js没有export语句):

import file from './deps.js'
console.log(file);

webpack.config.js // webpack configuration file webpack.config.js // webpack配置文件

module.exports = {
  entry: __dirname + '/src/app.js',
  mode: 'development',
  module: {
    rules: [
      {
        test: /deps.js/,
        use: 'exports-loader?file',
      }
    ]
  }
}

package.json // so we can run webpack locally to the project package.json //这样我们就可以在项目本地运行webpack

{
  "name": "exports-loader-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "webpack": "node_modules/webpack/bin/webpack.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "exports-loader": "^0.7.0",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3"
  }
}

With this setup, assuming webpack.config.js , package.json and src/ are in the root of the project, do: 通过此设置,假设webpack.config.jspackage.jsonsrc/位于项目的根目录中,请执行以下操作:

$ npm run webpack

To bundle the scripts, then: 要捆绑脚本,然后:

$ node dist/main.js to check that the file variable is being loaded (to load this in a browser will do the same). $ node dist/main.js来检查是否正在加载file变量(将其加载到浏览器中将执行相同的操作)。

Getting this to work with an import specific configuration. 使它与import特定的配置一起使用。

(this comes from this other answer ). (这来自另一个答案 )。

In order to do so, you need to use just the exports-loader , without any further configuration when you load it in the webpack.config.js : 为了做到这一点,你需要use只是exports-loader ,无需任何进一步的配置,当你在加载webpack.config.js

use: 'exports-loader',

And then specify the variables to wrap in an export clause in every import statement: 然后在每个import语句中指定要包装在export子句中的变量:

import file from 'exports-loader?file!./deps.js'

Why the require.resolve() syntax is not working? 为什么require.resolve()语法不起作用?

I really don't know. 我真的不知道 The test clause expects a regex as far as I know (that's why it is called test in fact, because of the test method of regex's in javascript) and I'm not used to other kind of valid syntaxes. 据我所知, test子句期望使用正则表达式(这就是为什么实际上将其称为test的原因,因为javascript中正则表达式的test方法),而且我不习惯其他有效语法。 I see that in your snippet: 我在您的代码段中看到了这一点:

module.exports = {
    module: {
        rules: [
            {
                test: require.resolve('globals.js'),
                use: exports-loader?file,parse=helpers.parse
            }
        ]
    }
}

The use value does not have string quotes. use值没有字符串引号。 I wonder if this is broking the config and then you get a misleading error, I don't know. 我不知道这是否破坏了配置,然后您得到一个误导性错误,我不知道。 I actually believe you just didn't paste the quotes when copy and pasting to stack overflow. 我实际上相信您只是在复制和粘贴到堆栈溢出时没有粘贴引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM