Does not work - produced paths for require are incorrect.
See original GitHub issuePlease, observe the following TypeScript file:
import { Component } from "@angular/core";
@Component({
selector: 'app-root',
templateUrl: './app/app.component.html'
})
export class AppComponent {
}
And it works fine before I use angular2-template-loader. When I am trying to use the latter and run webpack it fails with the following message:
polyfills.js 283 kB 1 [emitted] polyfills
vendor.js 3.32 MB 2 [emitted] vendor
main.js.map 17.2 kB 0 [emitted] main
polyfills.js.map 353 kB 1 [emitted] polyfills
vendor.js.map 3.94 MB 2 [emitted] vendor
index.html 607 bytes [emitted]
+ 912 hidden modules
ERROR in ./src/client/app/app.component.ts
Module not found: Error: Cannot resolve 'file' or 'directory' ./app/app.component.html in C:\Users\markk\IdeaProjects\QuestionarySample2\src\client\app
@ ./src/client/app/app.component.ts 18:22-57
...
Child html-webpack-plugin for "index.html":
+ 1 hidden modules
C:\Users\markk\IdeaProjects\QuestionarySample2>
(there are more errors like that for other components, I replaced them with an ellipsis)
The loader converts the aforementioned source into:
import { Component } from "@angular/core";
@Component({
selector: 'app-root',
template: require('./app/app.component.html')
})
export class AppComponent {
}
And now it fails.
As I understand, the reason is that require works off the current directory, whereas the templateUrl is relative to the root. So, β./app/app.component.htmlβ is correct when taken relative to the root, but wrong when relative to the current location.
It is worth noting, that I do not use require anywhere in the source code, only the ES6 import statement, which is also relative to the current location. It is only the Angular2 templateUrl and styleUrls that are relative to the root.
Anyway, I must be doing something wrong, because nobody seems to have my problem. But what am I doing wrong?
The file name is src/client/app/app.component.ts. The source code directory structure is:
C:.
β .gitignore
β karma.conf.js
β package.json
β tsconfig.json
β tslint.json
β typings.json
β webpack.config.js
β
ββββconfig
β karma-test-shim.js
β karma.conf.js
β webpack.common.js
β webpack.dev.js
β webpack.prod.js
β webpack.scratch.js
β webpack.test.js
β
ββββsrc
ββββclient
β β global.css
β β index.html
β β main.ts
β β polyfills.ts
β β tsconfig.json
β β vendor.ts
β β
β ββββapp
β β app.component.html
β β app.component.ts
β β app.module.ts
β β app.routing.ts
β β settings.component.ts
β β signout.component.ts
β β
β ββββassets
β β ...
β β
β ββββquestionnaire
β β ...
β β
β ββββshared
β ...
β
ββββserver
api.ts
main.ts
tsconfig.json
The webpack configuration is:
webpack.config.js
module.exports = require('./config/webpack.dev.js');
./config/webpack.dev.js
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
const path = require('path');
module.exports = webpackMerge(commonConfig, {
// devtool: 'cheap-module-eval-source-map',
devtool: 'source-map',
output: {
publicPath: `http://localhost:${commonConfig.port}/`,
filename: '[name].js',
sourceMapFilename: '[name].js.map',
chunkFilename: '[id].chunk.js'
},
plugins: [
new ExtractTextPlugin('[name].css')
],
devServer: {
inline: true,
hot: true,
progress: true,
port: commonConfig.port,
proxy: {
'/api*': {
target: `http://localhost:${commonConfig.port - 1}`
}
},
historyApiFallback: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
}
});
./config/webpack.common.js
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');
module.exports = {
port: process.env.PORT || 3000,
entry: {
polyfills: './src/client/polyfills',
vendor: './src/client/vendor',
main: './src/client/main'
},
output: {
path: path.join(__dirname, '../dist/client'),
},
resolve: {
extensions: ['', '.js', '.ts']
},
module: {
loaders: [
{
test: /\.ts$/,
loaders: ['ts', 'angular2-template-loader']
},
{
test: /\.html$/,
// loader: 'html'
loader: 'raw'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file?name=assets/[name].[hash].[ext]'
},
// {
// test: /\.css$/,
// exclude: path.join(__dirname, '../src/client/app'),
// loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
// },
{
test: /\.css$/,
// exclude: path.join(__dirname, '../src/client/app'),
loader: 'raw'
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['main', 'vendor', 'polyfills']
}),
new HtmlWebpackPlugin({
template: 'src/client/index.html'
})
]
};
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (3 by maintainers)
Anyone found a solution for this? spent days with no luck. Any input will be appreciated.
Has someone found a fix for this yet?