简体   繁体   English

在Webpack根目录之外的共享Webpack模块中使用jquery

[英]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库的最佳方法:

  1. Install every js library (eg jquery) with a package manager like npm 使用npm之类的包管理器安装每个js库(例如jquery)
  2. To each library it needs a TypeScript definitions file (eg @types/jquery, to find under npmjs.com ) 对于每个库,它都需要一个TypeScript定义文件(例如@ types / jquery,可在npmjs.com找到
  3. Install this TypeScript definition file also with npm 还要使用npm安装此TypeScript定义文件
  4. Note every TypeScript definition in the tsconfig.json under "files" like 注意tsconfig.json中“文件”下的每个TypeScript定义,例如

"files":[ "node_modules/@types/jquery/index.d.ts", "node_modules/@types/requirejs/index.d.ts", ]

  1. Do this compellingly (point 1-4) with the library requirejs. 使用库requirejs来强制执行此操作(第1-4点)。 This is a js file and module loader. 这是一个js文件和模块加载器。
  2. Now you are ready to load the js library in the TypeScript main file like: 现在您可以将js库加载到TypeScript主文件中,如下所示:

require(["jquery", "urijs"], function($, uri) { // your code });

Other notes: 其他说明:

  • In the index.html file: reference under the script tags only the js bundle files, builded by eg webpack. 在index.html文件中:脚本下的引用仅标记由webpack构建的js捆绑文件。
  • Webpack needs a TypeScript loader. Webpack需要一个TypeScript加载器。
  • Reference exported TypeScript classes in the tsconfig.json file under 'files' and also in the TypeScript file like: 在tsconfig.json文件中的“文件”下以及在TypeScript文件中引用导出的TypeScript类,例如:

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.

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