如何使用webpack为vanilla javascript项目实现类似角度的项目结构

时间:2018-05-28 13:57:41

标签: javascript webpack

我正在尝试使用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'
                ]
            }
        ]
    }
};

index.js

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());

试图实现这个项目结构

enter image description here

0 个答案:

没有答案