Учитывая 3 npm проектов с Webpack и Typescript.
├── project1/
│ ├── tsconfig.json
│ ├── package.json
│ ├── src/
│ │ └── index.ts
│ └── webpack1.config.js
├── project2
├── index.ts
├── tsconfig.json
│ ├── package.json
│ └── webpack2.config.js
└── project3/
└── index.html
Как можно импортировать Project1
в Project2
и затем использовать Project2
в теге sr c html? Я пробовал его тысячами разных способов (npm ссылка), и нет возможности провести некоторые тесты с модулем Project 2. Всегда возникают проблемы с модулем, которого нет в Project2
.
* 1009. * Вот мои конфиги:
ПРОЕКТ1 {
"name": "project1",
"version": "1.0.0",
"description": "",
"main": "dist/bundle.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"types": "dist/index.d.ts",
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"install": "^0.13.0",
"npm": "^6.13.6",
"ts-loader": "^6.2.1",
"typescript": "^3.7.5",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
}
}
const path = require('path');
module.exports = {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
library: 'COMMON1',
},
};
-------------------------------------------------src/index.ts
export class Test1{
public Init(){
console.log("Hello World!!");
}
}
ПРОЕКТ2
module.exports = {
entry: 'index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
library: 'common2',
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};
----------------------------tsconfig.json
{
"name": "project2",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"main": "dist/bundle.js",
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-loader": "^6.2.1",
"typescript": "^3.7.5",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
},
"dependencies": {}
}
---------------------------------------------------------
{
"compilerOptions": {
"incremental": true,
"target": "es5",
"module": "CommonJS",
"outDir": "./dist/",
"strict": true,
"baseUrl": "./",
"paths": {
"project1": [ "../project1/src" ],
"project1/*": [ "../project1/src/*" ]
},
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}
-------------------------------------------------index.ts
import { Test1} from "project1"; //Alwayas error!!
new Test1().Init();
const test1= 1;
export { test1, Test1};
ПРОЕКТ3
<!DOCTYPE html>
<html lang="en">
<head>
<script src="../project2/dist/bundle.js"></script>
<script>// I need common2.test1</script>
...
В основном, как я могу получить common2.test1
в html.