Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: service and pack plugins #352

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,770 changes: 1,770 additions & 0 deletions .yarn/releases/yarn.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ plugins:
pnpEnableEsmLoader: true

yarnPath: yarn/cli/src/cli.dev.mjs
#yarnPath: .yarn/releases/yarn.cjs
2 changes: 1 addition & 1 deletion code/code-service-worker/src/service.worker.content.ts

Large diffs are not rendered by default.

14 changes: 4 additions & 10 deletions code/code-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,10 @@ export class Service {

return webpack(
await config.build('development', [
{
name: 'start-server',
use: StartServerPlugin,
args: [
{
stdout: pass,
stderr: pass,
},
],
},
new StartServerPlugin({
stdout: pass,
stderr: pass
})
])
).watch({}, (error, stats) => {
if (error) {
Expand Down
164 changes: 89 additions & 75 deletions code/code-service/src/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { readFile } from 'node:fs/promises'
import { ModuleTypes } from './webpack.interfaces.js'
import type { WebpackEnvironment } from './webpack.interfaces.js'

import { writeFile } from 'node:fs/promises'
import { mkdtemp } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'

import Config from 'webpack-chain-5'

import { webpack } from '@atls/code-runtime/webpack'
import { tsLoaderPath } from '@atls/code-runtime/webpack'
import { nodeLoaderPath } from '@atls/code-runtime/webpack'
Expand All @@ -20,68 +20,101 @@ export class WebpackConfig {

async build(
environment: WebpackEnvironment = 'production',
plugins: Array<{
use: Config.PluginClass<webpack.WebpackPluginInstance> | webpack.WebpackPluginInstance
additionalPlugins: Array<{
use: webpack.WebpackPluginInstance
args: Array<any>
name: string
}> = []
): Promise<webpack.Configuration> {
const config = new Config()

await this.applyCommon(config, environment)
await this.applyPlugins(config, environment)
await this.applyModules(config)
const configFile = join(await mkdtemp(join(tmpdir(), 'code-service-')), 'tsconfig.json')

plugins.forEach((plugin) => {
config.plugin(plugin.name).use(plugin.use, plugin.args)
})
await writeFile(configFile, '{"include":["**/*"]}')

return config.toConfig()
}
const type = await this.getWorkspaceType()

private async applyCommon(config: Config, environment: WebpackEnvironment): Promise<void> {
config
.mode(environment)
.bail(environment === 'production')
.target('async-node')
.optimization.minimize(false)
const webpackExternals = new WebpackExternals(this.cwd)
const externals = ['webpack/hot/poll?100', await webpackExternals.build()]

config.entry('index').add(join(this.cwd, 'src/index'))
const plugins = this.createPlugins(environment, additionalPlugins)

if (environment === 'development') {
config.entry('hot').add('webpack/hot/poll?100')
return {
mode: environment,
bail: environment === 'production',
target: 'async-node',
optimization: { minimize: false },
experiments: {
outputModule: type === 'module',
},
plugins,
entry: {
index: join(this.cwd, 'src/index'),
...(environment === 'development' && { hot: 'webpack/hot/poll?100'}),
},
node: { __dirname: false, __filename: false },
output: {
path: join(this.cwd, 'dist'),
filename: '[name].js',
library: { type },
chunkFormat: type,
module: type === 'module',
},
resolve: {
extensionAlias: { '.js': ['.tsx', '.ts', '.js'], '.jsx': ['.tsx', '.ts', '.js'], '.cjs': ['.cjs', '.cts'], '.mjs': ['.mjs', '.mts'] },
extensions: ['.tsx', '.ts', '.js'],
alias: {
'class-transformer/storage': 'class-transformer/cjs/storage',
}
},
externals,
externalsType: type === 'module' ? 'import' : 'commonjs',
externalsPresets: {
node: true,
},
devtool:
environment === 'production' ? 'source-map' : 'eval-cheap-module-source-map',
module: {
rules: [
{
test: /(^.?|\.[^d]|[^.]d|[^.][^d])\.tsx?$/,
use: {
loader: tsLoaderPath,
options: {
transpileOnly: true,
experimentalWatchApi: true,
onlyCompileBundledFiles: true,
compilerOptions: { ...tsconfig.compilerOptions, sourceMap: true },
context: this.cwd,
configFile,
}
},
},
{ test: /\.(woff|woff2|eot|ttf|otf)$/i, type: 'asset/resource' },
{ test: /\.(png|svg|jpg|jpeg|gif)$/i, type: 'asset/resource' },
{ test: /\.node$/, use: nodeLoaderPath },
],
},
}

config.output.path(join(this.cwd, 'dist')).filename('[name].js')
config.output.chunkFormat('module')
config.output.module(true)

config.resolve.extensions.add('.tsx').add('.ts').add('.js')
config.resolve.extensionAlias
.set('.js', ['.js', '.ts'])
.set('.jsx', ['.jsx', '.tsx'])
.set('.cjs', ['.cjs', '.cts'])
.set('.mjs', ['.mjs', '.mts'])

config.resolve.alias.set('class-transformer/storage', 'class-transformer/cjs/storage')

config.externalsType('import')
config.externalsPresets({ node: true })
config.externals(['webpack/hot/poll?100', await new WebpackExternals(this.cwd).build()])

config.devtool(environment === 'production' ? 'source-map' : 'eval-cheap-module-source-map')

config.experiments({ outputModule: true })
}

private async applyPlugins(config: Config, environment: WebpackEnvironment): Promise<void> {
if (environment === 'development') {
config.plugin('hot').use(webpack.HotModuleReplacementPlugin)
private async getWorkspaceType(): Promise<ModuleTypes> {
try {
const content = await readFile(join(this.cwd, 'package.json'), 'utf-8')
const { type = 'commonjs' } = JSON.parse(content)

return type
} catch {
return 'module'
}
}

config.plugin('ignore').use(webpack.IgnorePlugin, [
{
private createPlugins(environment: string, additionalPlugins: []) {
const plugins = [
new webpack.IgnorePlugin({
checkResource: (resource: string): boolean => {
if (resource.endsWith('.js.map')) {
return true
}

if (!LAZY_IMPORTS.includes(resource)) {
return false
}
Expand All @@ -96,33 +129,14 @@ export class WebpackConfig {

return false
},
},
])
}

private async applyModules(config: Config): Promise<void> {
const configFile = join(await mkdtemp(join(tmpdir(), 'code-service-')), 'tsconfig.json')
}),
...additionalPlugins,
]

await writeFile(configFile, '{"include":["**/*"]}')
if (environment === 'development') {
plugins.push(new webpack.HotModuleReplacementPlugin())
}

config.module
.rule('ts')
.test(/.tsx?$/)
.use('ts')
.loader(tsLoaderPath)
.options({
transpileOnly: true,
experimentalWatchApi: true,
onlyCompileBundledFiles: true,
compilerOptions: { ...tsconfig.compilerOptions, sourceMap: true },
context: this.cwd,
configFile,
})

config.module
.rule('node')
.test(/\.node$/)
.use('node')
.loader(nodeLoaderPath)
return plugins
}
}
5 changes: 5 additions & 0 deletions code/code-service/src/webpack.ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ export const LAZY_IMPORTS = [
'@mikro-orm/mariadb',
'@mikro-orm/sqlite',
'@mikro-orm/mysql',

// nestjs
'@nestjs/mongoose',
'@nestjs/typeorm/dist/common/typeorm.utils',
'@nestjs/sequelize/dist/common/sequelize.utils',
]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class StartServerPlugin {
this.options = options
}

apply = (compiler: webpack.Compiler): void => {
apply(compiler: webpack.Compiler): void {
compiler.hooks.afterEmit.tapAsync({ name: 'StartServerPlugin' }, this.afterEmit)
}

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ __metadata:
"@atls/yarn-run-utils": "workspace:*"
"@atls/yarn-test-utils": "workspace:*"
"@jest/globals": "npm:29.7.0"
"@monstrs/logger": "npm:latest"
"@types/react": "npm:^18.3.3"
"@yarnpkg/builder": "npm:4.1.1"
"@yarnpkg/cli": "npm:4.2.2"
Expand Down
4 changes: 4 additions & 0 deletions yarn/cli/patches/cli.patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const replacements = [
from: ',_a=_typeModule(_typeModule),',
to: ';var _a=_typeModule(_typeModule);',
},
{
from: '_a = _typeModule(_typeModule),',
to: 'var _a = _typeModule(_typeModule);',
},
]

const bundle = join(fileURLToPath(new URL('.', import.meta.url)), '../bundles/yarn.cjs')
Expand Down
11 changes: 4 additions & 7 deletions yarn/pack-utils/src/copy.utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { Workspace } from '@yarnpkg/core'
import type { Cache } from '@yarnpkg/core'
import type { Project } from '@yarnpkg/core'
import type { Report } from '@yarnpkg/core'
import type { Descriptor } from '@yarnpkg/core'
import type { Locator } from '@yarnpkg/core'
import type { PortablePath } from '@yarnpkg/fslib'

import { Workspace } from '@yarnpkg/core'
import { Manifest } from '@yarnpkg/core'
import { structUtils } from '@yarnpkg/core'
import { xfs } from '@yarnpkg/fslib'
import { ppath } from '@yarnpkg/fslib'
import { toFilename } from '@yarnpkg/fslib'

export const copyCacheMarkedFiles = async (
project: Project,
Expand Down Expand Up @@ -52,7 +51,7 @@ export const copyPlugins = async (
destination: PortablePath,
report: Report
): Promise<void> => {
const pluginDir = ppath.join(toFilename('.yarn'), toFilename('plugins'))
const pluginDir = ppath.join('.yarn', 'plugins')

if (await xfs.existsPromise(ppath.join(project.cwd, pluginDir))) {
report.reportInfo(null, pluginDir)
Expand Down Expand Up @@ -136,14 +135,12 @@ export const copyYarnRelease = async (
report: Report
): Promise<void> => {
const src = project.configuration.get('yarnPath')
// @ts-expect-error any
const path = ppath.relative(project.cwd, src)
const path = ppath.relative(project.cwd, src!)
const dest = ppath.join(destination, path)

report.reportInfo(null, path)

// @ts-expect-error any
await xfs.copyPromise(dest, src, {
await xfs.copyPromise(dest, src!, {
overwrite: true,
})
}
1 change: 0 additions & 1 deletion yarn/pack-utils/src/export/ProtocolResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class ProtocolResolver implements Resolver {
}

supportsLocator(locator: Locator, opts: MinimalResolveOptions) {
// eslint-disable-next-line
if (semver.valid(locator.reference)) return true

if (TAG_REGEXP.test(locator.reference)) return true
Expand Down
3 changes: 2 additions & 1 deletion yarn/pack-utils/src/export/exportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

// @ts-nocheck

import { createGzip } from 'node:zlib'

import { Project } from '@yarnpkg/core'
import { VirtualFetcher } from '@yarnpkg/core'
import { Workspace } from '@yarnpkg/core'
Expand All @@ -16,7 +18,6 @@ import { tgzUtils } from '@yarnpkg/core'
import { ppath } from '@yarnpkg/fslib'
import { xfs } from '@yarnpkg/fslib'
import { packUtils } from '@yarnpkg/plugin-pack'
import { createGzip } from 'zlib'
import tar from 'tar-stream'

import { MultiResolver } from './MultiResolver.js'
Expand Down
2 changes: 1 addition & 1 deletion yarn/pack-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as workspacesUtils from './workspaces.utils.js'
import * as copyUtils from './copy.utils.js'
import * as packUtils from './pack.utils.js'
import * as workspacesUtils from './workspaces.utils.js'

export { workspacesUtils, copyUtils, packUtils }
Loading
Loading