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

feat: webpack-sources 的功能从webpack引入,避免执行引入版本有关问题 #12501

Merged
merged 8 commits into from
Sep 20, 2022
Merged
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@
"vue-template-compiler": "^2.6.11",
"webpack": "5.69.0",
"webpack-chain": "6.5.1",
"webpack-dev-server": "4.7.4",
"webpack-sources": "^3.2.3"
"webpack-dev-server": "4.7.4"
}
}
1 change: 0 additions & 1 deletion packages/taro-webpack5-prebundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"fs-extra": "^8.0.1",
"lodash": "^4.17.21",
"webpack-chain": "6.5.1",
"webpack-sources": "^3.2.3",
"webpack-virtual-modules": "^0.4.3"
},
"devDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions packages/taro-webpack5-prebundle/src/utils/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { promoteRelativePath } from '@tarojs/helper'
import path from 'path'
import { Chunk, Compilation } from 'webpack'
import { ConcatSource, Source } from 'webpack-sources'
import { Chunk, Compilation, sources } from 'webpack'

const { ConcatSource } = sources

/**
* 在文本头部加入一些 require 语句
*/
export function addRequireToSource (id: string, modules: Source, commonChunks: (Chunk | { name: string })[]) {
export function addRequireToSource (id: string, modules: sources.Source, commonChunks: (Chunk | { name: string })[]) {
const source = new ConcatSource()
commonChunks.forEach(chunkItem => {
source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(id, chunkItem.name)))});\n`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* MIT License http://www.opensource.org/licenses/mit-license.php
* Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
*/
import webpack from 'webpack'
import webpack, { sources } from 'webpack'
import ContainerPlugin from 'webpack/lib/container/ContainerPlugin'
import { ConcatSource } from 'webpack-sources'

const { ConcatSource } = sources

const { Template, RuntimeGlobals } = webpack
const ContainerEntryModule = require('webpack/lib/container/ContainerEntryModule')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
*/
import { META_TYPE } from '@tarojs/helper'
import webpack, { Compiler, NormalModule, RuntimeGlobals } from 'webpack'
import webpack, { Compiler, NormalModule, RuntimeGlobals, sources } from 'webpack'
import ContainerReferencePlugin from 'webpack/lib/container/ContainerReferencePlugin'
import RemoteModule from 'webpack/lib/container/RemoteModule'
import type { ContainerReferencePluginOptions, RemotesConfig } from 'webpack/types'
import { ConcatSource, RawSource } from 'webpack-sources'

import { addRequireToSource, getChunkEntryModule, getChunkIdOrName } from '../utils'
import { CollectedDeps, MF_NAME } from '../utils/constant'
Expand All @@ -23,6 +22,8 @@ const RemoteToExternalDependency = require('webpack/lib/container/RemoteToExtern
const PLUGIN_NAME = 'TaroContainerReferencePlugin'
const slashCode = '/'.charCodeAt(0)

const { RawSource } = sources

type MFOptions = Partial<ContainerReferencePluginOptions>

interface IParams {
Expand Down Expand Up @@ -191,7 +192,7 @@ export default class TaroContainerReferencePlugin extends ContainerReferencePlug
const hooks = webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation)
hooks.render.tap(
PLUGIN_NAME,
(modules: ConcatSource, { chunk }) => {
(modules: sources.ConcatSource, { chunk }) => {
const chunkEntryModule = getChunkEntryModule(compilation, chunk) as any
if (chunkEntryModule) {
const entryModule = chunkEntryModule.rootModule ?? chunkEntryModule
Expand Down
1 change: 0 additions & 1 deletion packages/taro-webpack5-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"webpack-chain": "6.5.1",
"webpack-dev-server": "4.7.4",
"webpack-format-messages": "^2.0.6",
"webpack-sources": "^3.2.3",
"webpackbar": "^5.0.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import { AppConfig, Config } from '@tarojs/taro'
import fs from 'fs-extra'
import { urlToRequest } from 'loader-utils'
import path from 'path'
import webpack, { Compilation, Compiler } from 'webpack'
import webpack, { Compilation, Compiler, sources } from 'webpack'
import EntryDependency from 'webpack/lib/dependencies/EntryDependency'
import { ConcatSource, RawSource } from 'webpack-sources'

import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency'
import { PrerenderConfig, validatePrerenderPages } from '../prerender/prerender'
Expand All @@ -30,6 +29,7 @@ import TaroNormalModulesPlugin from './TaroNormalModulesPlugin'
import TaroSingleEntryPlugin from './TaroSingleEntryPlugin'

const PLUGIN_NAME = 'TaroMiniPlugin'
const { ConcatSource, RawSource } = sources

interface ITaroMiniPluginOptions {
appEntry?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import md5 from 'md5'
import path from 'path'
import { Chunk, ChunkGraph, Compilation, Compiler, Module, sources } from 'webpack'
import SplitChunksPlugin from 'webpack/lib/optimize/SplitChunksPlugin'
import { ConcatSource, RawSource } from 'webpack-sources'

import type { IFileType } from '../utils/types'

const PLUGIN_NAME = 'MiniSplitChunkPlugin' // 插件名
const SUB_COMMON_DIR = 'sub-common' // 分包公共依赖目录
const SUB_VENDORS_NAME = 'sub-vendors' // 分包 vendors 文件名
const { ConcatSource, RawSource } = sources

const FileExtsMap = {
JS: '.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
taroJsComponents
} from '@tarojs/helper'
import { toDashed } from '@tarojs/shared'
import webpack, { Chunk, ChunkGraph, Compilation, Compiler } from 'webpack'
import { ConcatSource } from 'webpack-sources'
import webpack, { Chunk, ChunkGraph, Compilation, Compiler, sources } from 'webpack'

import { componentConfig } from '../template/component'
import type { AddPageChunks, IComponent } from '../utils/types'
import { addRequireToSource, getChunkEntryModule, getChunkIdOrName } from '../utils/webpack'
import TaroNormalModule from './TaroNormalModule'

const PLUGIN_NAME = 'TaroLoadChunksPlugin'
const { ConcatSource } = sources

interface IOptions {
commonChunks: string[]
Expand Down
7 changes: 4 additions & 3 deletions packages/taro-webpack5-runner/src/utils/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { promoteRelativePath } from '@tarojs/helper'
import path from 'path'
import { Chunk, Compilation } from 'webpack'
import { ConcatSource, Source } from 'webpack-sources'
import { Chunk, Compilation, sources} from 'webpack'

const { ConcatSource } = sources

/**
* 在文本头部加入一些 require 语句
*/
export function addRequireToSource (id: string, modules: Source, commonChunks: (Chunk | { name: string })[]) {
export function addRequireToSource (id: string, modules: sources.Source, commonChunks: (Chunk | { name: string })[]) {
const source = new ConcatSource()
commonChunks.forEach(chunkItem => {
source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(id, chunkItem.name)))});\n`)
Expand Down
133 changes: 0 additions & 133 deletions types/webpack-sources.d.ts

This file was deleted.