Je rencontre actuellement des problèmes pour que l'alias fonctionne correctement. D'après ma compréhension, pour que l'alias fonctionne correctement avec webpack, vous devez:
Versions
"TypeScript": "2.8.3",
"webpack": "4.16.2",
"webpack-cli": "3.1.0",
"awesome-TypeScript-loader": "5.2.0",
"html-webpack-plugin": "3.2.0",
"source-map-loader": "^0.2.3",
Voici l'exemple de fichier
Sample.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Footer from '@common/Footer';
export default class Sample{
public static page(): void {
ReactDOM.render(<Footer/>,
document.getElementById('footer')
);
}
}
Avec webpack, il est configuré pour utiliser awesome-TypeScript-loader. Si je comprends bien, il tire parti du TsConfigPathsPlugin pour examiner le tsconfig pour tous les alias, puis le résoudre. Donc, au moment où il arrive sur webpack, l'alias est déjà résolu. Mais ce n'est pas le cas. Dans le bundle.js, je m'attendrais à ne voir aucun @common ou aucun alias et qu'il aurait été converti.
J'ai également ajouté essayé de résoudre l'alias directement dans le webpack ainsi que les alias/aliasFields dans la résolution. Mais toujours pas de chance.
webpack.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const fs = require('fs');
const TsConfigPathsPlugin = require('awesome-TypeScript-loader').TsConfigPathsPlugin;
const ROOT_DIR = path.resolve(__dirname, ".","..");
const config = {
context: path.resolve(__dirname, '.',".."),
mode: "development",
resolve: {
modules: [
],
extensions: ['.ts', '.tsx', '.js', '.jsx'],
plugins: [
new TsConfigPathsPlugin({
configFileName: path.resolve(ROOT_DIR,'tsconfig.json')
})
],
aliasFields: ["@entry", "@common"],
alias: {
"@entry": "entry/",
"@common": "common/"
}
},
entry: {
entryPoint: path.resolve(ROOT_DIR,'entry, 'index.tsx')
},
optimization: {
minimize: false, // debugging purpose
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
},
output: {
filename: "[name]_bundle.js",
path: path.join(ROOT_DIR, 'dist_w'),
},
// Enable sourcemaps for debugging webpack's output.
devtool: "eval-source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-TypeScript-loader'.
{ test: /\.(ts|tsx)$/, loader: "awesome-TypeScript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
plugins: [
//Generate index.html in /dist => https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html', //Name of file in ./dist/
template: path.resolve(ROOT_DIR,'entry-point', 'index.html'),
hash: true,
})
],
stats: { //object
assets: true,
colors: true,
errors: true,
errorDetails: true,
hash: true
// ...
}
};
module.exports = config;
Le message d'erreur que je reçois du pack Web est:
Module not found: Error: Can't resolve '@common\Footer' in 'entry\src'
resolve '@common\Footer' in 'entry\src'
Parsed request is a module
using description file: <root>\package.json (relative path: ./entry/)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
entry\node_modules doesn't exist or is not a directory
<root>\..\..\node_modules doesn't exist or is not a directory
<root>\..\node_modules doesn't exist or is not a directory
<root>\node_modules doesn't exist or is not a directory
looking for modules in <root>\node_modules
using description file: <root>\package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
looking for modules in entry\node_modules
using description file: <root>\package.json (relative path: ./entry-point/node_modules)
Field 'browser' doesn't contain a valid alias configuration
using description file: <root>\package.json (relative path: ./node_modules/@common//Footer)
no extension
Field 'browser' doesn't contain a valid alias configuration
using description file: <root>\package.json (relative path: ./entry-point/node_modules/@common/Footer)
no extension
Field 'browser' doesn't contain a valid alias configuration
<root>\node_modules\@common\Footer doesn't exist
.ts
…
Tout conseil apprécié, merci, D
C'est ce que j'ai utilisé dans l'un de mes projets, vous devrez le configurer dans tsconfig et webpack.config, mais les valeurs sont différentes:
// webpack.config
resolve: {
extensions: [".ts", ".js"],
alias: { "@src": path.resolve(__dirname, "src") },
},
// tsconfig
"paths": {
"@src/*": ["./src/*"]
},