[英]Using jquery in a shared webpack module, which is outside the webpack root
I have multiple layouts, which depend on some shared typescript files, thats why I want to share this files with multiple layouts which are using webpack. 我有多个布局,这取决于一些共享的打字稿文件,这就是为什么我要与使用webpack的多个布局共享此文件的原因。
I'm trying to include jquery
in my ajax.ts
and get this error: 我试图在我的
ajax.ts
包含jquery
并得到此错误:
ERROR in ../_shared/ajax.ts
Module not found: Error: Can't resolve 'jquery' in '{...}/layouts/_shared'
_shared/ajax.ts: _shared / ajax.ts:
import * as $ from 'jquery';
export class AjaxListener {
constructor(){
// use jquery with $
}
}
layoutA/app.ts: layoutA / app.ts:
import { AjaxListener } from "../_shared/ajax";
import { App } from "../_shared/app";
let app = new App();
let ajaxListener = new AjaxListener();
My Folder Structure looks like this: 我的文件夹结构如下所示:
/layouts
/_shared
/ajax.ts
/app.ts
/layoutA
/app.ts
/webpack.config.js
/package.json (contains "@types/jquery": "^2.0.47" and "jquery": "^3.2.1")
/tsconfig.json
tsconfig.json: tsconfig.json:
{
"compilerOptions": {
"module": "es6",
"target": "es6",
"sourceMap": true
},
"exclude": [
"node_modules",
"typings/browser",
"typings/browser.d.ts",
"typings/main",
"typings/main.d.ts"
]
}
webpack.config.js: webpack.config.js:
const ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require("path");
var distPath = path.join(__dirname, "dist");
module.exports = [
{
entry: {
app: ['./app.sass', './app.ts']
},
resolve: {
extensions: [".tsx", ".js", ".ts", ".sass"]
},
cache: false,
output: {
path: distPath,
filename: "[name]_scripts.js"
},
module: {
rules : [
{
enforce: 'pre',
// "test" is commonly used to match the file extension
test: /\.js$/,
loader: "source-map-loader"
},
{
// "test" is commonly used to match the file extension
test: /\.tsx?$/,
exclude: [/node_modules/],
use: [ 'babel-loader', 'ts-loader' ]
},
{
test: /\.sass$/,
use: [
{
loader: "style-loader" // creates style nodes from JS strings
},{
loader: "css-loader", options: { sourceMap: true } // translates CSS into CommonJS
},{
loader: "sass-loader", options: { sourceMap: true } // compiles Sass to CSS
}
]
}
]
},
devtool: "eval"
}
]
If I try to import jquery inside layoutA/app.ts file (webpack root), it works fine. 如果我尝试将jquery导入layoutA / app.ts文件(webpack根目录)内,则工作正常。 Since the ajax.ts lives outside this folder, which is the best way to import libraries like jquery, lodash etc. in these files?
由于ajax.ts位于此文件夹之外,这是在这些文件中导入诸如jquery,lodash等库的最佳方法吗?
The following points must be observed for the best way to load js libraries in your context: 必须注意以下几点,以便在您的上下文中加载js库的最佳方法:
"files":[ "node_modules/@types/jquery/index.d.ts", "node_modules/@types/requirejs/index.d.ts", ]
require(["jquery", "urijs"], function($, uri) { // your code });
Other notes: 其他说明:
import {Car} from "./classes/Car";
Hope it helps you, to get a proper structur! 希望它对您有所帮助,以获取适当的结构!
Supplement: 补充:
Try the following: reference a js library in your ayax.ts like: 尝试以下操作:在ayax.ts中引用js库,例如:
private example = require("./[path to npm library]/node_modules/test/src/test.js");
If you call the library name like 'test' in the require command, then its not possible to resolve 'test'. 如果在require命令中将库名称称为“ test”,则无法解析“ test”。 It try to resolve over the package.json and can not find it because its outside of the root.
它尝试解析package.json,但找不到它,因为它在根目录之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.