gpt4 book ai didi

node.js - Docker + Webpack (Dev Server) + Yarnpkg 不完整的构建

转载 作者:IT老高 更新时间:2023-10-28 12:46:29 28 4
gpt4 key购买 nike

问题

转换 webpack现在在本地运行的项目在 docker 内部运行容器。这项工作分为两个 git分支机构:develop , 和 containers .

本地(无容器)

develop是稳定的基础,它通过本地运行 $ yarn install && npm run devpackage.json 中给出以下内容

"scripts": {
"start": "node .",
"env:dev": "cross-env NODE_ENV=development",
"env:prod": "cross-env NODE_ENV=production",
"predev": "npm run prebuild",
"dev": "npm run env:dev -- webpack-dev-server",
//[...]
}

分公司develop包括yarn.lock ,尽管 FWIW,$ rm yarn.lock && yarn install --force && npm run dev确实正确启动了服务器,即 GET http://localhost:3000给我主页,正如我期望看到的那样。在 $ git checkout containers 之后,以上所有操作都相同

docker

关闭本地开发服务器后,我运行 $ git checkout containers ,并且此分支不包含 yarn.lockpackage.lock .然后我运行 $ docker-compose up --build web (在单独的终端中,在包含 docker-compose.yaml 中的以下内容的同级目录中)

   web:
build:
context: ../frontend/
dockerfile: Dockerfile
env_file: ../frontend/.env
volumes:
- ../frontend/src:/code/src
ports:
- "3001:3000"
depends_on:
- api
networks:
- backend

frontend/Dockerfile服务web是这样的

# Dockerfile

FROM node:latest
RUN mkdir /code
ADD . /code/
WORKDIR /code/
RUN yarn cache clean && yarn install --non-interactive --force && npm rebuild node-sass

CMD npm run dev --verbose

给定

#frontend/.dockerignore

node_modules
deploy
.circleci
stories
.storybook

一切似乎顺利,启动的最后一行是web_1 | Server is running at http://localhost:3000/. 然而当我GET http://localhost:3001 (注意 docker-compose 中的端口映射),返回的页面不包含预期的 <style>...</style> <head> 中的标签应该由 webpack 注入(inject)(据我所知) ,给定下面的配置

// https://github.com/diegohaz/arc/wiki/Webpack
const path = require('path')
const devServer = require('@webpack-blocks/dev-server2')
const splitVendor = require('webpack-blocks-split-vendor')
const happypack = require('webpack-blocks-happypack')
const serverSourceMap = require('webpack-blocks-server-source-map')
const nodeExternals = require('webpack-node-externals')
const AssetsByTypePlugin = require('webpack-assets-by-type-plugin')
const ChildConfigPlugin = require('webpack-child-config-plugin')
const SpawnPlugin = require('webpack-spawn-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')

const {
addPlugins, createConfig, entryPoint, env, setOutput,
sourceMaps, defineConstants, webpack, group,
} = require('@webpack-blocks/webpack2')

const host = process.env.HOST || 'localhost'
const port = (+process.env.PORT + 1) || 3001
const sourceDir = process.env.SOURCE || 'src'
const publicPath = `/${process.env.PUBLIC_PATH || ''}/`.replace('//', '/')
const sourcePath = path.join(process.cwd(), sourceDir)
const outputPath = path.join(process.cwd(), 'dist/public')
const assetsPath = path.join(process.cwd(), 'dist/assets.json')
const clientEntryPath = path.join(sourcePath, 'client.js')
const serverEntryPath = path.join(sourcePath, 'server.js')
const devDomain = `http://${host}:${port}/`

//[...]
const sass = () => () => ({
module: {
rules: [
{
test: /\.(scss|sass)$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'sass-loader'},
],
},
],
},
})

const extractSass = new ExtractTextPlugin({
filename: 'style.css',
})

