jth*_*oof 5 loader typescript webpack umd
我正在尝试构建一个简单的文件,该文件依赖于使用 UMD 导出构建的库。
// main.ts
import { parseTree } from 'jsonc-parser';
const tree = parseTree('{ "name: "test" }');
console.log(tree);
Run Code Online (Sandbox Code Playgroud)
它编译得很好,但是 webpack 会抛出依赖错误:
哈希:85004e3e1bd3582666f5 版本:webpack 2.3.2 时间:959ms 资产大小 块 块名称 dist/bundle.js 61.8 kB 0 [emitted] main build/main.d.ts 0 bytes [emitted] [0] ./~/jsonc-解析器/lib/main.js 40.1 kB {0} [内置] [1] ./~/jsonc-parser/lib 160 字节 {0} [内置] [2] ./~/path-browserify/index.js 6.18 kB {0} [内置] [3] ./~/process/browser.js 5.3 kB {0} [内置] [4] ./src/main.ts 200 字节 {0} [内置] [5] ./ ~/vscode-nls/lib 160 字节 {0} [可选] [内置] [6] ./~/vscode-nls/lib/main.js 5.46 kB {0} [内置]
./~/jsonc-parser/lib/main.js 中的警告 3:24-31 关键依赖项:require 函数的使用方式无法静态提取依赖项
警告在 ./~/vscode-nls/lib/main.js 118:23-44 关键依赖:依赖的请求是一个表达式
./~/vscode-nls/lib/main.js 模块中的错误未找到:错误:无法解析 '.../webpack-typescript-umd/node_modules/vscode-nls/lib' @ 中的 'fs'。 /~/vscode-nls/lib/main.js 7:9-22 @ ./~/jsonc-parser/lib/main.js @ ./src/main.ts
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/main.ts',
output: {
filename: 'dist/bundle.js'
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['.ts', '.tsx', '.js'] // note if using webpack 1 you'd also need a '' in the array as well
},
module: {
loaders: [ // loaders will work with webpack 1 or 2; but will be renamed "rules" in future
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
configFileName: path.resolve(__dirname, 'tsconfig.json')
}
},
]
}
}
Run Code Online (Sandbox Code Playgroud)
我想保留我的js转译文件,commonjs但我也想捆绑jsonc-parser而不将其重新编译为commonjs.
我在 github 上创建了一个 repo来展示错误。希望这可以帮助你。
您可以简单npm install && npm run dist地重现错误。
我遇到了同样的问题,想分享两种解决该问题的方法:
如果使用的包由一个模块组成,就像 之前的版本一样,1.0.1您jsonc-parser可以将以下内容添加到您的webpack.config.js:
module: {
rules: [
// your rules come here.
],
noParse: /jsonc-parser|other-umd-packages/
},
Run Code Online (Sandbox Code Playgroud)
如果使用的包由多个文件组成,则可以使用umd-compat-loader作为解决方法。将加载程序添加umd-compat-loader到您的并在中package.json配置以下内容:rulewebpack.config.js
module: {
rules: [
// other rules come here.
{
test: /node_modules[\\|/](jsonc-parser|other-umd-packages)/,
use: { loader: 'umd-compat-loader' }
}
]
},
Run Code Online (Sandbox Code Playgroud)
有关如何正确使用的一些提示,可以在此处test找到。最后但并非最不重要的一点是,这要归功于解决方法的 OP。
| 归档时间: |
|
| 查看次数: |
2278 次 |
| 最近记录: |