Skip to content

Commit

Permalink
feat: support this.parse, close #90
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 6, 2022
1 parent 69e7149 commit 8f5c6ad
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Currently supports:

| Hook | Rollup | Vite | Webpack 4 | Webpack 5 | esbuild |
| ---- | :----: | :--: | :-------: | :-------: | :-----: |
| [`this.parse`](https://rollupjs.org/guide/en/#thisparse) ||||||
| [`this.addWatchFile`](https://rollupjs.org/guide/en/#thisaddwatchfile) ||||||
| [`this.emitFile`](https://rollupjs.org/guide/en/#thisemitfile)<sup>4</sup> ||||||
| [`this.getWatchFiles`](https://rollupjs.org/guide/en/#thisgetwatchfiles) ||||||
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:build": "nr build && jiti scripts/buildFixtures.ts"
},
"dependencies": {
"acorn": "^8.7.1",
"chokidar": "^3.5.3",
"webpack-sources": "^3.2.3",
"webpack-virtual-modules": "^0.4.3"
Expand Down
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

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

11 changes: 10 additions & 1 deletion src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import path from 'path'
import chokidar from 'chokidar'
import type { PartialMessage } from 'esbuild'
import type { SourceMap } from 'rollup'
import type { RawSourceMap } from '@ampproject/remapping/dist/types/types'
import { Parser } from 'acorn'
import { RawSourceMap } from '@ampproject/remapping'
import type { UnpluginBuildContext, UnpluginContext, UnpluginContextMeta, UnpluginFactory, UnpluginInstance } from '../types'
import { combineSourcemaps, fixSourceMap, guessLoader } from './utils'

Expand All @@ -28,6 +29,14 @@ export function getEsbuildPlugin <UserOptions = {}> (
const onLoadFilter = plugin.esbuild?.onLoadFilter ?? /.*/

const context:UnpluginBuildContext = {
parse (code: string, opts: any = {}) {
return Parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true,
...opts
})
},
addWatchFile (id) {
watchList.add(path.resolve(id))
},
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EmittedAsset, Plugin as RollupPlugin, PluginContextMeta as RollupContextMeta, SourceMap } from 'rollup'
import type { AcornNode, EmittedAsset, Plugin as RollupPlugin, PluginContextMeta as RollupContextMeta, SourceMap } from 'rollup'
import type { Compiler as WebpackCompiler, WebpackPluginInstance } from 'webpack'
import type { Plugin as VitePlugin } from 'vite'
import type { Plugin as EsbuildPlugin } from 'esbuild'
Expand All @@ -21,6 +21,7 @@ export interface UnpluginBuildContext {
addWatchFile: (id: string) => void;
emitFile: (emittedFile: EmittedAsset) => void;
getWatchFiles: () => string[];
parse: (input: string, options?: any) => AcornNode;
}

export interface UnpluginOptions {
Expand Down
11 changes: 10 additions & 1 deletion src/webpack/genContext.ts → src/webpack/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import { resolve } from 'path'
// eslint-disable-next-line import/default
import sources from 'webpack-sources'
import type { Compilation } from 'webpack'
import { Parser } from 'acorn'
import type { UnpluginBuildContext } from 'src'

export default function genContext (compilation: Compilation):UnpluginBuildContext {
export function createContext (compilation: Compilation): UnpluginBuildContext {
return {
parse (code: string, opts: any = {}) {
return Parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true,
...opts
})
},
addWatchFile (id) {
(compilation.fileDependencies ?? compilation.compilationDependencies).add(
resolve(process.cwd(), id)
Expand Down
7 changes: 4 additions & 3 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import VirtualModulesPlugin from 'webpack-virtual-modules'
import type { Resolver, ResolveRequest } from 'enhanced-resolve'
import type { UnpluginContextMeta, UnpluginInstance, UnpluginFactory, WebpackCompiler, ResolvedUnpluginOptions } from '../types'
import { slash, backSlash } from './utils'
import genContext from './genContext'
import { createContext } from './context'

const _dirname = typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(import.meta.url))
const TRANSFORM_LOADER = resolve(_dirname, 'webpack/loaders/transform.js')
const LOAD_LOADER = resolve(_dirname, 'webpack/loaders/load.js')
Expand Down Expand Up @@ -154,7 +155,7 @@ export function getWebpackPlugin<UserOptions = {}> (

if (plugin.watchChange || plugin.buildStart) {
compiler.hooks.make.tapPromise(plugin.name, async (compilation) => {
const context = genContext(compilation)
const context = createContext(compilation)
if (plugin.watchChange && (compiler.modifiedFiles || compiler.removedFiles)) {
const promises:Promise<void>[] = []
if (compiler.modifiedFiles) {
Expand All @@ -178,7 +179,7 @@ export function getWebpackPlugin<UserOptions = {}> (

if (plugin.buildEnd) {
compiler.hooks.emit.tapPromise(plugin.name, async (compilation) => {
await plugin.buildEnd!.call(genContext(compilation))
await plugin.buildEnd!.call(createContext(compilation))
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LoaderContext } from 'webpack'
import { UnpluginContext } from '../../types'
import genContext from '../genContext'
import { createContext } from '../context'
import { slash } from '../utils'

export default async function load (this: LoaderContext<any>, source: string, map: any) {
Expand All @@ -22,7 +22,7 @@ export default async function load (this: LoaderContext<any>, source: string, ma
id = id.slice(plugin.__virtualModulePrefix.length)
}

const res = await plugin.load.call(Object.assign(this._compilation && genContext(this._compilation), context), slash(id))
const res = await plugin.load.call(Object.assign(this._compilation && createContext(this._compilation), context), slash(id))

if (res == null) {
callback(null, source, map)
Expand Down
4 changes: 2 additions & 2 deletions src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LoaderContext } from 'webpack'
import { UnpluginContext } from '../../types'
import genContext from '../genContext'
import { createContext } from '../context'

export default async function transform (this: LoaderContext<any>, source: string, map: any) {
const callback = this.async()
Expand All @@ -15,7 +15,7 @@ export default async function transform (this: LoaderContext<any>, source: strin
error: error => this.emitError(typeof error === 'string' ? new Error(error) : error),
warn: error => this.emitWarning(typeof error === 'string' ? new Error(error) : error)
}
const res = await plugin.transform.call(Object.assign(this._compilation && genContext(this._compilation), context), source, this.resource)
const res = await plugin.transform.call(Object.assign(this._compilation && createContext(this._compilation), context), source, this.resource)

if (res == null) {
callback(null, source, map)
Expand Down

0 comments on commit 8f5c6ad

Please sign in to comment.