const prodSass = () => () => ({
module: {
rules: [
{ test: /\.(scss|sass)$/,
use: extractSass.extract({
use: [
{ loader: 'css-loader', options: { minimize: true } },
{ loader: 'sass-loader' },
],
fallback: 'style-loader',
}),
},
],
},
})

const babel = () => () => ({
module: {
rules: [
{ test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader' },
],
},
})

const assets = () => () => ({
module: {
rules: [
{ test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/, loader: 'url-loader?limit=8000' },
],
},
})

const resolveModules = modules => () => ({
resolve: {
modules: [].concat(modules, ['node_modules']),
},
})

const base = () => group([
setOutput({
filename: '[name].js',
path: outputPath,
publicPath,
}),
defineConstants({
'process.env.NODE_ENV': process.env.NODE_ENV,
'process.env.PUBLIC_PATH': publicPath.replace(/\/$/, ''),
}),
addPlugins([
new webpack.ProgressPlugin(),
extractSass,
]),
apiInsert(),
happypack([
babel(),
]),
assets(),
resolveModules(sourceDir),

env('development', [
setOutput({
publicPath: devDomain,
}),
sass(),
]),

env('production', [
prodSass(),
]),
])

const server = createConfig([
base(),
entryPoint({ server: serverEntryPath }),
setOutput({
filename: '../[name].js',
libraryTarget: 'commonjs2',
}),
addPlugins([
new webpack.BannerPlugin({
banner: 'global.assets = require("./assets.json");',
raw: true,
}),
]),
() => ({
target: 'node',
externals: [nodeExternals()],
stats: 'errors-only',
}),

env('development', [
serverSourceMap(),
addPlugins([
new SpawnPlugin('npm', ['start']),
]),
() => ({
watch: true,
}),
]),
])

const client = createConfig([
base(),
entryPoint({ client: clientEntryPath }),
addPlugins([
new AssetsByTypePlugin({ path: assetsPath }),
new ChildConfigPlugin(server),
]),

env('development', [
devServer({
contentBase: 'public',
stats: 'errors-only',
historyApiFallback: { index: publicPath },
headers: { 'Access-Control-Allow-Origin': '*' },
host,
port,
}),
sourceMaps(),
addPlugins([
new webpack.NamedModulesPlugin(),
]),
]),

env('production', [
splitVendor(),
addPlugins([
new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }),
]),
]),
])

module.exports = client

有趣的是,将此行添加到 package.json

 "dev-docker": "npm run predev && npm run env:dev -- webpack  --progress --watch --watch-poll",

并将 Dockerfile 的最后一行更改为 CMD npm run dev-docker确实产生了预期的效果...

假设

我目前的怀疑是我遗漏了一些关于 webpack 开发服务器如何处理其加载程序输出的信息,并且没有正确映射某些端口,但这是在黑暗中的一个镜头。

或者,webpack-dev-server版本有问题。本地是 4.4.2 docker 在哪里显示5.6.0 ,尽管这似乎不是问题,因为最新的文档与我自己的设置相匹配。我已经确认 package.json加载器模块的规范是每个模块的最新稳定版。

道歉

认识到这是由多种技术以一种依赖于配置且必然具有特殊性的方式交叉导致的问题,我谦虚地请求您帮助解决这个依赖 hell 。如果看起来我不明白某个给定的拼图是如何运作的,我很高兴听到它。任何想法、线索或建议,无论多么微不足道,都将不胜感激,并尽我所能加以利用。

最佳答案

这里是长镜头,但我试图在 docker 容器中运行 grails-vue 应用程序,并且遇到了 webpack-dev-server 的端口映射未正确公开的问题。

我在 github https://github.com/webpack/webpack-dev-server/issues/547 上发现了这个问题这导致我在 package.json 中将 --host 0.0.0.0 添加到我的开发任务中,如下所示:

"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 0.0.0.0"

这解决了我的问题,也许这会帮助你找到答案。

关于node.js - Docker + Webpack (Dev Server) + Yarnpkg 不完整的构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49180734/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com