Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用webpack为一个香草javascript项目实现类似angular的项目结构_Javascript_Webpack - Fatal编程技术网

如何使用webpack为一个香草javascript项目实现类似angular的项目结构

如何使用webpack为一个香草javascript项目实现类似angular的项目结构,javascript,webpack,Javascript,Webpack,我正在尝试使用webpack为自己制作一个样板,我已经创建了一个基本项目,但我想用某种方式来实现类似角度的代码结构,其中每个组件都有自己的.html、.js和.css文件,我确实使用jspm找到了这样的结构,但我正在尝试使用webpack实现同样的结构 以下是使用JSPM的工作副本:-- https://github.com/vijay-vadlamani/my-sample-project package.json { "name": "webpack-boilerplate", "v

我正在尝试使用webpack为自己制作一个样板,我已经创建了一个基本项目,但我想用某种方式来实现类似角度的代码结构,其中每个组件都有自己的.html、.js和.css文件,我确实使用jspm找到了这样的结构,但我正在尝试使用webpack实现同样的结构

以下是使用JSPM的工作副本:--
https://github.com/vijay-vadlamani/my-sample-project

package.json

{
  "name": "webpack-boilerplate",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^0.1.19",
    "css-loader": "^0.28.11",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.21.0",
    "webpack": "^4.9.1",
    "webpack-cli": "^2.1.4",
    "webpack-dev-server": "^3.1.4"
  },
  "dependencies": {
    "lodash": "^4.17.10"
  }
}
webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');

module.exports = {
    entry: './src/index.js',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist',
        hot: true
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
        title: 'Output Management'
        }),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ],
    output: {
        filename : 'main.js',
        path: path.resolve(__dirname, 'dist')
    },
    module : {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader'
                ]
            }
        ]
    }
};
import _ from 'lodash';
import './style.css';
// import 

function component() {
    var element = document.createElement('div');

    let tmp = `
    <div class="container">
        <div id="content">Hi</div>
    </div>
    <footer class="footer">
        <p class="text-center">
            ©2015 - Vijay Vadlamani
        </p>
    </footer>
    `;

    element.innerHTML = `${tmp}`;

    return element;
}

document.body.appendChild(component());
index.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');

module.exports = {
    entry: './src/index.js',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist',
        hot: true
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
        title: 'Output Management'
        }),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ],
    output: {
        filename : 'main.js',
        path: path.resolve(__dirname, 'dist')
    },
    module : {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader'
                ]
            }
        ]
    }
};
import _ from 'lodash';
import './style.css';
// import 

function component() {
    var element = document.createElement('div');

    let tmp = `
    <div class="container">
        <div id="content">Hi</div>
    </div>
    <footer class="footer">
        <p class="text-center">
            ©2015 - Vijay Vadlamani
        </p>
    </footer>
    `;

    element.innerHTML = `${tmp}`;

    return element;
}

document.body.appendChild(component());
从“lodash”导入; 导入“/style.css”; //进口 函数组件(){ var-element=document.createElement('div'); 设tmp=` 你好

©2015-维杰·瓦德拉马尼

`; element.innerHTML=`${tmp}`; 返回元素; } document.body.appendChild(component()); 试图实现这个项目结构


这很有意思,但你有什么具体问题吗?有什么问题吗?@Kaddath是的,现在我可以呈现index.js中的内容,但无法引入HomeComponent或xyzComponent中的内容