index.js
import _ from 'lodash';
import searchPage from './search.js'
import './index.css';
function component() {
const button = document.createElement('button')
button.classList.add('button')
button.textContent = 'click me'
return button;
}
document.body.appendChild(component());
function searchPageComponent() {
document.body.remove()
const searchPageVar = searchPage()
console.log(searchPageVar)
return searchPageVar
}
const button = document.querySelector('.button')
button.addEventListener('click', () => {
document.body.appendChild(searchPageComponent())
})
search.js
export default function searchPage() {
const stockSearchPage = document.createElement('div')
stockSearchPage.setAttribute('id', 'search')
stockSearchPage.innerHTML = 'Search Page'
console.log('search for stocks')
return stockSearchPage
}
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: {
index: './src/index.js',
print: './src/search.js',
},
devtool: 'inline-source-map',
devServer: {
static: './dist',
},
plugins: [
new HtmlWebpackPlugin({
title: 'Webpack-demo',
}),
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
clean: true,
publicPath: '/',
},
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
],
},
optimization: {
runtimeChunk: 'single',
},
};
Click me button
Missing div *appears in console with no errors as to why not appended to the page
0 comments:
Post a Comment
Thanks