This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fc982a
commit 8e9061b
Showing
10 changed files
with
212 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,119 @@ | ||
import { Plugin, Compiler, BannerPlugin, DefinePlugin } from 'webpack'; | ||
import { setEnvironment, getDefineVariables, getVariables } from './helpers'; | ||
import { | ||
setEnvironment, | ||
getDefineVariables, | ||
getVariables, | ||
getExternals, | ||
withExternals, | ||
getDependencies, | ||
} from './helpers'; | ||
import { PiletWebpackPluginOptions } from './types'; | ||
|
||
const pluginName = 'PiletWebpackPlugin'; | ||
|
||
export interface PiletWebpackPluginOptions { | ||
/** | ||
* The name of the pilet. | ||
*/ | ||
name: string; | ||
/** | ||
* The version of the pilet. | ||
*/ | ||
version: string; | ||
/** | ||
* The name of the Piral instance / app shell. | ||
*/ | ||
piral: string; | ||
/** | ||
* The schema version. By default, v1 is used. | ||
*/ | ||
schema?: 'v0' | 'v1' | 'none'; | ||
/** | ||
* The shared dependencies. By default, these are read from the | ||
* Piral instance. | ||
*/ | ||
externals?: Array<string>; | ||
/** | ||
* Additional environment variables to define. | ||
*/ | ||
variables?: Record<string, string>; | ||
} | ||
export class PiletWebpackPlugin implements Plugin { | ||
private variables: Record<string, string>; | ||
private externals: Array<string>; | ||
|
||
function getExternals(piral: string) { | ||
const shellPkg = require(`${piral}/package.json`); | ||
const piralExternals = shellPkg.pilets?.externals ?? []; | ||
return [ | ||
...piralExternals, | ||
'@dbeining/react-atom', | ||
'@libre/atom', | ||
'history', | ||
'react', | ||
'react-dom', | ||
'react-router', | ||
'react-router-dom', | ||
'tslib', | ||
'path-to-regexp', | ||
]; | ||
} | ||
constructor(private options: PiletWebpackPluginOptions) {} | ||
|
||
export class PiletWebpackPlugin implements Plugin { | ||
constructor(private options: PiletWebpackPluginOptions) { } | ||
piletVxWebpackConfigEnhancer(compiler: Compiler) { | ||
const config = compiler.options; | ||
|
||
apply(compiler: Compiler) { | ||
const environment = process.env.NODE_ENV || 'development'; | ||
const { name, version, piral, externals = getExternals(piral), schema } = this.options; | ||
setEnvironment(this.variables); | ||
withExternals(config, this.externals); | ||
|
||
const plugins = [new DefinePlugin(getDefineVariables(this.variables))]; | ||
compiler.hooks.afterEnvironment.tap(pluginName, () => {}); | ||
return plugins; | ||
} | ||
|
||
piletV1WebpackConfigEnhancer(compiler: Compiler) { | ||
const { name } = this.options; | ||
const config = compiler.options; | ||
const shortName = name.replace(/\W/gi, ''); | ||
const jsonpFunction = `pr_${shortName}`; | ||
const variables = { | ||
...getVariables(name, version, environment), | ||
...this.options.variables, | ||
}; | ||
const prName = `wp4Chunkpr_${shortName}`; | ||
const [mainEntry] = Object.keys(config.entry); | ||
|
||
setEnvironment(this.variables); | ||
withExternals(config, this.externals); | ||
|
||
const plugins = [ | ||
new DefinePlugin(getDefineVariables(variables)), | ||
new DefinePlugin(getDefineVariables(this.variables)), | ||
new BannerPlugin({ | ||
banner: `//@pilet v:1(${prName})`, | ||
entryOnly: true, | ||
include: `${mainEntry}.js`, | ||
raw: true, | ||
}), | ||
]; | ||
|
||
if (schema !== 'none') { | ||
const bannerSuffix = schema === 'v1' ? `1(${jsonpFunction})` : `0`; | ||
|
||
plugins.push( | ||
new BannerPlugin({ | ||
banner: `//@pilet v:${bannerSuffix}`, | ||
entryOnly: true, | ||
include: /\.js$/, | ||
raw: true, | ||
}), | ||
); | ||
} | ||
compiler.hooks.afterEnvironment.tap(pluginName, () => { | ||
config.output.jsonpFunction = `${prName}`; | ||
config.output.library = name; | ||
config.output.libraryTarget = 'umd'; | ||
config.output.auxiliaryComment = { | ||
commonjs2: `\nfunction define(d,k){if(typeof document!=='undefined'){var _r=${prName};delete ${prName};document.currentScript.app=k.apply(null,d.map(_r))}}define.amd=!0;`, | ||
} as any; | ||
}); | ||
|
||
setEnvironment(variables); | ||
return plugins; | ||
} | ||
|
||
plugins.forEach(plugin => plugin.apply(compiler)); | ||
piletV2WebpackConfigEnhancer(compiler: Compiler) { | ||
const { name, importmap } = this.options; | ||
const config = compiler.options; | ||
const shortName = name.replace(/\W/gi, ''); | ||
const prName = `wp4Chunkpr_${shortName}`; | ||
const [mainEntry] = Object.keys(config.entry); | ||
|
||
withExternals(config, this.externals); | ||
setEnvironment(this.variables); | ||
|
||
const dependencies = getDependencies(importmap, config); | ||
|
||
const plugins = [ | ||
new DefinePlugin(getDefineVariables(this.variables)), | ||
new BannerPlugin({ | ||
banner: `//@pilet v:2(${prName},${JSON.stringify(dependencies)})`, | ||
entryOnly: true, | ||
include: `${mainEntry}.js`, | ||
raw: true, | ||
}), | ||
]; | ||
|
||
compiler.hooks.afterEnvironment.tap(pluginName, () => { | ||
const current = compiler.options.externals; | ||
compiler.options.output.jsonpFunction = `${jsonpFunction}_chunks`; | ||
compiler.options.output.library = name; | ||
|
||
if (schema !== 'none') { | ||
compiler.options.output.libraryTarget = 'umd'; | ||
} | ||
|
||
if (schema === 'v1') { | ||
const reset = environment !== 'production' ? `delete ${jsonpFunction}_chunks;` : ''; | ||
compiler.options.output.auxiliaryComment = { | ||
commonjs2: `\nfunction define(d,k){${reset}(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`, | ||
} as any; | ||
} | ||
|
||
compiler.options.externals = Array.isArray(current) | ||
? [...current, ...externals] | ||
: current | ||
? [current, ...externals] | ||
: externals; | ||
config.module.rules.push({ parser: { system: false } }); | ||
config.output.jsonpFunction = `${prName}`; | ||
config.output.libraryTarget = 'system'; | ||
}); | ||
|
||
return plugins; | ||
} | ||
|
||
setup(compiler: Compiler) { | ||
const { name, version, piral, externals = getExternals(piral), schema } = this.options; | ||
const environment = process.env.NODE_ENV || 'development'; | ||
this.variables = { | ||
...getVariables(name, version, environment), | ||
...this.options.variables, | ||
}; | ||
this.externals = externals; | ||
|
||
switch (schema) { | ||
case 'v1': | ||
return this.piletV1WebpackConfigEnhancer(compiler); | ||
case 'v2': | ||
return this.piletV2WebpackConfigEnhancer(compiler); | ||
case 'v0': | ||
case 'none': | ||
default: | ||
return this.piletVxWebpackConfigEnhancer(compiler); | ||
} | ||
} | ||
|
||
apply(compiler: Compiler) { | ||
const plugins = this.setup(compiler); | ||
plugins.forEach(plugin => plugin.apply(compiler)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export interface SharedDependency { | ||
id: string; | ||
name: string; | ||
ref: string; | ||
type: 'local' | 'remote'; | ||
entry: string; | ||
} | ||
|
||
export interface PiletWebpackPluginOptions { | ||
/** | ||
* The name of the pilet. | ||
*/ | ||
name: string; | ||
/** | ||
* The version of the pilet. | ||
*/ | ||
version: string; | ||
/** | ||
* The name of the Piral instance / app shell. | ||
*/ | ||
piral: string; | ||
/** | ||
* The schema version. By default, v1 is used. | ||
*/ | ||
schema?: 'v0' | 'v1' | 'v2' | 'none'; | ||
/** | ||
* The shared dependencies. By default, these are read from the | ||
* Piral instance. | ||
*/ | ||
externals?: Array<string>; | ||
/** | ||
* Additional environment variables to define. | ||
*/ | ||
variables?: Record<string, string>; | ||
/** | ||
* The shared dependencies to consider. | ||
*/ | ||
importmap?: Array<SharedDependency>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.