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(build): add an option to use SystemJS #588

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"rollup-plugin-web-worker-loader": "^1.3.1",
"selfsigned": "^1.10.8",
"slash": "^3.0.0",
"systemjs": "^6.7.1",
"vue": "^3.0.2",
"ws": "^7.3.1"
},
Expand Down
26 changes: 23 additions & 3 deletions src/node/build/buildPluginHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../utils'
import { resolveAsset, registerAssets } from './buildPluginAsset'
import { InternalResolver } from '../resolver'
import { UserConfig } from '../config'
import { UserConfig, BuildConfig } from '../config'
import {
parse as Parse,
transform as Transform,
Expand All @@ -28,6 +28,7 @@ export const createBuildHtmlPlugin = async (
inlineLimit: number,
resolver: InternalResolver,
shouldPreload: ((chunk: OutputChunk) => boolean) | null,
useSystemJs: BuildConfig['useSystemJs'],
config: UserConfig
) => {
if (!fs.existsSync(indexPath)) {
Expand Down Expand Up @@ -80,11 +81,30 @@ export const createBuildHtmlPlugin = async (
}
}

const injectSystemJs = (html: string) => {
let tag: string
if (typeof useSystemJs === 'string') {
tag = `<script src="${useSystemJs}"></script>`
} else {
const systemJsRuntime = fs
.readFileSync(require.resolve('systemjs/dist/s.min.js'), 'utf8')
.replace(/\n\/\/# sourceMappingURL=[^\n]+\n$/, '')

tag = `<script>${systemJsRuntime}</script>`
}
if (/<\/head>/.test(html)) {
return html.replace(/<\/head>/, `${tag}\n</head>`)
}
return tag + '\n' + html
}

const injectScript = (html: string, filename: string) => {
filename = isExternalUrl(filename)
? filename
: `${publicBasePath}${path.posix.join(assetsDir, filename)}`
const tag = `<script type="module" src="${filename}"></script>`
const tag = useSystemJs
? `<script>System.import('${filename}');</script>`
: `<script type="module" src="${filename}"></script>`
if (/<\/head>/.test(html)) {
return html.replace(/<\/head>/, `${tag}\n</head>`)
} else {
Expand All @@ -105,7 +125,7 @@ export const createBuildHtmlPlugin = async (
}

const renderIndex = async (bundleOutput: RollupOutput['output']) => {
let result = processedHtml
let result = useSystemJs ? injectSystemJs(processedHtml) : processedHtml
for (const chunk of bundleOutput) {
if (chunk.type === 'chunk') {
if (chunk.isEntry) {
Expand Down
6 changes: 5 additions & 1 deletion src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ function prepareConfig(config: Partial<BuildConfig>): BuildConfig {
rollupPluginVueOptions = {},
root = process.cwd(),
shouldPreload = null,
useSystemJs = false,
silent = false,
sourcemap = false,
terserOptions = {},
Expand Down Expand Up @@ -330,6 +331,7 @@ function prepareConfig(config: Partial<BuildConfig>): BuildConfig {
rollupPluginVueOptions,
root,
shouldPreload,
useSystemJs,
silent,
sourcemap,
terserOptions,
Expand Down Expand Up @@ -365,6 +367,7 @@ export async function build(
silent,
sourcemap,
shouldPreload,
useSystemJs,
env,
mode: configMode,
define: userDefineReplacements,
Expand Down Expand Up @@ -405,6 +408,7 @@ export async function build(
assetsInlineLimit,
resolver,
shouldPreload,
useSystemJs,
options
)

Expand Down Expand Up @@ -592,7 +596,7 @@ export async function build(

await bundle[write ? 'write' : 'generate']({
dir: resolvedAssetsPath,
format: 'es',
format: useSystemJs ? 'system' : 'es',
sourcemap,
entryFileNames: `[name].[hash].js`,
chunkFileNames: `[name].[hash].js`,
Expand Down
24 changes: 24 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,30 @@ export interface BuildConfig extends Required<SharedConfig> {
* @internal
*/
ssr?: boolean
/**
* Set to `false` will produce modular javascript code,
* which requires browser's native ES module support,
* other options will use SystemJS to load each module instead.
* Set to `true` will insert the SystemJS runtime to a inline script tag,
* set to string will load the SystemJS runtime via CDN you specified.
*
* Example `vite.config.js`:
* ```js
* module.exports = {
* // by default vite will not use SystemJS
* useSystemJs: false,
*
* // will insert a inline script tag contains SystemJS runtime
* useSystemJs: true,
*
* // will load SystemJS runtime via CDN
* useSystemJs: '//unpkg.com/systemjs@6/dist/s.min.js',
* }
* ```
*
* @default false
*/
useSystemJs: boolean | string

// The following are API / config only and not documented in the CLI. --------
/**
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6433,6 +6433,11 @@ symbol-tree@^3.2.4:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

systemjs@^6.7.1:
version "6.7.1"
resolved "https://registry.yarnpkg.com/systemjs/-/systemjs-6.7.1.tgz#3db5036f450180a0701e078fbb5b434a690026f0"
integrity sha512-Q78H/SYy9ErC8PH8r9vA/FcQ3X+Hf33dOpx2JKP/Ma6f2gHuSScPFuCKZH+6CIj7EsIJlzODxSG4mMIpjOh5oA==

tar-fs@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-2.1.0.tgz#d1cdd121ab465ee0eb9ccde2d35049d3f3daf0d5"
Expand Down